Skip to content

Commit 50ed94a

Browse files
committed
fix(@clack/prompts): clear spinner hooks on spinner.stop
1 parent 24a3f4a commit 50ed94a

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

.changeset/red-seas-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/prompts': patch
3+
---
4+
5+
fix: clear `spinner` hooks on `spinner.stop`

packages/prompts/src/index.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,41 @@ export const spinner = () => {
636636
let isSpinnerActive: boolean = false;
637637
let _message: string = '';
638638

639+
const handleExit = (code: number) => {
640+
const msg = code > 1 ? 'Something went wrong' : 'Canceled';
641+
if (isSpinnerActive) stop(msg, code);
642+
};
643+
644+
const errorEventHandler = () => handleExit(2);
645+
const signalEventHandler = () => handleExit(1);
646+
647+
const registerHooks = () => {
648+
// Reference: https://nodejs.org/api/process.html#event-uncaughtexception
649+
process.on('uncaughtExceptionMonitor', errorEventHandler);
650+
// Reference: https://nodejs.org/api/process.html#event-unhandledrejection
651+
process.on('unhandledRejection', errorEventHandler);
652+
// Reference Signal Events: https://nodejs.org/api/process.html#signal-events
653+
process.on('SIGINT', signalEventHandler);
654+
process.on('SIGTERM', signalEventHandler);
655+
process.on('exit', handleExit);
656+
};
657+
658+
const clearHooks = () => {
659+
process.removeListener('uncaughtExceptionMonitor', errorEventHandler);
660+
process.removeListener('unhandledRejection', errorEventHandler);
661+
process.removeListener('SIGINT', signalEventHandler);
662+
process.removeListener('SIGTERM', signalEventHandler);
663+
process.removeListener('exit', handleExit);
664+
};
665+
639666
const start = (msg: string = ''): void => {
640667
isSpinnerActive = true;
641668
unblock = block();
642669
_message = msg.replace(/\.+$/, '');
643670
process.stdout.write(`${color.gray(S_BAR)}\n`);
644671
let frameIndex = 0;
645672
let dotsTimer = 0;
673+
registerHooks()
646674
loop = setInterval(() => {
647675
const frame = color.magenta(frames[frameIndex]);
648676
const loadingDots = '.'.repeat(Math.floor(dotsTimer)).slice(0, 3);
@@ -667,27 +695,14 @@ export const spinner = () => {
667695
process.stdout.write(cursor.move(-999, 0));
668696
process.stdout.write(erase.down(1));
669697
process.stdout.write(`${step} ${_message}\n`);
698+
clearHooks()
670699
unblock();
671700
};
672701

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

677-
const handleExit = (code: number) => {
678-
const msg = code > 1 ? 'Something went wrong' : 'Canceled';
679-
if (isSpinnerActive) stop(msg, code);
680-
};
681-
682-
// Reference: https://nodejs.org/api/process.html#event-uncaughtexception
683-
process.on('uncaughtExceptionMonitor', () => handleExit(2));
684-
// Reference: https://nodejs.org/api/process.html#event-unhandledrejection
685-
process.on('unhandledRejection', () => handleExit(2));
686-
// Reference Signal Events: https://nodejs.org/api/process.html#signal-events
687-
process.on('SIGINT', () => handleExit(1));
688-
process.on('SIGTERM', () => handleExit(1));
689-
process.on('exit', handleExit);
690-
691706
return {
692707
start,
693708
stop,

0 commit comments

Comments
 (0)