Skip to content

Commit 0e114f2

Browse files
committed
fix: Fix issue with push --watch terminating early (#1045)
1 parent 23c0e88 commit 0e114f2

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/commands/push.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const command = new Command('push')
7777
console.log(msg);
7878
};
7979

80-
const stopWatching = clasp.files.watchLocalFiles(onReady, async paths => {
80+
const stopWatching = await clasp.files.watchLocalFiles(onReady, async paths => {
8181
if (!(await onChange(paths))) {
8282
stopWatching();
8383
}

src/core/files.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ function debounceFileChanges<T>(callback: (files: T[]) => Promise<void> | void,
126126
return function (path: T) {
127127
// Already tracked as changed, ignore
128128
if (collectedPaths.includes(path)) {
129+
debug('Ignoring pending file change for path %s', path);
129130
return;
130131
}
131132

133+
debug('Debouncing change for path %s', path);
132134
collectedPaths.push(path);
133135

134136
clearTimeout(timeoutId);
135137
timeoutId = setTimeout(() => {
138+
debug('Firing debounced file');
136139
callback(collectedPaths);
137140
collectedPaths = [];
138141
}, delayMs);
@@ -226,12 +229,18 @@ export class Files {
226229
const collector = debounceFileChanges(onFilesChanged, 500);
227230

228231
const onChange = async (path: string) => {
232+
debug('Have file changes: %s', path);
229233
collector(path);
230234
};
231235
let matcher: Matcher | undefined;
232236
if (ignorePatterns && ignorePatterns.length) {
233-
matcher = file => {
234-
return micromatch.not([file], ignorePatterns, {dot: true}).length === 0;
237+
matcher = (file, stats) => {
238+
if (!stats?.isFile()) {
239+
return false;
240+
}
241+
file = path.relative(this.options.files.projectRootDir, file);
242+
const ignore = micromatch.not([file], ignorePatterns, {dot: true}).length === 0;
243+
return ignore;
235244
};
236245
}
237246
const watcher = chokidar.watch(this.options.files.contentDir, {
@@ -244,8 +253,13 @@ export class Files {
244253
watcher.on('add', onChange);
245254
watcher.on('change', onChange);
246255
watcher.on('unlink', onChange);
247-
248-
return async () => await watcher.close();
256+
watcher.on('error', err => {
257+
debug('Unexpected error during watch: %O', err);
258+
});
259+
return async () => {
260+
debug('Stopping watch');
261+
await watcher.close();
262+
};
249263
}
250264

251265
async getChangedFiles(): Promise<ProjectFile[]> {

0 commit comments

Comments
 (0)