0

I am building a CLI that creates starter code for web applications. Right now we just have the UI set up with some sample questions. We want to make sure that when the user presses CRTL-C or CRTL-D the program is exited cleanly and a goodbye message appears. I was able to catch CRTL-C but it does not work for CRTL-D.

Here is the catch I have so far:

let answers = async () => {
    try {
        await prompt(questions)
    } catch (err) {
        console.log(chalk.yellow("Closing web-stater-cli..."))
    }
}

answers()

But when I hit CRTL-D it just stops with no message. Does anyone have any suggestions for how to properly exit and display the message?

  • use process events `process.on('SIGINT', ...`, this will help https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits – Lawrence Cherone Mar 09 '20 at 21:42

1 Answers1

0

Probably the readline interface that you are using does not support ^D. I'd suggest to try this behaviour using the readline built-in module, setting the stdin to raw mode. This way you can listen to the "close" event with no signal SIGINT (no ctrl+C). You can check more on node doc

stackchain
  • 81
  • 5