gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:47:39 27 4
gpt4 key购买 nike

我需要运行一个 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"));

});

我用 gulp-shell运行 shell 命令,但我无法从 git rev-list --count HEAD 获得输出.

最佳答案

如果您需要命令的直接输出,您可以使用 Node 的 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 的更多信息,请参见此处: 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

关于gulp:如何执行 shell 命令并获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39554852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com