chore: Recover dev server after dependencies change (#14211)

* chore: Restart dev server after yarn install and report missing dependencies

* fix: Restart dev server from Yarn install state after postinstall
This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-21 18:19:01 -04:00
1 parent 89af21166b
commit 7f4b67cd7a
1 file changed
+31
+31
View File
@@ -7,6 +7,7 @@ const {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
rmSync,
statSync,
watch,
@@ -112,6 +113,19 @@ function getDest(file) {
return undefined;
}
/**
* The dependencies in package.json that are not installed.
* @return {string[]} the names of the missing packages.
*/
function getMissingDependencies() {
const { dependencies = {} } = JSON.parse(
readFileSync("package.json", "utf8")
);
return Object.keys(dependencies).filter(
(name) => !existsSync(path.join("node_modules", name, "package.json"))
);
}
/**
* Builds, then watches the source directories and updates the output of each
* changed, added or removed file. A full build runs again after a failed one.
@@ -129,6 +143,14 @@ async function watchSources(command) {
let isBuildPending = false;
const startServer = () => {
const missing = getMissingDependencies();
if (missing.length) {
console.log(
`Missing dependencies: ${missing.join(", ")}. Run "yarn install", the server starts when it completes.`
);
return;
}
// Run in a new process group so the forked service processes are stopped
// together with the master.
child = spawn(command[0], command.slice(1), {
@@ -276,6 +298,15 @@ async function watchSources(command) {
}
});
// Yarn writes the install state after the postinstall script, so a restart
// here picks up the dependencies of a newly checked out branch.
mkdirSync(".yarn", { recursive: true });
watch(".yarn", (_event, filename) => {
if (filename === "install-state.gz") {
restartServer();
}
});
for (const signal of ["SIGINT", "SIGTERM"]) {
process.once(signal, () => stopServer(() => process.exit(0)));
}