Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gulp: how to execute shell command and get the output

Tags:

gulp

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.

like image 717
Xaree Lee Avatar asked Dec 25 '22 01:12

Xaree Lee


1 Answers

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

like image 165
ryanlutgen Avatar answered May 23 '23 06:05

ryanlutgen