Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chain sequential tasks [duplicate]

Tags:

node.js

gulp

q

I have 3 tasks that I want to process and when those 3 tasks are done I want to do the following:

  1. Concat the three files together
  2. Uglify
  3. Write to disk

With Grunt I had a long process for all this. Here is what I have tried with Gulp

gulp.task('libs', function () {
return gulp.src('js/libs/*.js')
        .pipe(concat('01.libs.js', {newLine: ';'}))
        .pipe(gulp.dest('min'));
});

gulp.task('plugins', function () {
    return gulp.src('js/plugins/*.js')
            .pipe(concat('02.plugins.js', {newLine: ';'}))
            .pipe(gulp.dest('min'));
});

gulp.task('apps', function () {
    return gulp.src('js/apps/**/*.js')
            .pipe(concat('03.apps.js', {newLine: ';'}))
            .pipe(gulp.dest('min'));
});

gulp.task('scripts', ['libs', 'plugins', 'apps'], function () {
    return gulp .src('min/*.js')
            .pipe(concat('testFile.js', {newLine: ';\r\n'}))
            .pipe(rename({suffix: '.min.v' + pkg.version }))
            .pipe(gulp.dest('min'))
            .pipe(notify({ message: 'Scripts minified'}));
});

This works but I want to just stream the output rather than write out 3 intermediate files only to then concat those.

So I then tried:

function libs () {
    return gulp.src('js/libs/*.js')
            .pipe(concat('01.libs.js', {newLine: ';'}));
}

function plugins () {
    return gulp.src('js/plugins/*.js')
            .pipe(concat('02.plugins.js', {newLine: ';'}));
}

function apps () {
    return gulp.src('js/apps/**/*.js')
            .pipe(concat('03.apps.js', {newLine: ';'}));
}

So then my build would be:

gulp.task('build', function () {
    return libs()
        .pipe(plugins())
        .pipe(apps())
        .pipe(concat('TestFile.js', {newLine: ';\r\n'}))
        .pipe(rename({suffix: '.min.v' + pkg.version }))
        .pipe(gulp.dest('min'));
});

This doesnt work.

So I tried Q:

function allOfThem () {
    return Q.all(libs(), plugins(), apps());
}

gulp.task('build', function () {
    return allOfThem().then(function (one, two, three) {
        console.log(one, two, three);
    });
});

This I guess works but no data in the callback for then.

I'm lost. What's the best way to achieve this?

like image 889
Mike Fielden Avatar asked Feb 21 '14 20:02

Mike Fielden


People also ask

Do tasks run in parallel?

Concurrent tasks progress at the same time in the worker system but they don't progress simultaneously. Parallel tasks are executed by different workers at the same time. Concurrency refers to how a worker system handles multiple tasks while parallelism refers to how a worker system handles a single task.

How do I run two tasks in parallel?

If you have several tasks that can be run in parallel, but still need to wait for all the tasks to end, you can easily achieve this using the Task. WhenAll() method in . NET Core. This will upload the first file, then the next file.

What is a continuation task?

A continuation task (also known just as a continuation) is an asynchronous task that's invoked by another task, known as the antecedent, when the antecedent finishes.

What is ContinueWith c#?

The ContinueWith function is a method available on the task that allows executing code after the task has finished execution. In simple words it allows continuation. Things to note here is that ContinueWith also returns one Task. That means you can attach ContinueWith one task returned by this method.


1 Answers

Use event-stream.merge

var es = require('event-stream');
gulp.task('build', function () {
    return es.merge(libs(), plugins(), apps())
        .pipe(concat('TestFile.js', {newLine: ';\r\n'}))
        .pipe(rename({suffix: '.min.v' + pkg.version }))
        .pipe(gulp.dest('min'));
});
like image 91
Contra Avatar answered Sep 30 '22 05:09

Contra