Skip to content

Commit

Permalink
handle exit on terminal window close
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Nov 14, 2023
1 parent 150e423 commit 79ab97a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/cli/commands/project/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ exports.handler = async options => {

await LocalDev.start();

handleExit(() => LocalDev.stop());
handleExit(({ isSIGHUP }) => LocalDev.stop(!isSIGHUP));
};

exports.builder = yargs => {
Expand Down
27 changes: 16 additions & 11 deletions packages/cli/lib/LocalDevManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,30 @@ class LocalDevManager {
this.compareLocalProjectToDeployed(runnableComponents);
}

async stop() {
SpinniesManager.add('cleanupMessage', {
text: i18n(`${i18nKey}.exitingStart`),
});

async stop(showProgress = true) {
if (showProgress) {
SpinniesManager.add('cleanupMessage', {
text: i18n(`${i18nKey}.exitingStart`),
});
}
await this.stopWatching();

const cleanupSucceeded = await this.devServerCleanup();

if (!cleanupSucceeded) {
SpinniesManager.fail('cleanupMessage', {
text: i18n(`${i18nKey}.exitingFail`),
});
if (showProgress) {
SpinniesManager.fail('cleanupMessage', {
text: i18n(`${i18nKey}.exitingFail`),
});
}
process.exit(EXIT_CODES.ERROR);
}

SpinniesManager.succeed('cleanupMessage', {
text: i18n(`${i18nKey}.exitingSucceed`),
});
if (showProgress) {
SpinniesManager.succeed('cleanupMessage', {
text: i18n(`${i18nKey}.exitingSucceed`),
});
}
process.exit(EXIT_CODES.SUCCESS);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/cli/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const { logger } = require('@hubspot/cli-lib/logger');
const handleExit = callback => {
const terminationSignals = [
'beforeExit',
'SIGINT',
'SIGUSR1',
'SIGUSR2',
'SIGINT', // Terminal trying to interrupt (Ctrl + C)
'SIGUSR1', // Start Debugger User-defined signal 1
'SIGUSR2', // User-defined signal 2
'uncaughtException',
'SIGTERM',
'SIGHUP',
'SIGTERM', // Represents a graceful termination
'SIGHUP', // Parent terminal has been closed
];
let exitInProgress = false;

Expand All @@ -21,7 +21,7 @@ const handleExit = callback => {
// Prevent duplicate exit handling
if (!exitInProgress) {
exitInProgress = true;
await callback();
await callback({ isSIGHUP: signal === 'SIGHUP' });
}
});
});
Expand Down

0 comments on commit 79ab97a

Please sign in to comment.