如何在 ShellJS 命令执行期间使用 cli-spinner 运行?

How to have cli-spinner run during ShellJS command exec?

我目前正在尝试制作一个简单的命令行节点程序,它允许我轻松地为我制作的许多 React/Redux 应用程序创建样板。我正在使用 ShellJS to execute console commands (I've tried using Node's child_process, too.) The problem is getting cli-spinner 来执行终端命令。这是我的代码:

#! /usr/bin/env node
var shell = require('shelljs');
var userArgs = process.argv.slice(2);
var folderName = userArgs[0];
var Spinner = require('cli-spinner').Spinner;
var depSpin = new Spinner('Installing dependencies.. %s');
depSpin.setSpinnerString(10);

shell.mkdir(folderName);
shell.cd(folderName);

depSpin.start(); 
// I expect for the spinner to start here (before the execution of the commands.)

shell.exec('npm init -y', {silent: true});
shell.exec('npm install --save babel-core babel-loader babel-preset-es2015 babel-preset-react react-dom react-redux redux webpack', {silent: true});
shell.exec('npm install --save-dev babel-preset-env webpack-dev-server', {silent: true});

depSpin.stop(); 
// Since ShellJS should run synchronously, 
// the spinner should stop right after the last command finishes.

shell.touch('webpack.config.js');
shell.mkdir(['build', 'frontend']);

shell.cd('frontend');
shell.mkdir(['components', 'containers', 'reducers', 'store']);
shell.touch('app.js');

但是当 运行 程序时,它只是在安装依赖项时挂起而不显示任何内容。这与微调器代码甚至不在其中时的情况相同。我还尝试删除 depSpin.stop(),这只会让程序永远挂在微调器上。我感觉这个问题是由 cli-spinnerShellJS 都使用终端的冲突引起的。

我能够通过使用 child_processspawn 来实现这种效果。我必须创建 child_process.spawn 和 运行 所有与 && 连接的命令。这释放了控制台输出,使其完全成为 cli-spinner 输出。然后我为子进程退出以停止微调器做了一个事件处理程序。

#! /usr/bin/env node
var shell = require('shelljs');
var userArgs = process.argv.slice(2);
var folderName = userArgs[0];
var Spinner = require('cli-spinner').Spinner;
var depSpin = new Spinner('Installing dependencies.. %s');
var spawn = require('child_process').spawn;
var commandsDep = [
  'cd ' + folderName,
  'npm init -y',
  'npm install --save babel-core babel-loader babel-preset-es2015 babel-preset-react react-dom react-redux redux webpack',
  'npm install --save-dev babel-preset-env webpack-dev-server'
];
var depChild = spawn(commandsDep.join(' && '), {
  shell: true
});
depSpin.setSpinnerString(18);

shell.mkdir(folderName);
shell.cd(folderName);

depSpin.start();
depChild.on('exit', () => {
  depSpin.stop();
  shell.exec('clear');
  console.log('Installing dependencies.. ✓');
})

shell.touch('webpack.config.js');
shell.mkdir(['build', 'frontend']);

shell.cd('frontend');
shell.mkdir(['components', 'containers', 'reducers', 'store']);
shell.touch('app.js');

shell.cd('containers');
shell.touch(['AppContainer.js', 'Root.js']);

shell.cd('../reducers');
shell.touch('index.js');

shell.cd('../store');
shell.touch('configureStore.js');

根据 Owens 的回答,我自己制作了一个辅助方法 运行 带有微调器的长命令与普通 shelljs 命令内联;

const Spinner = require('cli-spinner').Spinner;
const spinner = new Spinner('installing.. %s');
spinner.setSpinnerString('|/-\');
var spawn = require('child_process').spawn;

const longCommand = (command, onSuccess) => {
  return new Promise((resolve, reject) => {
    var process = spawn(command, { shell: true });
      spinner.start();
      process.on('exit', () => {
        spinner.stop();
        onSuccess();
        resolve();
      })
  })
}

const npmInstall = async () => {
  await longCommand("npm install", () => console.log(`NPM modules installed! `))
  // Other stuff
  shell.mkdir('new')
}

npmInstall()