Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Gulp中轻松运行系统shell任务/命令?_Node.js_Shell_Gulp_Child Process - Fatal编程技术网

Node.js 如何在Gulp中轻松运行系统shell任务/命令?

Node.js 如何在Gulp中轻松运行系统shell任务/命令?,node.js,shell,gulp,child-process,Node.js,Shell,Gulp,Child Process,我考虑过以下几种选择: 使用gulpshell:该插件已被弃用且存在bug 使用gulpexec:似乎与我没有用的gulpstreams捆绑在一起。我不知道如何使用它来运行单个任务,作者甚至声称,您不应该只运行一个任务 使用child\u process.exec尽管这是gulp exec的作者建议的,但解决方案似乎不好,原因有两个:a)stdout和stderr没有逐步打印到控制台,只有在子进程结束后才可以作为缓冲区/字符串访问。b) 如果进程产生太多控制台输出,则会超出缓冲区限制,exec崩

我考虑过以下几种选择:

使用
gulpshell
:该插件已被弃用且存在bug

使用
gulpexec
:似乎与我没有用的gulpstreams捆绑在一起。我不知道如何使用它来运行单个任务,作者甚至声称,您不应该只运行一个任务

使用
child\u process.exec
尽管这是
gulp exec
的作者建议的,但解决方案似乎不好,原因有两个:a)stdout和stderr没有逐步打印到控制台,只有在子进程结束后才可以作为缓冲区/字符串访问。b) 如果进程产生太多控制台输出,则会超出缓冲区限制,exec崩溃。这意味着,除非您100%确定您的流程可以生成多少输出,否则不应该使用它

使用
child\u process.spawn
虽然有点冗长,但这是迄今为止我发现的最好的:

gulp.task('list', (cb) => {
  childProcess.spawn('find', ['/'], { stdio: 'inherit' }).on('close', cb);
});

这是一个很好的比较。你试过execSync吗?你是说
子进程.execSync
?好吧,那只是
child\u process.exec的同步版本,我猜它会遇到与其异步同类相同的问题。