Skip to content

Commit

Permalink
fix(@clack/prompts): clear spinner hooks on spinner.stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mist3rBru committed Aug 27, 2023
1 parent 24a3f4a commit 50ed94a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-seas-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/prompts': patch
---

fix: clear `spinner` hooks on `spinner.stop`
43 changes: 29 additions & 14 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,41 @@ export const spinner = () => {
let isSpinnerActive: boolean = false;
let _message: string = '';

const handleExit = (code: number) => {
const msg = code > 1 ? 'Something went wrong' : 'Canceled';
if (isSpinnerActive) stop(msg, code);
};

const errorEventHandler = () => handleExit(2);
const signalEventHandler = () => handleExit(1);

const registerHooks = () => {
// Reference: https://nodejs.org/api/process.html#event-uncaughtexception
process.on('uncaughtExceptionMonitor', errorEventHandler);
// Reference: https://nodejs.org/api/process.html#event-unhandledrejection
process.on('unhandledRejection', errorEventHandler);
// Reference Signal Events: https://nodejs.org/api/process.html#signal-events
process.on('SIGINT', signalEventHandler);
process.on('SIGTERM', signalEventHandler);
process.on('exit', handleExit);
};

const clearHooks = () => {
process.removeListener('uncaughtExceptionMonitor', errorEventHandler);
process.removeListener('unhandledRejection', errorEventHandler);
process.removeListener('SIGINT', signalEventHandler);
process.removeListener('SIGTERM', signalEventHandler);
process.removeListener('exit', handleExit);
};

const start = (msg: string = ''): void => {
isSpinnerActive = true;
unblock = block();
_message = msg.replace(/\.+$/, '');
process.stdout.write(`${color.gray(S_BAR)}\n`);
let frameIndex = 0;
let dotsTimer = 0;
registerHooks()
loop = setInterval(() => {
const frame = color.magenta(frames[frameIndex]);
const loadingDots = '.'.repeat(Math.floor(dotsTimer)).slice(0, 3);
Expand All @@ -667,27 +695,14 @@ export const spinner = () => {
process.stdout.write(cursor.move(-999, 0));
process.stdout.write(erase.down(1));
process.stdout.write(`${step} ${_message}\n`);
clearHooks()
unblock();
};

const message = (msg: string = ''): void => {
_message = msg ?? _message;
};

const handleExit = (code: number) => {
const msg = code > 1 ? 'Something went wrong' : 'Canceled';
if (isSpinnerActive) stop(msg, code);
};

// Reference: https://nodejs.org/api/process.html#event-uncaughtexception
process.on('uncaughtExceptionMonitor', () => handleExit(2));
// Reference: https://nodejs.org/api/process.html#event-unhandledrejection
process.on('unhandledRejection', () => handleExit(2));
// Reference Signal Events: https://nodejs.org/api/process.html#signal-events
process.on('SIGINT', () => handleExit(1));
process.on('SIGTERM', () => handleExit(1));
process.on('exit', handleExit);

return {
start,
stop,
Expand Down

0 comments on commit 50ed94a

Please sign in to comment.