Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async task with multiple gulp.src #82

Closed
6aKa opened this issue Jan 3, 2014 · 21 comments
Closed

Async task with multiple gulp.src #82

6aKa opened this issue Jan 3, 2014 · 21 comments

Comments

@6aKa
Copy link

6aKa commented Jan 3, 2014

Example, with useing callback

gulp.task('test', function(cb) {

  gulp.src('bootstrap/js/*.js')
    .pipe(gulp.dest('public/bootstrap'))
    .on('end', function() {cb()});

  gulp.src('jquery.cookie/jquery.cookie.js')
    .pipe(gulp.dest('public/jquery'))
    .on('end', function() {cb()});

});

Please add method for event 'end'. Like this

gulp.task('test', function(cb) {

  gulp.src('bootstrap/js/*.js')
    .pipe(gulp.dest('public/bootstrap'))
    .end(cb());

  gulp.src('jquery.cookie/jquery.cookie.js')
    .pipe(gulp.dest('public/jquery'))
    .end(cb());

});

And for 'error' and 'data' events.

@dashed
Copy link
Contributor

dashed commented Jan 3, 2014

@6aKa You're suggesting wrappers for .on(...) which is part of the node.js events api (see: http://nodejs.org/api/events.html).

This is out of scope of gulp.

@floatdrop
Copy link
Contributor

You got a problem - cb is called twice from src. I suggest this is more accurate way to do this:

gult.task('cookie', function(cb) {
    gulp.src('jquery.cookie/jquery.cookie.js')
        .pipe(gulp.dest('public/jquery'));
});

gult.task('bootstrap', function(cb) {
    gulp.src('bootstrap/js/*.js')
        .pipe(gulp.dest('public/bootstrap'));
});

gulp.task('test', function() {
    gulp.run('cookie', 'bootstrap');
});

But if you want shorter version of on('end', function ... ) you can write like this:

gulp.task('test', function(cb) {

  gulp.src('bootstrap/js/*.js')
    .pipe(gulp.dest('public/bootstrap'))
    .on('end', cb);

  gulp.src('jquery.cookie/jquery.cookie.js')
    .pipe(gulp.dest('public/jquery'))
    .on('end', cb);
});

@6aKa
Copy link
Author

6aKa commented Jan 3, 2014

@dashed I suggest aliases. For example I use many variables, and path.join for src and dest. Gulpfile look likes from hell. And because, I want many short constructions, aliases :)

@floatdrop Split for individual task more accurate, but ugly if you copy many dependencies to work directory. I want true async tasks, with multiple gulp.src inside, without callback hell

@robrich
Copy link
Contributor

robrich commented Jan 3, 2014

Should gulp warn you or error if you call cb more than once? Currently it just suppresses the condition, ignoring all but the first call. See robrich/orchestrator#10

@floatdrop
Copy link
Contributor

@6aKa maybe with event-stream.concat this will look nicer:

var es = require('event-stream');

gulp.task('test', function(cb) {
    es.concat(
        gulp.src('bootstrap/js/*.js')
            .pipe(gulp.dest('public/bootstrap')),
        gulp.src('jquery.cookie/jquery.cookie.js')
            .pipe(gulp.dest('public/jquery'))
    ).on('end', cb);
});

@dashed
Copy link
Contributor

dashed commented Jan 3, 2014

There should be a wiki page to hold all the neat usage patterns of gulp. Some of the interesting ones usually get buried in github issues.

@6aKa
Copy link
Author

6aKa commented Jan 3, 2014

@robrich for errors I have event 'error' and can catch with .on('error', cb). I like ideas for callbacks from https://github.com/caolan/async.
Useing async.series or async.parallelI can collect results in callback from all streams
Useing async.waterfall I get truly asynchronous streams with arguments
Something like this

gulp.task('test', function(cb) {
async.series([
  gulp.src('bootstrap/js/*.js')
    .pipe(gulp.dest('public/bootstrap'))
    .on('error', callback(err, 'one'))
    .on('end', callback(null, 'one'));
  gulp.src('jquery.cookie/jquery.cookie.js')
    .pipe(gulp.dest('public/jquery'))
    .on('error', callback(err, 'two'))
    .on('end', callback(null, 'two'););
], function (err, result) {
});
});

@floatdrop concat with event-stream look very good. I think this trick should be added to wiki :)

@floatdrop
Copy link
Contributor

Added recepies in wiki. Is this closing this task @6aKa ?

@yocontra
Copy link
Member

yocontra commented Jan 3, 2014

@floatdrop perfect - just what that section was for

@yocontra yocontra closed this as completed Jan 3, 2014
@yocontra
Copy link
Member

yocontra commented Jan 3, 2014

You can actually just return the es.concat stream btw - no need to listen for end or error events since the task system already does that if you return a stream from a task

@floatdrop
Copy link
Contributor

Thanks @contra ! It also make mocha gist shorter!

@treeskar
Copy link

treeskar commented Jun 8, 2014

You can use merge-stream

var concat = require('gulp-concat'),
    merge = require('merge-stream');

merge(
        gulp.src('./src/app/**/*.js')
            .pipe(concat('all.js'))
            .pipe(gulp.dest('./tmp/js')),
        gulp.src('./tmp/tpl/templates.js')
    )
        .pipe(concat('all.js'))
        .pipe(gulp.dest('../public/js'));

@PanYuntao
Copy link

I do it like this:

gulp.task('publish', function(cb) {
    async.series([
        function (callback) {
            gulp.src('css/*.css')
                .pipe(minifyCss())
                .pipe(rev())
                .pipe(gulp.dest('public/css'))
                .pipe( rev.manifest() )
                .pipe( gulp.dest( 'rev/css' ) )
                .on('end', callback);
        },
        function (callback) {
            gulp.src('js/*.js')
                .pipe(jshint())
                .pipe(uglify())
                .pipe(rev())
                .pipe(gulp.dest('public/js'))
                .pipe( rev.manifest() )
                .pipe( gulp.dest( 'rev/js' ) )
                .on('end', callback);
        }
        ,
        function (callback) {
            gulp.src('images/*')
                .pipe(imagemin({
                    progressive: true,
                    svgoPlugins: [{removeViewBox: false}],
                    use: [pngquant()]
                }))
                .pipe(gulp.dest('public/img'))
                .on('end', callback);
        }
        ],
        function (err, values) {
            if (err) {
                cb('your error');
            }
            else
            {
                console.log('success');
                cb();
                //process.exit();
            }
        });
});

gulp.task('default', ['publish']);

@yairEO

This comment has been minimized.

@yairEO

This comment has been minimized.

@stephenlacy
Copy link
Contributor

stephenlacy commented Jun 13, 2018

Do not restart an issue thread from 4 years ago.

@yairEO
Copy link

yairEO commented Jun 13, 2018

it's better, IMHO, that the information regarding this question will be here than scattered around the internet, so others will easily access it and gain insights.

@robrich
Copy link
Contributor

robrich commented Jun 13, 2018

const async = require('async');

@yairEO
Copy link

yairEO commented Jun 13, 2018

@robrich - This async which you require, is it a 3rd-party script?

why would I want to load such enormous package for something so simple like I showed in my code comment above?

@robrich

This comment has been minimized.

@phated
Copy link
Member

phated commented Jun 13, 2018

@robrich you're right, it's getting locked because you are so misinformed about where gulp is now. We even have tight integration with async/await

@gulpjs gulpjs locked as off-topic and limited conversation to collaborators Jun 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants