从gulp任务运行节点js脚本

use*_*944 1 javascript task node.js gulp

我在一个文件夹(scripts/*.js)中有许多js脚本.如何从gulp任务执行它们(而不是多次使用'node script.js')?

就像是

gulp.task('exec_all_scripts', function () {
  gulp.src(path.join(__dirname, './scripts/*.js'))
})
Run Code Online (Sandbox Code Playgroud)

Qua*_*yen 5

Gulp是一个任务运行器,意味着它意味着自动化命令序列; 不运行整个脚本.相反,您可以使用NPM.我不认为有一种方法可以使用glob脚本并一次运行它们,但您可以将每个文件设置为自己的npm脚本并用于npm-run-all运行它们:

{
    "name": "sample",
    "version": "0.0.1",
    "scripts": {
        "script:foo": "node foo.js",
        "script:bar": "node bar.js",
        "script:baz": "node baz.js",
        "start": "npm-run-all --parallel script:*",
    },
    "dependencies": {
        "npm-run-all": "^4.0.2"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用npm start一次运行所有脚本.

如果你真的需要使用gulp来运行脚本,你可以使用相同的策略,然后使用gulp-rungulp运行npm脚本.

var run = require('gulp-run');

// use gulp-run to start a pipeline 
gulp.task('exec_all_scripts', function() {
    return run('npm start').exec()    // run "npm start". 
        .pipe(gulp.dest('output'));      // writes results to output/echo. 
})
Run Code Online (Sandbox Code Playgroud)