Running a shell command from gulp
我想使用
这是从gulp任务运行命令的惯用方法吗?
1 2 3 4 5 6 | var cmd = 'ls'; gulp.src('', {read: false}) .pipe(shell(cmd, {quiet: true})) .on('error', function (err) { gutil.log(err); }); |
对于你的情况,它实际上说:
Note: If you just want to run a command, just run the command, don't use this plugin:
1 2 3 4 5 6 7 8 9 | 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); }); }) |
执行此操作的新方法使控制台输出保持不变(例如,使用颜色):
请参阅:https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
1 2 3 4 5 6 7 8 9 10 | 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); }); }); |
使用gulp 4,您的任务可以直接返回子进程以指示任务完成:
1 2 3 4 5 6 7 8 | 'use strict'; var cp = require('child_process'); var gulp = require('gulp'); gulp.task('reset', function() { return cp.execFile('git checkout -- .'); }); |
gulp-v4-running-shell-commands.md