Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added --watch-ignored new command line option #2077

Merged
merged 19 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cmd/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type CmdRunParams = {|
preInstall: boolean,
sourceDir: string,
watchFile?: string,
watchIgnored?: Array<string>,
startUrl?: Array<string>,
target?: Array<string>,
args?: Array<string>,
Expand Down Expand Up @@ -84,6 +85,7 @@ export default async function run(
preInstall = false,
sourceDir,
watchFile,
watchIgnored,
startUrl,
target,
args,
Expand Down Expand Up @@ -248,6 +250,7 @@ export default async function run(
extensionRunner,
sourceDir,
watchFile,
watchIgnored,
artifactsDir,
ignoreFiles,
noInput,
Expand Down
8 changes: 7 additions & 1 deletion src/extension-runners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export type WatcherCreatorParams = {|
reloadExtension: (string) => void,
sourceDir: string,
watchFile?: string,
watchIgnored?: Array<string>,
artifactsDir: string,
onSourceChange?: OnSourceChangeFn,
ignoreFiles?: Array<string>,
Expand All @@ -250,7 +251,8 @@ export type WatcherCreatorFn = (params: WatcherCreatorParams) => Watchpack;

export function defaultWatcherCreator(
{
reloadExtension, sourceDir, watchFile, artifactsDir, ignoreFiles,
reloadExtension, sourceDir, watchFile,
watchIgnored, artifactsDir, ignoreFiles,
onSourceChange = defaultSourceWatcher,
createFileFilter = defaultFileFilterCreator,
}: WatcherCreatorParams
Expand All @@ -261,6 +263,7 @@ export function defaultWatcherCreator(
return onSourceChange({
sourceDir,
watchFile,
watchIgnored,
artifactsDir,
onChange: () => reloadExtension(sourceDir),
shouldWatchFile: (file) => fileFilter.wantFile(file),
Expand All @@ -274,6 +277,7 @@ export type ReloadStrategyParams = {|
extensionRunner: IExtensionRunner,
sourceDir: string,
watchFile?: string,
watchIgnored?: Array<string>,
artifactsDir: string,
ignoreFiles?: Array<string>,
noInput?: boolean,
Expand All @@ -293,6 +297,7 @@ export function defaultReloadStrategy(
noInput = false,
sourceDir,
watchFile,
watchIgnored,
}: ReloadStrategyParams,
{
createWatcher = defaultWatcherCreator,
Expand All @@ -311,6 +316,7 @@ export function defaultReloadStrategy(
},
sourceDir,
watchFile,
watchIgnored,
artifactsDir,
ignoreFiles,
});
Expand Down
11 changes: 10 additions & 1 deletion src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,20 @@ Example: $0 --help run.
},
'watch-file': {
describe: 'Reload the extension only when the contents of this' +
'file changes. This is useful if you use a custom' +
' file changes. This is useful if you use a custom' +
' build process for your extension',
demandOption: false,
type: 'string',
},
'watch-ignored': {
describe: 'Paths and globs patterns that should not be ' +
'watched for changes. This is useful if you want ' +
'to explicitly prevent web-ext from watching part ' +
'of the extension directory tree, ' +
'e.g. the node_modules folder.',
demandOption: false,
type: 'array',
},
'pre-install': {
describe: 'Pre-install the extension into the profile before ' +
'startup. This is only needed to support older versions ' +
Expand Down
10 changes: 8 additions & 2 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export type OnChangeFn = () => any;
export type OnSourceChangeParams = {|
sourceDir: string,
watchFile?: string,
watchIgnored?: Array<string>,
artifactsDir: string,
onChange: OnChangeFn,
shouldWatchFile: ShouldWatchFn,
debounceTime?: number,
|};

// NOTE: this fix an issue with flow and default exports (which currently
Expand All @@ -36,16 +38,20 @@ export default function onSourceChange(
{
sourceDir,
watchFile,
watchIgnored,
artifactsDir,
onChange,
shouldWatchFile,
debounceTime = 1000,
}: OnSourceChangeParams
): Watchpack {
// TODO: For network disks, we would need to add {poll: true}.
const watcher = new Watchpack();
const watcher = watchIgnored ?
new Watchpack({ignored: watchIgnored}) :
new Watchpack();

const executeImmediately = true;
onChange = debounce(onChange, 1000, executeImmediately);
onChange = debounce(onChange, debounceTime, executeImmediately);

watcher.on('change', (filePath) => {
proxyFileChanges({artifactsDir, onChange, filePath, shouldWatchFile});
Expand Down
12 changes: 11 additions & 1 deletion tests/functional/test.cli.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ const EXPECTED_MESSAGE = 'Fake Firefox binary executed correctly.';

describe('web-ext run', () => {

it('accepts: --no-reload --watch-file --source-dir SRCDIR --firefox FXPATH',
it('accepts: --no-reload --watch-file --source-dir SRCDIR ' +
'--firefox FXPATH --watch-ignored',
() => withTempAddonDir(
{addonPath: minimalAddonPath},
(srcDir) => {
const watchedFile = path.join(srcDir, 'watchedFile.txt');
const watchIgnoredArr = ['ignoredFile1.txt', 'ignoredFile2.txt'].map(
(file) => path.join(srcDir, file)
);
const watchIgnoredFile = path.join(srcDir, 'ignoredFile3.txt');

fs.writeFileSync(watchedFile, '');
watchIgnoredArr.forEach((file) => fs.writeFileSync(file, ''));
fs.writeFileSync(watchIgnoredFile, '');

const argv = [
'run', '--verbose', '--no-reload',
'--source-dir', srcDir,
'--watch-file', watchedFile,
'--firefox', fakeFirefoxPath,
'--watch-ignored', ...watchIgnoredArr,
'--watch-ignored', watchIgnoredFile,
];
const spawnOptions = {
env: {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test-extension-runners/test.extension-runners.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('util/extension-runners', () => {
sourceDir: '/path/to/extension/source/',
artifactsDir: '/path/to/web-ext-artifacts',
watchFile: '/path/to/watched/file',
watchIgnored: '/path/to/ignored/file',
onSourceChange: sinon.spy(() => {}),
ignoreFiles: ['path/to/file', 'path/to/file2'],
reloadExtension: sinon.spy(() => Promise.resolve()),
Expand All @@ -321,6 +322,7 @@ describe('util/extension-runners', () => {
sinon.match({
sourceDir: config.sourceDir,
watchFile: config.watchFile,
watchIgnored: config.watchIgnored,
artifactsDir: config.artifactsDir,
onChange: sinon.match.typeOf('function'),
})
Expand Down Expand Up @@ -391,6 +393,7 @@ describe('util/extension-runners', () => {
extensionRunner,
sourceDir: '/path/to/extension/source',
watchFile: '/path/to/watched/file',
watchIgnored: '/path/to/ignored/file',
artifactsDir: '/path/to/web-ext-artifacts/',
ignoreFiles: ['first/file', 'second/file'],
};
Expand Down Expand Up @@ -425,6 +428,7 @@ describe('util/extension-runners', () => {
sinon.match({
sourceDir: sentArgs.sourceDir,
watchFile: sentArgs.watchFile,
watchIgnored: sentArgs.watchIgnored,
artifactsDir: sentArgs.artifactsDir,
ignoreFiles: sentArgs.ignoreFiles,
})
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test.program.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,32 @@ describe('program.main', () => {
});
});

async function testWatchIgnoredOption(watchIgnored) {
const fakeCommands = fake(commands, {
run: () => Promise.resolve(),
});

await execProgram(
['run', '--watch-ignored', ...watchIgnored],
{commands: fakeCommands});

sinon.assert.calledWithMatch(
execProgram,
fakeCommands.run,
{watchIgnored}
);
}

it('calls run with a single watchIgnored pattern', () => {
testWatchIgnoredOption(['path/to/fake/file1.txt']);
});

it('calls run with a multiple watchIgnored patterns', () => {
testWatchIgnoredOption(
['path/to/fake/file1.txt', 'path/to/fake/pattern*']
);
});

it('converts custom preferences into an object', () => {
const fakeCommands = fake(commands, {
run: () => Promise.resolve(),
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/test.watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {it, describe} from 'mocha';
import {fs} from 'mz';
import sinon from 'sinon';
import {assert} from 'chai';
import Watchpack from 'watchpack';

import {default as onSourceChange, proxyFileChanges} from '../../src/watcher';
import {withTempDir} from '../../src/util/temp-dir';
Expand Down Expand Up @@ -206,4 +207,58 @@ describe('watcher', () => {

});

describe('--watch-ignored is passed in', () => {
it('does not change if ignored file is touched', () =>
withTempDir(async (tmpDir) => {
const debounceTime = 10;
const onChange = sinon.spy();
const tmpPath = tmpDir.path();
const files = ['foo.txt', 'bar.txt', 'foobar.txt'].map(
(filePath) => path.join(tmpPath, filePath)
);

const watcher = onSourceChange({
sourceDir: tmpPath,
artifactsDir: path.join(tmpPath, 'web-ext-artifacts'),
onChange,
watchIgnored: ['foo.txt'].map(
(filePath) => path.join(tmpPath, filePath)
),
shouldWatchFile: (filePath) => filePath !== tmpPath,
debounceTime,
});

const watchAll = new Watchpack();
watchAll.watch(files, [], 0);

async function waitDebounce() {
await new Promise((resolve) => setTimeout(resolve, debounceTime * 2));
}

async function assertOnChange(filePath, expectedCallCount) {
const promiseOnChanged = new Promise((resolve) => watchAll.once(
'change',
(f) => resolve(f))
);
await waitDebounce();
await fs.writeFile(filePath, '<content>');
assert.equal(filePath, await promiseOnChanged);
await waitDebounce();
sinon.assert.callCount(onChange, expectedCallCount);
}

// Verify foo.txt is being ignored.
await assertOnChange(files[0], 0);

// Verify that the other two files are not be ignored.
await assertOnChange(files[1], 1);
await assertOnChange(files[2], 2);

watcher.close();
watchAll.close();
// Leave watcher.close some time to complete its cleanup before withTempDir will remove the
// test directory.
await waitDebounce();
}));
});
});