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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With