fix(js): skip watcher if --watch=false for node executor (#17887)

This commit is contained in:
Jack Hsu 2023-06-30 15:10:24 -04:00 committed by GitHub
parent 0608318449
commit b7d283bced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 42 deletions

View File

@ -3,8 +3,11 @@ import {
checkFilesExist, checkFilesExist,
cleanupProject, cleanupProject,
newProject, newProject,
promisifiedTreeKill,
readFile,
runCLI, runCLI,
runCLIAsync, runCLIAsync,
runCommandUntil,
tmpProjPath, tmpProjPath,
uniq, uniq,
updateFile, updateFile,
@ -24,12 +27,21 @@ describe('Node Applications + esbuild', () => {
checkFilesDoNotExist(`apps/${app}/webpack.config.js`); checkFilesDoNotExist(`apps/${app}/webpack.config.js`);
updateFile(`apps/${app}/src/main.ts`, `console.log('Hello World!');`); updateFile(`apps/${app}/src/main.ts`, `console.log('Hello World!');`);
await runCLIAsync(`build ${app}`);
const p = await runCommandUntil(`serve ${app} --watch=false`, (output) => {
process.stdout.write(output);
return output.includes(`Hello World!`);
});
checkFilesExist(`dist/apps/${app}/main.js`); checkFilesExist(`dist/apps/${app}/main.js`);
const result = execSync(`node dist/apps/${app}/main.js`, {
cwd: tmpProjPath(), // Check that updating the file won't trigger a rebuild since --watch=false.
}).toString(); updateFile(`apps/${app}/src/main.ts`, `console.log('Bye1');`);
expect(result).toMatch(/Hello World!/); await new Promise((res) => setTimeout(res, 2000));
expect(readFile(`dist/apps/${app}/apps/${app}/src/main.js`)).not.toContain(
`Bye!`
);
await promisifiedTreeKill(p.pid, 'SIGKILL');
}, 300_000); }, 300_000);
}); });

View File

@ -213,46 +213,48 @@ export async function* nodeExecutor(
tasks.push(task); tasks.push(task);
}; };
const stopWatch = await daemonClient.registerFileWatcher( if (options.watch) {
{ const stopWatch = await daemonClient.registerFileWatcher(
watchProjects: [context.projectName], {
includeDependentProjects: true, watchProjects: [context.projectName],
}, includeDependentProjects: true,
async (err, data) => { },
if (err === 'closed') { async (err, data) => {
logger.error(`Watch error: Daemon closed the connection`); if (err === 'closed') {
process.exit(1); logger.error(`Watch error: Daemon closed the connection`);
} else if (err) { process.exit(1);
logger.error(`Watch error: ${err?.message ?? 'Unknown'}`); } else if (err) {
} else { logger.error(`Watch error: ${err?.message ?? 'Unknown'}`);
logger.info(`NX File change detected. Restarting...`); } else {
await addToQueue(); logger.info(`NX File change detected. Restarting...`);
await debouncedProcessQueue(); await addToQueue();
await debouncedProcessQueue();
}
} }
} );
);
const stopAllTasks = (signal: NodeJS.Signals = 'SIGTERM') => { const stopAllTasks = (signal: NodeJS.Signals = 'SIGTERM') => {
for (const task of tasks) { for (const task of tasks) {
task.stop(signal); task.stop(signal);
} }
}; };
process.on('SIGTERM', async () => { process.on('SIGTERM', async () => {
stopWatch(); stopWatch();
stopAllTasks('SIGTERM'); stopAllTasks('SIGTERM');
process.exit(128 + 15); process.exit(128 + 15);
}); });
process.on('SIGINT', async () => { process.on('SIGINT', async () => {
stopWatch(); stopWatch();
stopAllTasks('SIGINT'); stopAllTasks('SIGINT');
process.exit(128 + 2); process.exit(128 + 2);
}); });
process.on('SIGHUP', async () => { process.on('SIGHUP', async () => {
stopWatch(); stopWatch();
stopAllTasks('SIGHUP'); stopAllTasks('SIGHUP');
process.exit(128 + 1); process.exit(128 + 1);
}); });
}
await addToQueue(); await addToQueue();
await processQueue(); await processQueue();