Running a shell command from gulp

匿名 (未验证) 提交于 2019-12-03 01:20:02

问题:

I would like to run a shell command from gulp, using gulp-shell. I see the following idiom being used the gulpfile.

Is this the idiomatic way to run a command from a gulp task?

var cmd = 'ls'; gulp.src('', {read: false})     .pipe(shell(cmd, {quiet: true}))     .on('error', function (err) {        gutil.log(err); });

回答1:

gulp-shell has been blacklisted. You should use gulp-exec instead, which has also a better documentation.

For your case it actually states:

Note: If you just want to run a command, just run the command, don't use this plugin:

var exec = require('child_process').exec;  gulp.task('task', function (cb) {   exec('ping localhost', function (err, stdout, stderr) {     console.log(stdout);     console.log(stderr);     cb(err);   }); })


回答2:

The new way to do this that keeps console output the same (e.g., with colors):

see: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

var gulp = require('gulp'); var spawn = require('child_process').spawn;  gulp.task('my-task', function (cb) {   var cmd = spawn('cmd', ['arg1', 'agr2'], {stdio: 'inherit'});   cmd.on('close', function (code) {     console.log('my-task exited with code ' + code);     cb(code);   }); });



回答3:

With gulp 4 your tasks can directly return a child process to signal task completion:

'use strict';  var cp = require('child_process'); var gulp = require('gulp');  gulp.task('reset', function() {   return cp.execFile('git checkout -- .'); });

https://github.com/gulpjs/gulp/blob/4.0/docs/recipes/running-shell-commands.md



转载请标明出处:Running a shell command from gulp
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!