SBoudrias / Inquirer.js

A collection of common interactive command line user interfaces.
MIT License
20.09k stars 1.3k forks source link

Problems with exiting from CLI #1050

Open amadeus-8 opened 3 years ago

amadeus-8 commented 3 years ago

I've created simple CLI using inquirer and child_process. And i have problem, when i'am closing process with CTLR+C, it seems like its still running process, because terminal not responding anything. And i have to close terminal every time and open it again, it's uncomfortable for me. What i need to do, to exit all processes with simple CTRL+C command? Searched so many solutions in google, but no result. Hope somebody can help me with it

const inquirer = require('inquirer')
const { execSync } = require('child_process')
const { readdirSync } = require('fs')

const targetFolder = './src/pages'

const directories = readdirSync(targetFolder)

const questions = [
    {
        type: 'list',
        name: 'page',
        message: 'Choose page to compile',
        choices: directories,
    },
]

inquirer.prompt(questions).then(({ page }) => {
    const options = {
        stdio: 'inherit',
    }
    execSync(`npm run start -- --env page=${page}`, options)
})
raskyer commented 1 month ago

We have the same issue on Powershell when hitting CTRL+ C:

ExitPromptError: User force closed the prompt with 0 null
    at path\node_modules\@inquirer\core\dist\cjs\lib\create-prompt.js:60:28
    at Emitter.emit (path\node_modules\signal-exit\dist\cjs\index.js:71:19)
    at #processEmit (path\node_modules\signal-exit\dist\cjs\index.js:240:27)
    at #process.emit (path\node_modules\signal-exit\dist\cjs\index.js:191:37)
    at process.callbackTrampoline (node:internal/async_hooks:128:17)
SBoudrias commented 1 month ago

@raskyer is it the same issue? Is the problem the error itself? Or that even with the error the prompt doesn't exit?

raskyer commented 1 month ago

I have both, for the error message I have fixed it with the solution from another ticket. I have added process.exit(0) in the catch and it seems to fix the issue but without it it's hanging.

SBoudrias commented 1 month ago

process.exit() forces the process to exit immediately.

Without it, Node waits for timeouts and other scheduled async operations to complete in order to safely exit. That's not specific to Inquirer.