-
Notifications
You must be signed in to change notification settings - Fork 40
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
[cli] Add new options to watcher #173
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,37 +102,69 @@ program | |
.description("Generate TypeScript types from XState machines") | ||
.argument("<files>", "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<Promise<void>> = []; | ||
// 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any specific reason to prefer this in certain situations? |
||
) | ||
.option( | ||
"-i, --ignored <patterns...>", | ||
"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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one has been added, right? could you elaborate on why you think it's needed? what kind of issues this can help us avoid? |
||
awaitWriteFinish: shouldCoalesceWriteEvents | ||
? { | ||
stabilityThreshold: 50, | ||
pollInterval: pollInterval, | ||
} | ||
: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previously we were using |
||
}) | ||
.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<Promise<void>> = []; | ||
// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are those
-p
and-i
shortcuts matching the Tailwind CLI? Personally, I would not introduce those as we might add some other options in the future and they might be better suited for having those shortcuts (this feels especial;y true about-p
)