1

Having trouble understanding gulp (I'm new).

I want to automate a product build which involves cloning from a gitlab repo.

I don't understand why I need src('package.json') there, is this some kind of a trick? I also don't get why I get notified when task starts and not at the end!

var gulp   = require('gulp');
var git    = require('gulp-git-streamed');
var notify = require('gulp-notify');


gulp.task('clone', function() {
  return gulp
    .src('package.json') // wth is this?
    .pipe(git.clone('https://gitlab.somedomain.com/mockups/theme.git', {args: './src/gitlab/mockups'}))
    .pipe(notify('psd repo cloned!')); // why does this run before cloning is finished?
});

What happens is:

[13:16:34] gulp-notify: [Gulp notification] psd repo cloned!
[13:20:23]  Cloning into './src/gitlab/mockups'...
Checking out files: 100% (18/18), done.

Any thoughts?

Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70

1 Answers1

2

gulp-git-streamed is a wrapper around gulp-git which allows you to apply git operations to files in a stream. This makes sense for things like adding and committing files:

return gulp.src('**/*.js')
  .pipe(git.add())
  .pipe(git.commit('Commit all JS files'))

It's kind of pointless for a clone operation however. gulp-git-streamed doesn't actually emit the cloned files into the stream and since it requires an existing stream to begin with you have to artificially create one with a dummy file that you know exists like package.json.

In your case it's probably best to just use gulp-git and node-notifier directly:

var gulp   = require('gulp');
var git    = require('gulp-git');
var notifier = require('node-notifier');

gulp.task('clone', function(done) {
  git.clone('https://gitlab.somedomain.com/mockups/theme.git',
    {args: './src/gitlab/mockups'}, function (err) {
      if (err) {
        notifier.notify(err.message);      
      } else {
        notifier.notify('psd repo cloned!');
      }
      done();
  });
});

gulp.task('dependsOnClone', ['clone'], function(done) {
  console.log('starts executing after clone task has finished');
});
Sven Schoenung
  • 30,224
  • 8
  • 65
  • 70
  • yes I tried that, problem with that approach is that I cannot chain clone task anymore. –  Apr 08 '16 at 10:40
  • meaning tasks that depend on clone run before clone... with wrapper at least this is avoided. –  Apr 08 '16 at 10:40
  • You probably forgot to use a `done()` callback in your implementation. Try mine. Tasks that depend on the `clone` task will start only after the `clone` task has finished. – Sven Schoenung Apr 08 '16 at 10:50
  • yes I didn't have done()! can you in few words tell me what done does here? –  Apr 08 '16 at 11:41
  • 1
    `git.clone()` is asynchronous. That means it returns immediately, but the actual job of cloning the repository hasn't finished yet. There has to be something that tells gulp that the clone operation has completed. Otherwise gulp will just assume that the `clone` task has already finished and start running the next task. That 'something' is the `done()` callback. Only after it has been called does gulp consider the `clone` task to be finished and start executing the next task. – Sven Schoenung Apr 08 '16 at 11:54
  • thank you! would you be willing to take a look at my other issue? http://stackoverflow.com/questions/36500547/gulp-generate-html-file-with-jade-via-markdown-json –  Apr 08 '16 at 13:25