2016-09-18 10 views
1

を取得する:一息:どのようにシェルコマンドを実行すると、私はシェルコマンドを実行し、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")); 

}); 

私はシェルコマンドを実行するためにgulp-shellを使用しますが、私ができます」出力をgit rev-list --count HEADから取得します。

答えて

5

コマンドから直接出力する必要がある場合は、Nodeのchild_process.execSyncを使用してからtoStringを呼び出すことができます。あなたのコードの場合

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

が、それはこのようなものになります:たとえば

child_process.execSync詳細について

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

}); 

を、ここを参照してください:あまりにもFYI https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options

あなたやこの質問を見る他の誰かのために、gulp-shellはgulpチームによってブラックリストに載っています:https://github.com/gulpjs/plugins/blob/master/src/blackList.json

+0

本当にありがとうございました。私は 'gulp-shell 'を使うのをやめます。 –

関連する問題