From 14ae957b503c28b0245eb4bf52220f524210b657 Mon Sep 17 00:00:00 2001 From: Rafau Date: Thu, 30 Jun 2022 22:13:30 +0200 Subject: [PATCH] Add new options to watcher - add `--poll` / `-p` option for polling - add `--ignored` / `-i` option for ignoring specified paths - add some default options for watcher based on config used in `tailwindcss` --- apps/cli/src/bin.ts | 86 +++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/apps/cli/src/bin.ts b/apps/cli/src/bin.ts index 2ca1a589..232f76b3 100644 --- a/apps/cli/src/bin.ts +++ b/apps/cli/src/bin.ts @@ -102,37 +102,69 @@ program .description("Generate TypeScript types from XState machines") .argument("", "The files to target, expressed as a glob pattern") .option("-w, --watch", "Run the typegen in watch mode") - .action(async (filesPattern: string, opts: { watch?: boolean }) => { - if (opts.watch) { - // TODO: implement per path queuing to avoid tasks related to the same file from overlapping their execution - const processFile = (path: string) => { - if (path.endsWith(".typegen.ts")) { - return; - } - writeToFiles([path]).catch(() => {}); - }; - // TODO: handle removals - watch(filesPattern, { awaitWriteFinish: true }) - .on("add", processFile) - .on("change", processFile); - } else { - const tasks: Array> = []; - // TODO: could this cleanup outdated typegen files? - watch(filesPattern, { persistent: false }) - .on("add", (path) => { + .option( + "-p, --poll", + "Use polling instead of filesystem events when watching" + ) + .option( + "-i, --ignored ", + "Defines files/paths to be ignored (anymatch-compatible definition)" + ) + .action( + async ( + filesPattern: string, + opts: { watch?: boolean; poll?: boolean; ignored?: string[] } + ) => { + const ignored = opts.ignored ?? undefined; + const shouldPoll = opts.poll ?? false; + const pollInterval = 10; + const shouldCoalesceWriteEvents = + shouldPoll || process.platform === "win32"; + + if (opts.watch) { + // TODO: implement per path queuing to avoid tasks related to the same file from overlapping their execution + const processFile = (path: string) => { if (path.endsWith(".typegen.ts")) { return; } - tasks.push(writeToFiles([path])); + writeToFiles([path]).catch(() => {}); + }; + + // TODO: handle removals + watch(filesPattern, { + ignored, + usePolling: shouldPoll, + interval: shouldPoll ? pollInterval : undefined, + ignoreInitial: true, + awaitWriteFinish: shouldCoalesceWriteEvents + ? { + stabilityThreshold: 50, + pollInterval: pollInterval, + } + : false, }) - .on("ready", async () => { - const settled = await allSettled(tasks); - if (settled.some((result) => result.status === "rejected")) { - process.exit(1); - } - process.exit(0); - }); + .on("add", processFile) + .on("change", processFile); + } else { + const tasks: Array> = []; + // TODO: could this cleanup outdated typegen files? + watch(filesPattern, { persistent: false, ignored }) + .on("add", (path) => { + // console.log(`${path} - processing`); + if (path.endsWith(".typegen.ts")) { + return; + } + tasks.push(writeToFiles([path])); + }) + .on("ready", async () => { + const settled = await allSettled(tasks); + if (settled.some((result) => result.status === "rejected")) { + process.exit(1); + } + process.exit(0); + }); + } } - }); + ); program.parse(process.argv);