Comparing version
@@ -35,3 +35,5 @@ import { Observable } from 'rxjs'; | ||
} | ||
export type PromptFn<Value = any, Config = any> = (config: Config, context?: StreamOptions) => Promise<Value>; | ||
export type PromptFn<Value = any, Config = any> = (config: Config, context: StreamOptions & { | ||
signal: AbortSignal; | ||
}) => Promise<Value>; | ||
/** | ||
@@ -50,3 +52,2 @@ * Provides a set of prompt-constructors. | ||
private opt; | ||
rl?: InquirerReadline; | ||
constructor(prompts: PromptCollection, opt?: StreamOptions); | ||
@@ -57,6 +58,2 @@ run(questions: PromptSession<A>, answers?: Partial<A>): Promise<A>; | ||
/** | ||
* Handle the ^C exit | ||
*/ | ||
private onForceClose; | ||
/** | ||
* Close the interface and cleanup listeners | ||
@@ -63,0 +60,0 @@ */ |
@@ -147,8 +147,2 @@ "use strict"; | ||
}); | ||
Object.defineProperty(this, "rl", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "prepareQuestion", { | ||
@@ -199,8 +193,20 @@ enumerable: true, | ||
const promptFn = isPromptConstructor(prompt) | ||
? (q, { signal } = {}) => new Promise((resolve, reject) => { | ||
const rl = node_readline_1.default.createInterface(setupReadlineOptions(this.opt)); | ||
rl.resume(); | ||
? (q, opt) => new Promise((resolve, reject) => { | ||
const { signal } = opt; | ||
if (signal.aborted) { | ||
reject(new core_1.AbortPromptError({ cause: signal.reason })); | ||
return; | ||
} | ||
const rl = node_readline_1.default.createInterface(setupReadlineOptions(opt)); | ||
/** | ||
* Handle the ^C exit | ||
*/ | ||
const onForceClose = () => { | ||
this.close(); | ||
process.kill(process.pid, 'SIGINT'); | ||
console.log(''); | ||
}; | ||
const onClose = () => { | ||
process.removeListener('exit', this.onForceClose); | ||
rl.removeListener('SIGINT', this.onForceClose); | ||
process.removeListener('exit', onForceClose); | ||
rl.removeListener('SIGINT', onForceClose); | ||
rl.setPrompt(''); | ||
@@ -212,30 +218,23 @@ rl.output.unmute(); | ||
}; | ||
this.rl = rl; | ||
// Make sure new prompt start on a newline when closing | ||
process.on('exit', this.onForceClose); | ||
rl.on('SIGINT', this.onForceClose); | ||
process.on('exit', onForceClose); | ||
rl.on('SIGINT', onForceClose); | ||
const activePrompt = new prompt(q, rl, this.answers); | ||
const cleanup = () => { | ||
onClose(); | ||
this.rl = undefined; | ||
cleanupSignal === null || cleanupSignal === void 0 ? void 0 : cleanupSignal(); | ||
}; | ||
if (signal) { | ||
const abort = () => { | ||
reject(new core_1.AbortPromptError({ cause: signal.reason })); | ||
cleanup(); | ||
}; | ||
if (signal.aborted) { | ||
abort(); | ||
return; | ||
} | ||
signal.addEventListener('abort', abort); | ||
cleanupSignal = () => { | ||
signal.removeEventListener('abort', abort); | ||
cleanupSignal = undefined; | ||
}; | ||
} | ||
const abort = () => { | ||
reject(new core_1.AbortPromptError({ cause: signal.reason })); | ||
cleanup(); | ||
}; | ||
signal.addEventListener('abort', abort); | ||
cleanupSignal = () => { | ||
signal.removeEventListener('abort', abort); | ||
cleanupSignal = undefined; | ||
}; | ||
activePrompt.run().then(resolve, reject).finally(cleanup); | ||
}) | ||
: prompt; | ||
let cleanupModuleSignal; | ||
const { signal: moduleSignal } = this.opt; | ||
@@ -246,5 +245,5 @@ if (moduleSignal === null || moduleSignal === void 0 ? void 0 : moduleSignal.aborted) { | ||
else if (moduleSignal) { | ||
const abort = (reason) => { var _a; return (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.abort(reason); }; | ||
const abort = () => { var _a; return (_a = this.abortController) === null || _a === void 0 ? void 0 : _a.abort(moduleSignal.reason); }; | ||
moduleSignal.addEventListener('abort', abort); | ||
cleanupSignal = () => { | ||
cleanupModuleSignal = () => { | ||
moduleSignal.removeEventListener('abort', abort); | ||
@@ -262,2 +261,3 @@ }; | ||
cleanupSignal === null || cleanupSignal === void 0 ? void 0 : cleanupSignal(); | ||
cleanupModuleSignal === null || cleanupModuleSignal === void 0 ? void 0 : cleanupModuleSignal(); | ||
}); | ||
@@ -267,15 +267,2 @@ }) | ||
/** | ||
* Handle the ^C exit | ||
*/ | ||
Object.defineProperty(this, "onForceClose", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: () => { | ||
this.close(); | ||
process.kill(process.pid, 'SIGINT'); | ||
console.log(''); | ||
} | ||
}); | ||
/** | ||
* Close the interface and cleanup listeners | ||
@@ -304,3 +291,3 @@ */ | ||
const shouldRun = yield (0, run_async_1.default)(when)(this.answers); | ||
return shouldRun !== false; | ||
return Boolean(shouldRun); | ||
} | ||
@@ -307,0 +294,0 @@ return when !== false; |
{ | ||
"name": "inquirer", | ||
"version": "11.0.1", | ||
"version": "11.0.2", | ||
"description": "A collection of common interactive command line user interfaces.", | ||
@@ -88,3 +88,3 @@ "author": "Simon Boudrias <admin@simonboudrias.com>", | ||
"typings": "./dist/cjs/types/index.d.ts", | ||
"gitHead": "9e29035c2efc78f44aed3c7732aee46ab1d64ca2" | ||
"gitHead": "95674a1ebabb2e715d243b1718647274400416ce" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
75023
-0.42%925
-2.01%