Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run node js scripts from gulp task

I have many js scripts in one folder (scripts/*.js). How to execute them all from the gulp task (instead of using 'node script.js' many times)?

something like

gulp.task('exec_all_scripts', function () {
  gulp.src(path.join(__dirname, './scripts/*.js'))
})
like image 666
user8439944 Avatar asked May 31 '26 20:05

user8439944


1 Answers

Gulp is a task runner, meaning it's meant to automate sequences of commands; not run entire scripts. Instead, you can use NPM for that. I don't think there's a way to glob scripts and run them all at once, but you can set each file as its own npm script and use npm-run-all to run them:

{
    "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"
    }
}

Then you can use npm start to run all your scripts at once.

If you really need to use gulp to run the scripts, you can use the same strategy, and then use gulp-run to run the npm script with gulp.

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. 
})
like image 148
Quangdao Nguyen Avatar answered Jun 03 '26 10:06

Quangdao Nguyen