Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 59 additions & 27 deletions apps/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Copy link
Member

@Andarist Andarist Jul 5, 2022

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)

"Use polling instead of filesystem events when watching"
Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously we were using true here at all times - any particular reason why this has been changed?

})
.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);