Skip to content

Commit

Permalink
fix: cleanup after sigint in dev process
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyyaaa committed Sep 25, 2024
1 parent 345a92b commit c707fde
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/compiler/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { command as execa } from "execa";
(async () => {
let unsub: (() => void) | undefined;

// Add this block
process.on("SIGINT", () => {
console.log("\nReceived SIGINT. Cleaning up...");
if (unsub) unsub();
process.exit(0);
});

const tasks = new Listr([
{
title: "Clean output",
Expand Down Expand Up @@ -63,7 +70,9 @@ import { command as execa } from "execa";
const timingLabel = `[t:${t}] Recompile took`;
console.time(timingLabel);

compile(_events, { cancel: () => {} })
const compilationPromise = compile(_events, {
cancel: () => {},
})
.then(() => {
spinner.succeed("Compiled successfully.");
console.timeEnd(timingLabel);
Expand All @@ -76,20 +85,35 @@ import { command as execa } from "execa";
spinner.fail(
`Error watching src/ for changes ${err?.message}.`,
);
return;
}

return () => {
if (compilationPromise) {
compilationPromise.then(() => {
spinner.stop();
});
}
};
},
500,
);

const watchSrc = watcher.subscribe(srcDir, (err, _events) => {
onChange(err, _events);
const cleanup = onChange(err, _events);

return () => {
cleanup?.();
};
});

const watchScaffold = watcher.subscribe(
scaffoldDir,
(err, _events) => {
onChange(err, _events);
const cleanup = onChange(err, _events);

return () => {
cleanup?.();
};
},
);

Expand Down Expand Up @@ -117,6 +141,7 @@ import { command as execa } from "execa";
tasks.run().catch((err: Error) => {
if (unsub) unsub();
console.error(err);
process.exit(1);
});
})();

Expand Down

0 comments on commit c707fde

Please sign in to comment.