Skip to content

Commit

Permalink
feat: enabled watching multiple files via --watch-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Dexterp37 committed Jan 15, 2021
1 parent 9e23a4c commit 949e9fa
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/cmd/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type CmdRunParams = {|
noReload: boolean,
preInstall: boolean,
sourceDir: string,
watchFile?: string,
watchFile?: Array<string>,
watchIgnored?: Array<string>,
startUrl?: Array<string>,
target?: Array<string>,
Expand Down
4 changes: 2 additions & 2 deletions src/extension-runners/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export class MultiExtensionRunner {
export type WatcherCreatorParams = {|
reloadExtension: (string) => void,
sourceDir: string,
watchFile?: string,
watchFile?: Array<string>,
watchIgnored?: Array<string>,
artifactsDir: string,
onSourceChange?: OnSourceChangeFn,
Expand Down Expand Up @@ -276,7 +276,7 @@ export function defaultWatcherCreator(
export type ReloadStrategyParams = {|
extensionRunner: IExtensionRunner,
sourceDir: string,
watchFile?: string,
watchFile?: Array<string>,
watchIgnored?: Array<string>,
artifactsDir: string,
ignoreFiles?: Array<string>,
Expand Down
2 changes: 1 addition & 1 deletion src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ Example: $0 --help run.
' file changes. This is useful if you use a custom' +
' build process for your extension',
demandOption: false,
type: 'string',
type: 'array',
},
'watch-ignored': {
describe: 'Paths and globs patterns that should not be ' +
Expand Down
18 changes: 11 additions & 7 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type OnChangeFn = () => any;

export type OnSourceChangeParams = {|
sourceDir: string,
watchFile?: string,
watchFile?: Array<string>,
watchIgnored?: Array<string>,
artifactsDir: string,
onChange: OnChangeFn,
Expand Down Expand Up @@ -57,18 +57,22 @@ export default function onSourceChange(
proxyFileChanges({artifactsDir, onChange, filePath, shouldWatchFile});
});

log.debug(`Watching for file changes in ${watchFile || sourceDir}`);
log.debug(
`Watching ${watchFile ? watchFile.join(',') : sourceDir} for changes`
);

const watchedDirs = [];
const watchedFiles = [];

if (watchFile) {
if (fs.existsSync(watchFile) && !fs.lstatSync(watchFile).isFile()) {
throw new UsageError('Invalid --watch-file value: ' +
`"${watchFile}" is not a file.`);
}
for (const path of watchFile) {
if (fs.existsSync(path) && !fs.lstatSync(path).isFile()) {
throw new UsageError('Invalid --watch-file value: ' +
`"${path}" is not a file.`);
}

watchedFiles.push(watchFile);
watchedFiles.push(path);
}
} else {
watchedDirs.push(sourceDir);
}
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/test.program.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,22 +543,30 @@ describe('program.main', () => {
});
});

it('calls run with a watched file', () => {
const watchFile = 'path/to/fake/file.txt';

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

return execProgram(
['run', '--watch-file', watchFile],
['run', '--watch-file', ...watchFile],
{commands: fakeCommands})
.then(() => {
sinon.assert.calledWithMatch(
fakeCommands.run,
{watchFile}
);
});
}

it('calls run with a watched file', () => {
testWatchFileOption(['path/to/fake/file.txt']);
});

it('calls run with multiple watched files', () => {
testWatchFileOption(
['path/to/fake/file.txt', 'path/to/fake/file2.txt']
);
});

async function testWatchIgnoredOption(watchIgnored) {
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test.watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {withTempDir} from '../../src/util/temp-dir';
import { makeSureItFails } from './helpers';

type AssertWatchedParams = {
watchFile?: string,
watchFile?: Array<string>,
touchedFile: string,
}

Expand All @@ -28,7 +28,7 @@ describe('watcher', () => {
const someFile = path.join(tmpDir.path(), touchedFile);

if (watchFile) {
watchFile = path.join(tmpDir.path(), watchFile);
watchFile = watchFile.map((f) => path.join(tmpDir.path(), f));
}

var resolveChange;
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('watcher', () => {
watchedDirPath,
tmpDirPath,
} = await watchChange({
watchFile: 'foo.txt',
watchFile: ['foo.txt'],
touchedFile: 'foo.txt',
});

Expand All @@ -132,7 +132,7 @@ describe('watcher', () => {
watchedDirPath,
tmpDirPath,
} = await watchChange({
watchFile: 'bar.txt',
watchFile: ['bar.txt'],
touchedFile: 'foo.txt',
});

Expand All @@ -143,7 +143,7 @@ describe('watcher', () => {

it('throws error if a non-file is passed into --watch-file', () => {
return watchChange({
watchFile: '/',
watchFile: ['/'],
touchedFile: 'foo.txt',
}).then(makeSureItFails()).catch((error) => {
assert.match(
Expand Down

0 comments on commit 949e9fa

Please sign in to comment.