Skip to content

Commit

Permalink
fix(js): skip watcher if --watch=false for node executor (#17887)
Browse files Browse the repository at this point in the history
(cherry picked from commit b7d283b)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Jun 30, 2023
1 parent d471af7 commit 60e6c36
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
22 changes: 17 additions & 5 deletions e2e/node/src/node-esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import {
checkFilesExist,
cleanupProject,
newProject,
promisifiedTreeKill,
readFile,
runCLI,
runCLIAsync,
runCommandUntil,
tmpProjPath,
uniq,
updateFile,
Expand All @@ -24,12 +27,21 @@ describe('Node Applications + esbuild', () => {
checkFilesDoNotExist(`apps/${app}/webpack.config.js`);

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`);
const result = execSync(`node dist/apps/${app}/main.js`, {
cwd: tmpProjPath(),
}).toString();
expect(result).toMatch(/Hello World!/);

// Check that updating the file won't trigger a rebuild since --watch=false.
updateFile(`apps/${app}/src/main.ts`, `console.log('Bye1');`);
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);
});
76 changes: 39 additions & 37 deletions packages/js/src/executors/node/node.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,46 +213,48 @@ export async function* nodeExecutor(
tasks.push(task);
};

const stopWatch = await daemonClient.registerFileWatcher(
{
watchProjects: [context.projectName],
includeDependentProjects: true,
},
async (err, data) => {
if (err === 'closed') {
logger.error(`Watch error: Daemon closed the connection`);
process.exit(1);
} else if (err) {
logger.error(`Watch error: ${err?.message ?? 'Unknown'}`);
} else {
logger.info(`NX File change detected. Restarting...`);
await addToQueue();
await debouncedProcessQueue();
if (options.watch) {
const stopWatch = await daemonClient.registerFileWatcher(
{
watchProjects: [context.projectName],
includeDependentProjects: true,
},
async (err, data) => {
if (err === 'closed') {
logger.error(`Watch error: Daemon closed the connection`);
process.exit(1);
} else if (err) {
logger.error(`Watch error: ${err?.message ?? 'Unknown'}`);
} else {
logger.info(`NX File change detected. Restarting...`);
await addToQueue();
await debouncedProcessQueue();
}
}
}
);
);

const stopAllTasks = (signal: NodeJS.Signals = 'SIGTERM') => {
for (const task of tasks) {
task.stop(signal);
}
};
const stopAllTasks = (signal: NodeJS.Signals = 'SIGTERM') => {
for (const task of tasks) {
task.stop(signal);
}
};

process.on('SIGTERM', async () => {
stopWatch();
stopAllTasks('SIGTERM');
process.exit(128 + 15);
});
process.on('SIGINT', async () => {
stopWatch();
stopAllTasks('SIGINT');
process.exit(128 + 2);
});
process.on('SIGHUP', async () => {
stopWatch();
stopAllTasks('SIGHUP');
process.exit(128 + 1);
});
process.on('SIGTERM', async () => {
stopWatch();
stopAllTasks('SIGTERM');
process.exit(128 + 15);
});
process.on('SIGINT', async () => {
stopWatch();
stopAllTasks('SIGINT');
process.exit(128 + 2);
});
process.on('SIGHUP', async () => {
stopWatch();
stopAllTasks('SIGHUP');
process.exit(128 + 1);
});
}

await addToQueue();
await processQueue();
Expand Down

0 comments on commit 60e6c36

Please sign in to comment.