Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run another gulpfile in current gulpfile

Tags:

gulp

I need to run another gulp file before the 'default' task in current gulp file. Is there any gulp plugin for this situation.

like image 516
zhenyong Avatar asked Jan 17 '15 07:01

zhenyong


1 Answers

You can use child_process.exec(...) to run the both gulp tasks like you would from the console using the CLI API. There is Gulp.run but that function is deprecated and will be removed moving forward.

This snippet will run both gulp files below in succession.

run-two-gulp-files.js

To run: node run-two-gulp-files.js

./gulpfile.js depends on ./other-thing-with-gulpfile/gulpfile.js

var exec = require('child_process').exec;

// Run the dependency gulp file first
exec('gulp --gulpfile ./other-thing-with-gulpfile/gulpfile.js', function(error, stdout, stderr) {
    console.log('other-thing-with-gulpfile/gulpfile.js:');
    console.log(stdout);
    if(error) {
        console.log(error, stderr);
    }
    else {

        // Run the main gulp file after the other one finished
        exec('gulp --gulpfile ./gulpfile.js', function(error, stdout, stderr) {
            console.log('gulpfile.js:');
            console.log(stdout);
            if(error) {
                console.log(error, stderr);
            }
        });
    }
});

gulpfile.js

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

gulp.task('file1-txt', function() {
    return gulp.src('file1.txt')
        .pipe(replace(/foo/g, 'bar'))
        .pipe(gulp.dest('dest'));
});

gulp.task('default', ['file1-txt']);

other-thing-with-gulpfile/gulpfile.js

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

gulp.task('file2-txt', function() {
    return gulp.src('file2.txt')
        .pipe(replace(/baz/g, 'qux'))
        .pipe(gulp.dest('../dest'));
});

gulp.task('default', ['file2-txt']);
like image 81
MLM Avatar answered Oct 15 '22 01:10

MLM