Skip to content

Instantly share code, notes, and snippets.

@idflood
Forked from lukin0110/gulpfile-git-version.js
Created October 25, 2017 06:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save idflood/2ac2255320b0ed389b3ab627533ee674 to your computer and use it in GitHub Desktop.
Gulp git version, fetches the latest git commit hash. Can be used as version for your application. Inject/replace it in your html
'use strict';
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var GIT_VERSION = "na";
gulp.task('version', function(cb){
//git --git-dir=.git log --pretty='%ct %h' -1
//git --git-dir=.git log --pretty='%h' -1
var child = spawn("git", ["--git-dir=.git", "log", "--pretty=%h", "-1"], {cwd: process.cwd()}),
stdout = '',
stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
});
child.on('close', function(code) {
var normalized = stdout.replace(/(?:\r\n|\r|\n)/g, "")
console.log("Version: " + normalized);
GIT_VERSION = normalized;
cb();
});
});
@idflood
Copy link
Author

idflood commented Oct 25, 2017

Added the callback (cb) and called it on close. Otherwise the task ordering may be wrong, and a task requiring this could have the GIT_VERSION not defined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment