Problems with exiting from CLI

285 Views Asked by At

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)
})
1

There are 1 best solutions below

3
On

If you use async functions or promises, you have to terminate the program with process.exit(). Add it after execSync into the then call.