【问题标题】:Running a shell command from gulp从 gulp 运行 shell 命令
【发布时间】:2015-06-13 05:23:32
【问题描述】:

我想从 gulp 运行一个 shell 命令,使用 gulp-shell。我看到 gulpfile 使用了以下成语。

这是从 gulp 任务运行命令的惯用方式吗?

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

【问题讨论】:

    标签: javascript gulp


    【解决方案1】:

    gulp-shell 已被列入黑名单。您应该改用gulp-exec,它也有更好的文档。

    对于您的情况,它实际上说明了:

    注意:如果你只是想运行一个命令,只运行命令,不要使用这个插件:

    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);
      });
    })
    

    【讨论】:

    • gulp-shell has been blacklisted: 你这是什么意思?
    • Gulp 提供了一个不是以“Gulp 方式”编写的包的黑名单。在此处查看列表:github.com/gulpjs/plugins/blob/master/src/blackList.json
    • 传递给exec 的函数不会向 Gulp 记录任何内容。为什么没有发生这种情况?
    • 您在回答“只需运行命令”中的意思是什么......如果没有这样的插件,我如何通过 gulp 任务运行命令? (必须是一项任务,因为 gulp.watch 在每次文件更改时都会调用它
    • 更新:仍然没有记录任何东西。
    【解决方案2】:

    保持控制台输出相同的新方法(例如,使用颜色):

    见: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】:

    使用gulp 4,您的任务可以直接返回子进程来表示任务完成:

    '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

    【讨论】:

    • Gulp 4 是芝诺悖论的现代化身。
    • 较新的 Gulp 版本不会像这样使用gulp.task。如果它不适合您,请尝试 cp.exec 而不是 cp.execFile
    【解决方案4】:

    你可以这样做:

    const { spawn } = require('child_process');
    const gulp = require('gulp');
    
    gulp.task('list', function() {
        const cmd = spawn('ls');
        cmd.stdout.on('data', (data) => {
            console.log(`stdout: ${data}`);
        });
        return cmd;
    });
    

    【讨论】:

      【解决方案5】:

      根据 Gulp 的文档,你可以像这样运行echo 这样的命令:

      const cp = require('child_process');
      
      function childProcessTask() {
        return cp.exec('echo *.js');
      }
      
      exports.default = childProcessTask;
      

      exec 接受一个将由 shell 解析的字符串,默认情况下它会静音输出。

      我个人喜欢改用child_process.spawnspawn 接受命令的名称,然后是参数列表。如果您使用选项stdio: 'inherit',它不会吞下它的输出。

      function childProcessTask() {
        return cp.spawn('echo', ['one', 'two'], {stdio: 'inherit'});
      }
      

      参考资料:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 2011-06-04
        • 2015-07-17
        • 2018-07-30
        • 2011-01-28
        • 1970-01-01
        • 2012-12-22
        相关资源
        最近更新 更多