4

I need to run a shell command and write the results into a json file:

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

});

I use gulp-shell to run the shell command, but I can't get the output from git rev-list --count HEAD.

Xaree Lee
  • 3,188
  • 3
  • 34
  • 55

1 Answers1

11

If you need the straight output from a command, you can use Node's child_process.execSync and then calling toString on it.

For example:

var execSync = require('child_process').execSync;
console.log(execSync('pwd').toString());

For your code, it would look something like this:

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

});

For more info on child_process.execSync, see here: https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options

FYI too for you or anyone else who views this question, gulp-shell is been blacklisted by the gulp team: https://github.com/gulpjs/plugins/blob/master/src/blackList.json

ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
  • Really thank your help and info. I'll stop to use `gulp-shell`. – Xaree Lee Sep 18 '16 at 06:50
  • That blackList.json file was last updated in 2018 but gulp-shell is updated regularly still. I wonder if including it on the black list is justified at this point, almost two years later... – Chris Morgan Feb 15 '20 at 14:31
  • @Chris Morgan: All recent commits on gulp-shell are just updating dependencies. The most recent major change was a rewrite in TS. The reasoning behind being on the blacklist is still there, promoting anti-patterns. Note the syntax on the gulp-shell readme has not changed since this request to remove it from the blacklist: https://github.com/gulpjs/gulp/issues/1353 Side note: Looking at Gulp commits, the commits for the past ~2 years are just docs. People have moved on to AngularCLI, Webpack, etc, therefore it makes sense the blacklist has also seen the same level of inactivity. – ryanlutgen Feb 17 '20 at 07:30