-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
custom exit handlers #405
Comments
Why not listen to those events yourself? |
Those events never triggers in prompt mode. I want to return back to prev. menu or cancel a step with this keys.
|
You can pass your own event listener like @SBoudrias suggested. I ended up doing this in my app since I needed synchronous behavior.
|
I would prefer /**
* By default Inquirer handles Ctrl-C itself by force-quitting the process with
* no way to clean up. This wrapper around Inquirer throws a Error
* instead, allowing normal exception handling.
*/
async function safePrompt<T>(question: inquirer.Question<T>): Promise<T> {
const promptModule = inquirer.createPromptModule()
const ui = new inquirer.ui.Prompt((promptModule as any).prompts, {})
const deferred = PromiseUtils.deferred<T>()
// Remove the force-quit behavior
const rl = ui.rl
rl.listeners("SIGINT").forEach(listener => rl.off("SIGINT", listener as any))
// Insert our listener to reject the promise
function handleCtrlC() {
// remove the listener
rl.off("SIGINT", handleCtrlC)
// Clean up inquirer
ui.close()
// Then reject our promise
deferred.reject(
new Error("Aborted due to Ctrl-C during a prompt", ui)
)
}
rl.on("SIGINT", handleCtrlC)
// Run the UI
ui.run<T>([question]).then(deferred.resolve, deferred.reject)
return await deferred.promise
} (Implementation of PromiseUtils.deferred left as an exercise to the reader) |
Hey, i'm using inquirer on nodejs, i would like to use this function but in a js file |
i called the safePrompt using @justjake's code, type Answer = {
templateName: string;
};
export async function askTemplateName() {
return safePrompt<Answer>([
{
name: 'templateName',
type: 'list',
message: 'template name:',
choices: ['node.js', 'next.js'],
},
]);
} but i've seen an error like below,
export default class Deferred<T> {
public promise: Promise<T>;
// @ts-ignore
public resolve: (value: T | PromiseLike<T>) => void;
// @ts-ignore
public reject: (reason?: any) => void;
constructor() {
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
export async function safePrompt<T>(question: DistinctQuestion<T>): Promise<T> {
const promptModule = inquirer.createPromptModule();
const ui = new inquirer.ui.Prompt<T>((promptModule as any).prompts);
const deferred = new Deferred<T>();
// Remove the force-quit behavior
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { rl } = ui;
rl.listeners('SIGINT').forEach(listener => rl.off('SIGINT', listener as any));
// Insert our listener to reject the promise
function handleCtrlC() {
// remove the listener
rl.off('SIGINT', handleCtrlC);
// Clean up inquirer
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ui.close();
// Then reject our promise
deferred.reject(new Error(`Aborted due to Ctrl-C during a prompt. \n${ui.toString()}`));
}
rl.on('SIGINT', handleCtrlC);
// Run the UI
ui.run([question]).then(deferred.resolve, deferred.reject);
return deferred.promise;
} How can i catch the SIGINT signal? |
Closing this ticket as stale. Open new issues if you encounter problem with the new Inquirer API. The new API expose a documented |
https://github.com/SBoudrias/Inquirer.js/blob/master/lib/ui/baseUI.js#L21
this.rl.on('SIGINT', this.onForceClose);
process.on('exit', this.onForceClose);
Please provide an option or function to set exit handlers our own.
The text was updated successfully, but these errors were encountered: