Skip to content

Commit eb0bbc1

Browse files
authored
fix(watch): use a better debounce option name for the watch (#476)
* fix(watch): use a better debounce option name for the watch
1 parent 79ab643 commit eb0bbc1

File tree

7 files changed

+27
-33
lines changed

7 files changed

+27
-33
lines changed

packages/cli/schemas/lerna-schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@
13861386
}
13871387
},
13881388
"watch": {
1389-
"emitChangesDelay": {
1389+
"debounce": {
13901390
"type": "number",
13911391
"description": "Defaults to 200, time to wait in milliseconds before emitting all the file changes into a single event."
13921392
},

packages/cli/src/cli-commands/cli-watch-commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
hidden: true,
3535
type: 'boolean',
3636
},
37-
'emit-changes-delay': {
37+
debounce: {
3838
group: 'Command Options:',
3939
describe:
4040
'Time to wait in milliseconds before emitting all the file changes into a single event, defaults to 200',

packages/core/src/models/command-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ export interface WatchCommandOption {
432432
* This provide enough time for the system to collect all Chokidar events (1x per file changes)
433433
* and merge them into a single Lerna watch change event to be emitted (Lerna will join all file paths into a CSV string separated by whitespace by default).
434434
*/
435-
emitChangesDelay?: number;
435+
debounce?: number;
436436

437437
/** Defaults to whitespace, the delimiter that will be used to separate files when mutiple file changes are emitted by the watch */
438438
fileDelimiter?: string;

packages/watch/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ lerna watch
2525

2626
```sh
2727
$ lerna watch -- <command>
28+
# you can press "x" to exit the watch mode.
2829
```
2930

3031
The values `$LERNA_PACKAGE_NAME` and `$LERNA_FILE_CHANGES` will be replaced with the package name, the file that changed respectively. If multiple file changes are detected, they will all be listed and separated by a whitespace (unless custom file delimiter are provided).
@@ -106,7 +107,7 @@ $ npx -c 'lerna watch -- echo \$LERNA_PACKAGE_NAME \$LERNA_FILE_CHANGES'
106107
- [`@lerna/watch`](#lernawatch)
107108
- [Usage](#usage)
108109
- [Options](#options)
109-
- [`--emit-changes-delay`](#--emit-changes-delay)
110+
- [`--debounce`](#--debounce)
110111
- [`--file-delimiter`](#--file-delimiter)
111112
- [`--glob`](#--glob)
112113
- [`--stream`](#--stream)
@@ -128,11 +129,11 @@ $ npx -c 'lerna watch -- echo \$LERNA_PACKAGE_NAME \$LERNA_FILE_CHANGES'
128129

129130
> **Note** to limit the number of files being watched, you might want to take a look at either [`--ignored`](#--ignored) and/or [`--glob`](#--glob) options. The `lerna watch` command skips `.git/`, `dist/` and `node_modules/` directories by default.
130131
131-
### `--emit-changes-delay`
132-
Defaults to `200`, time to wait in milliseconds before collecting all file changes before emitting them into a single watch event. This option exists because we want to provide enough time for `lerna watch` to collect all file changes (within that period) and merge these file paths into a single watch change event (chokidar has no grouping feature and emits an event for every file that changed) and we want to avoid emitting too many events (especially for a watch that triggers a rebuild). This option will come into play when you make a code change that triggers hundred of file changes, you might need to adjust the delay by increasing its value (for comparison sake the `Nx` library have their `Nx Watch` set, and fixed, at `500`).
132+
### `--debounce`
133+
Defaults to `200` time to wait in milliseconds before collecting all file changes before emitting them into a single watch event. Basically this option is to provide enough time for `lerna watch` to collect all files that changed (within that period) and avoid emitting too many watch events since Chokidar has no such debounce feature. This option becomes quite important when you do code change that affects hundred of file changes at the same time, the default is 200 but you might need to adjust the delay by increasing its value (in comparison, many libraries use `500` debounce for a watch).
133134

134135
```sh
135-
$ lerna watch --emit-changes-delay=500 -- <command>
136+
$ lerna watch --debounce=500 -- <command>
136137
```
137138

138139
### `--file-delimiter`

packages/watch/src/__tests__/watch-command.spec.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Watch Command', () => {
154154
});
155155

156156
it('should take glob input option, without slash prefix, and expect it to be appended to the file path being watch by chokidar', async () => {
157-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--glob', 'src/**/*.{ts,tsx}', '--', 'lerna run build');
157+
await lernaWatch(testDir)('--debounce', '0', '--glob', 'src/**/*.{ts,tsx}', '--', 'lerna run build');
158158

159159
expect(watchMock).toHaveBeenCalledWith(
160160
[
@@ -171,7 +171,7 @@ describe('Watch Command', () => {
171171
});
172172

173173
it('should take glob input option, with slash prefix, and expect same appended to the file path being watch by chokidar', async () => {
174-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--glob', '/src/**/*.{ts,tsx}', '--', 'lerna run build');
174+
await lernaWatch(testDir)('--debounce', '0', '--glob', '/src/**/*.{ts,tsx}', '--', 'lerna run build');
175175

176176
expect(watchMock).toHaveBeenCalledWith(
177177
[
@@ -188,7 +188,7 @@ describe('Watch Command', () => {
188188
});
189189

190190
it('should be able to take --await-write-finish options as a boolean', async () => {
191-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--await-write-finish', '--', 'lerna run build');
191+
await lernaWatch(testDir)('--debounce', '0', '--await-write-finish', '--', 'lerna run build');
192192

193193
expect(watchMock).toHaveBeenCalledWith(
194194
[path.join(testDir, 'packages/package-1'), path.join(testDir, 'packages/package-2')],
@@ -203,7 +203,7 @@ describe('Watch Command', () => {
203203
});
204204

205205
it('should take options prefixed with "awf" (awfPollInterval) and transform them into a valid chokidar "awaitWriteFinish" option', async () => {
206-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--awf-poll-interval', '500', '--', 'lerna run build');
206+
await lernaWatch(testDir)('--debounce', '0', '--awf-poll-interval', '500', '--', 'lerna run build');
207207

208208
expect(watchMock).toHaveBeenCalledWith(
209209
[path.join(testDir, 'packages/package-1'), path.join(testDir, 'packages/package-2')],
@@ -218,14 +218,7 @@ describe('Watch Command', () => {
218218
});
219219

220220
it('should take options prefixed with "awf" (awfStabilityThreshold) and transform them into a valid chokidar "awaitWriteFinish" option', async () => {
221-
await lernaWatch(testDir)(
222-
'--emit-changes-delay',
223-
'0',
224-
'--awf-stability-threshold',
225-
'275',
226-
'--',
227-
'lerna run build'
228-
);
221+
await lernaWatch(testDir)('--debounce', '0', '--awf-stability-threshold', '275', '--', 'lerna run build');
229222

230223
expect(watchMock).toHaveBeenCalledWith(
231224
[path.join(testDir, 'packages/package-1'), path.join(testDir, 'packages/package-2')],
@@ -240,7 +233,7 @@ describe('Watch Command', () => {
240233
});
241234

242235
it('should execute change watch callback only in the given scope', async () => {
243-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
236+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
244237
await watchChangeHandler('change', path.join(testDir, 'packages/package-2/some-file.ts'));
245238

246239
expect(calledInPackages()).toEqual(['package-2']);
@@ -262,7 +255,7 @@ describe('Watch Command', () => {
262255

263256
it('should execute change watch callback with --stream in the given scope', async () => {
264257
await lernaWatch(testDir)(
265-
'--emit-changes-delay',
258+
'--debounce',
266259
'0',
267260
'--scope',
268261
'package-2',
@@ -297,7 +290,7 @@ describe('Watch Command', () => {
297290
});
298291

299292
it('should execute change watch callback with default whitespace file delimiter', async () => {
300-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--', 'echo $LERNA_PACKAGE_NAME $LERNA_FILE_CHANGES');
293+
await lernaWatch(testDir)('--debounce', '0', '--', 'echo $LERNA_PACKAGE_NAME $LERNA_FILE_CHANGES');
301294
watchChangeHandler('change', path.join(testDir, 'packages/package-2/file-1.ts'));
302295
await watchChangeHandler('change', path.join(testDir, 'packages/package-2/some-file.ts'));
303296

@@ -323,7 +316,7 @@ describe('Watch Command', () => {
323316

324317
it('should execute change watch callback with custom file delimiter when defined', async () => {
325318
// prettier-ignore
326-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--file-delimiter', ';;', '--', 'echo $LERNA_PACKAGE_NAME $LERNA_FILE_CHANGES');
319+
await lernaWatch(testDir)('--debounce', '0', '--file-delimiter', ';;', '--', 'echo $LERNA_PACKAGE_NAME $LERNA_FILE_CHANGES');
327320
watchChangeHandler('change', path.join(testDir, 'packages/package-2/file-1.ts'));
328321
await watchChangeHandler('change', path.join(testDir, 'packages/package-2/some-file.ts'));
329322

@@ -348,7 +341,7 @@ describe('Watch Command', () => {
348341
});
349342

350343
it('should execute watch add callback only on the given scope', async () => {
351-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
344+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
352345
await watchAddHandler('add', path.join(testDir, 'packages/package-2/some-file.ts'));
353346

354347
expect(calledInPackages()).toEqual(['package-2']);
@@ -369,7 +362,7 @@ describe('Watch Command', () => {
369362
});
370363

371364
it('should execute watch add callback only the given scope', async () => {
372-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
365+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
373366
await watchAddDirHandler('addDir', path.join(testDir, 'packages/package-2/some-folder'));
374367

375368
expect(calledInPackages()).toEqual(['package-2']);
@@ -390,7 +383,7 @@ describe('Watch Command', () => {
390383
});
391384

392385
it('should execute watch add callback only the given scope', async () => {
393-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
386+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
394387
await watchUnlinkHandler('unlink', path.join(testDir, 'packages/package-2/some-file.ts'));
395388

396389
expect(calledInPackages()).toEqual(['package-2']);
@@ -411,7 +404,7 @@ describe('Watch Command', () => {
411404
});
412405

413406
it('should execute watch add callback only the given scope', async () => {
414-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
407+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
415408
await watchUnlinkDirHandler('unlinkDir', path.join(testDir, 'packages/package-2/some-folder'));
416409

417410
expect(calledInPackages()).toEqual(['package-2']);
@@ -432,7 +425,7 @@ describe('Watch Command', () => {
432425
});
433426

434427
it('should execute watch callback only the given scoped package', async () => {
435-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
428+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
436429

437430
watchAddHandler('add', path.join(testDir, 'packages/package-1/my-file.ts'));
438431
watchAddHandler('add', path.join(testDir, 'packages/package-2/new-file-1.ts'));
@@ -462,7 +455,7 @@ describe('Watch Command', () => {
462455

463456
it('should execute watch multiple callbacks that were queued on multiple packages', async () => {
464457
// prettier-ignore
465-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-{1,2}', '--', 'echo $LERNA_PACKAGE_NAME && Promise.resolve(true)');
458+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-{1,2}', '--', 'echo $LERNA_PACKAGE_NAME && Promise.resolve(true)');
466459

467460
watchAddHandler('add', path.join(testDir, 'packages/package-2/new-file-1.ts'));
468461
watchUnlinkHandler('unlink', path.join(testDir, 'packages/package-2/new-file-1.ts'));
@@ -514,7 +507,7 @@ describe('Watch Command', () => {
514507

515508
try {
516509
// prettier-ignore
517-
await lernaWatch(testDir)('--emit-changes-delay', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
510+
await lernaWatch(testDir)('--debounce', '0', '--scope', 'package-2', '--', 'echo $LERNA_PACKAGE_NAME');
518511
const promise = watchAddHandler('add', path.join(testDir, 'packages/package-2/some-file.ts'));
519512
stdin.send('x');
520513
stdin.end();

packages/watch/src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export const CHOKIDAR_AVAILABLE_OPTIONS = [
1919
export const FILE_DELIMITER = ' ';
2020

2121
// threshold to hold before firing a single event with merged files
22-
export const EMIT_CHANGES_DELAY = 200;
22+
export const DEBOUNCE_DELAY = 200;

packages/watch/src/watch-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { FilterOptions, getFilteredPackages } from '@lerna-lite/filter-packages'
1212
import chokidar from 'chokidar';
1313
import path from 'path';
1414

15-
import { CHOKIDAR_AVAILABLE_OPTIONS, EMIT_CHANGES_DELAY, FILE_DELIMITER } from './constants';
15+
import { CHOKIDAR_AVAILABLE_OPTIONS, DEBOUNCE_DELAY, FILE_DELIMITER } from './constants';
1616
import { ChangesStructure } from './models';
1717

1818
export function factory(argv: WatchCommandOption) {
@@ -155,7 +155,7 @@ export class WatchCommand extends Command<WatchCommandOption & FilterOptions> {
155155
}
156156

157157
protected executeCommandCallback() {
158-
const debounceDelay = this.options.emitChangesDelay ?? EMIT_CHANGES_DELAY;
158+
const debounceDelay = this.options.debounce ?? DEBOUNCE_DELAY;
159159

160160
return new Promise((resolve) => {
161161
// once we reached emit change stability threshold, we'll fire events for each packages & events while the file paths array will be merged

0 commit comments

Comments
 (0)