my pkgs monorepo
at main 41 lines 1.0 kB view raw
1let cancelled = false; 2let sigintHandlerRegistered = false; 3 4/** 5 * Register the SIGINT handler (should be called early in the application) 6 */ 7export function registerKillswitch(): void { 8 if (sigintHandlerRegistered) { 9 return; 10 } 11 12 // Flip the killswitch when the user hits CTRL-C 13 process.on('SIGINT', () => { 14 if (cancelled) { 15 // If already cancelled and user presses Ctrl+C again, force exit 16 console.log('\n\nForce quit detected. Exiting immediately...'); 17 process.exit(1); 18 } 19 20 console.log('\n\n⚠️ Ctrl+C detected — stopping after current batch completes...'); 21 console.log('Press Ctrl+C again to force quit immediately.\n'); 22 cancelled = true; 23 }); 24 25 sigintHandlerRegistered = true; 26} 27 28/** 29 * Manually cancel the import if needed. 30 */ 31export function cancelImport() { 32 cancelled = true; 33} 34 35/** 36 * Check whether the import should stop. 37 * Call this inside loops, batch processors, etc. 38 */ 39export function isImportCancelled(): boolean { 40 return cancelled; 41}