diff --git a/packages/nx/bin/nx.ts b/packages/nx/bin/nx.ts index ccb02f62730d0..0fb90814683ca 100644 --- a/packages/nx/bin/nx.ts +++ b/packages/nx/bin/nx.ts @@ -21,6 +21,7 @@ import { performance } from 'perf_hooks'; import { setupWorkspaceContext } from '../src/utils/workspace-context'; import { daemonClient } from '../src/daemon/client/client'; import { removeDbConnections } from '../src/utils/db-connection'; +import { signalToCode } from '../src/utils/exit-codes'; function main() { if ( @@ -275,12 +276,26 @@ const getLatestVersionOfNx = ((fn: () => string) => { return () => cache || (cache = fn()); })(_getLatestVersionOfNx); -function nxCleanup() { +function nxCleanup(signal?: NodeJS.Signals) { removeDbConnections(); + if (signal) { + process.exit(signalToCode(signal)); + } else { + process.exit(); + } } -process.on('exit', nxCleanup); -process.on('SIGINT', nxCleanup); -process.on('SIGTERM', nxCleanup); -process.on('SIGHUP', nxCleanup); + +process.on('exit', () => { + nxCleanup(); +}); +process.on('SIGINT', () => { + nxCleanup('SIGINT'); +}); +process.on('SIGTERM', () => { + nxCleanup('SIGTERM'); +}); +process.on('SIGHUP', () => { + nxCleanup('SIGHUP'); +}); main();