3

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'))
})
2
  • Gulp isn't really a script runner. Why would you expect it's the correct tool for this job? Commented Aug 9, 2017 at 13:06
  • I dont know. I thought it was able to do with gulp Commented Aug 9, 2017 at 14:28

3 Answers 3

10

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. 
})
Sign up to request clarification or add additional context in comments.

Comments

0

you can export functions in your scripts/*.js and import them in gulpfile.js and call the functions in 'exec_all_scripts' task, it's easy

Comments

0

You could concatinate all of the scripts into a single script and then execute it from the same task, a different task, or using a different process. See the following NPM package: https://www.npmjs.com/package/gulp-concat

Here is an example:

var concat = require('gulp-concat');   // include package

gulp.task('exec_all_scripts', function() {
  return gulp.src(path.join(__dirname, './scripts/*.js')
    .pipe(concat('all_scripts.js'))
    .pipe(gulp.dest('./dist/'));  // assuming you had a dist folder but this could be anything
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.