Gulp - Get the return value of one task from another

My goal is to add the latest git command to my file index.html

.

The following task successfully returns the latest git hash (using gulp-git ):

var git = require('gulp-git');
gulp.task('hash', function() {
   git.revParse({args:'--short HEAD'}, function (err, hash) {
     return hash;
   });
});

      

The following task creates my HTML:

var inject = require('inject-string');
gulp.task('html', function () {
  return gulp.src('app/index.html')
    .pipe(inject.append('append git hash here!'))
    .pipe(gulp.dest('dist'))
});

      

This adds the row to successfully index.html

, but how do I insert the task return value hash

into html

?

+3


source to share


1 answer


Of course, you can add a callback method to your hash task so that you can store the result into a variable to use in your html task. The html task must also have a hash task as a dependency, so the hash is never undefined. Also, you should use something like gulp-cheerio to inject the hash into the output file so that you don't add the hash outside the closing html tag.



var gulp = require('gulp'),
    git  = require('gulp-git'),
    cheerio = require('gulp-cheerio');

var gitHash;

gulp.task('hash', function(cb) {
  return git.revParse({args:'--short HEAD'}, function(err, hash) {
     gitHash = hash;
     cb();
   });
});

gulp.task('html', ['hash'], function() {
  return gulp.src('app/index.html')
    .pipe(cheerio(function($) {
        $('body').append('<p>' + gitHash + '</p>');
    }))
    .pipe(gulp.dest('dist'));
});

      

+5


source







All Articles