How to select a command from the command line and execute it?

first of all, let"s talk about my project, which uses vue-li3.0 as scaffolding. After running the service and packaging, it is changed to the specified directory, that is, there will be many subprojects under this project. each time you run or package, you need to specify the name of the subproject, which is the background . So here comes the idea:

1. List all the subproject names through the command line
2. Select a name
3. Select the environment (local environment, test environment, demo environment.)
4. Choose to run the service or package operation

all the previous command line selections can be done through inquirer, but how do I execute a command after selecting it?
at present, I want to select the corresponding command configured under package.json, such as npm run serve
, which can be successfully executed through child_process, but cannot see the running output

.

Note: at present, my command to switch subprojects and run is npm config set project:name demoName & & npm run serve,. I"m a little tired every time I write this. Let"s see if the gods have a good way to implement it

.
Apr.04,2021

you can't see the output because you didn't redirect the output of the child process to the current process,

just write it this way:

var ls = spawn(_cmd, _args);

    ls.stdout.on('data', data => {
      console.log(`${data}`);
    });

    ls.stderr.on('data', data => {
      console.log(`stderr: ${data}`);
    });

    ls.on('close', code => {
      // console.log(`:${code}`);
    });

first of all, I would like to thank @ for the proposal offered by "one Blood". I am node scum, and I was finally able to realize

.

clipboard.png

the following is the code I implemented

const inquirer = require('inquirer');
const fs = require('fs');
const { spawn } = require('child_process');

try {
    // 
    fs.readdir('./project', (err, files) => {
        inquirer.prompt([
            {
                type: 'list',
                name: 'project',
                message: '',
                choices: files
            },
            {
                type: 'list',
                name: 'dev',
                message: '',
                choices: ['dev','test']
            },
            {
                type: 'list',
                name: 'operate',
                message: '',
                choices: ['serve','build']
            }
        ]).then(v => {
            console.log(v)
            const cmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
            const args = ['config', 'set', 'test:name', v.project, '&&', 'npm', 'run', v.operate]
            const opts = {
                stdio: 'inherit' // 
            }
            const ls = spawn(cmd, args, opts);
        })
    })
} catch (error) {
    console.error(error)
}
Menu