在gulp中为子文件夹执行命令 [英] Excute command in gulp for sub folder
问题描述
我的项目结构如下:
myapp
-server.js
-test
--testcontent
--- package.json
-package.json
我有两个 package.json
文件,我想在 npm install
> package.json 在 testcontent
文件夹中。
如果在命令行I转到 myapp / test / testcontent
并运行 npm install
它可以工作,并创建一个新文件夹 node_modules
与来自正确的 package.json
的依赖关系。如何从gulp内完成?
我尝试了以下方法,但它使用 package.json
code> myapp 不是 testcontent
子文件夹中的一个:
gulp.task('default',function(){
var options = {
continueOnError:true,// default = false,true表示不会发出错误事件
pipeStdout:true,// default = false,true表示将stdout写入file.contents
customTemplatingThing:test//将内容传递给gutil.template()
};
var reportOptions = {
err:true,// default = true,false表示不写err
stderr:true,// default = true,false表示不写stderr
stdout:true // default = true,false表示不写stdout
}
gulp.src('test / testcontent /')
.pipe(exec('npm安装',选项))
.pipe(exec.reporter(reportOptions));
});
是这项工作的错误工具。实际上,
gulp-exec
插件的作者明确地建议不要以您所使用的方式使用它:
注意:如果您只是想要运行一个命令,只需运行命令,不要使用这个插件
相反,您可以使用node.js内置的< a href =https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options =nofollow> child_process.spawn()
。您可以使用 cwd
选项传递执行该命令的目录:
var spawn = require('child_process')。spawn;
$ b $ gulp.task('default',function(done){
spawn('npm',['install'],{cwd:'test / testcontent /',stdio:'继承'})
.on('close',done);
});
My project is structured as follows:
myapp
-server.js
-test
--testcontent
---package.json
-package.json
I have two package.json
files and I want to run npm install
on the package.json
inside the testcontent
folder.
If in the command line I go to myapp/test/testcontent
and run npm install
it works and it creates a new folder node_modules
with the dependencies from the correct package.json
. How can that be done from within gulp?
I tried the following but it uses the package.json
in myapp
not the one in the testcontent
sub folder:
gulp.task('default', function () {
var options = {
continueOnError: true, // default = false, true means don't emit error event
pipeStdout: true, // default = false, true means stdout is written to file.contents
customTemplatingThing: "test" // content passed to gutil.template()
};
var reportOptions = {
err: true, // default = true, false means don't write err
stderr: true, // default = true, false means don't write stderr
stdout: true // default = true, false means don't write stdout
}
gulp.src('test/testcontent/')
.pipe(exec('npm install' , options))
.pipe(exec.reporter(reportOptions));
});
gulp-exec
is the wrong tool for this job. In fact the authors of the gulp-exec
plugin explicitly advise against using it the way you are doing:
Note: If you just want to run a command, just run the command, don't use this plugin
Instead you use the node.js built-in child_process.spawn()
. You can pass the directory where the command should be executed using the cwd
option:
var spawn = require('child_process').spawn;
gulp.task('default', function(done) {
spawn('npm', ['install'], { cwd: 'test/testcontent/', stdio: 'inherit' })
.on('close', done);
});
这篇关于在gulp中为子文件夹执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!