Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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
gulp:如何执行shell命令并获得输出_Gulp - Fatal编程技术网

gulp:如何执行shell命令并获得输出

gulp:如何执行shell命令并获得输出,gulp,Gulp,我需要运行shell命令并将结果写入json文件: const Shell = require('gulp-shell'); gulp.task('build-info', () => { gulp.src('./app/_bundle_info_.json') .pipe(jeditor((json) => { var buildNumber = Shell.task([ 'git rev-list --count HEAD'

我需要运行shell命令并将结果写入json文件:

const Shell = require('gulp-shell');

gulp.task('build-info', () => {
  gulp.src('./app/_bundle_info_.json')
    .pipe(jeditor((json) => {
      var buildNumber = Shell.task([
        'git rev-list --count HEAD'
      ]);
      // !!!: I can't get the output of `git rev-list --count HEAD ` via `buildNumber`
      json.buildNumber = buildNumber;
      return json;
    }))
    .pipe(gulp.dest("./app"));

});

我使用运行shell命令,但无法从git rev list--count HEAD中获得输出。如果需要命令的直接输出,可以使用节点的child_process.execSync,然后调用toString

例如:

var execSync = require('child_process').execSync;
console.log(execSync('pwd').toString());
对于您的代码,它将如下所示:

var execSync = require('child_process').execSync;

gulp.task('build-info', () => {
  gulp.src('./app/_bundle_info_.json')
    .pipe(jeditor((json) => {
      var buildNumber = execSync('git rev-list --count HEAD').toString();
      // !!!: I can't get the output of `git rev-list --count HEAD ` via `buildNumber`
      json.buildNumber = buildNumber;
      return json;
    }))
    .pipe(gulp.dest("./app"));

});
有关child_process.execSync的更多信息,请参阅此处:


仅供您或其他任何人参考,gulp shell已被gulp团队列入黑名单:

非常感谢您的帮助和信息。我将停止使用
gulp shell
。该blackList.json文件上次更新是在2018年,但gulp shell仍然定期更新。我想知道,在几乎两年后的这个时候,将其列入黑名单是否合理…@Chris Morgan:最近对gulp shell的所有承诺都只是更新依赖项。最新的重大变化是TS的重写。被列入黑名单的原因仍然存在,促进了反模式。注意,自从请求将gulp shell自述文件从黑名单中删除以来,gulp shell自述文件上的语法没有改变:旁注:查看gulp提交,过去2年的提交仅为文档。人们已经转向AngularCLI、Webpack等,因此黑名单上也出现了同样程度的不活动是有道理的。