-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
62 lines (52 loc) · 1.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var through = require("through2"),
gutil = require("gulp-util"),
path = require("path"),
git = require("./lib/git");
module.exports = function (options) {
"use strict";
var sha = null;
options = options || {};
var length = options.length || 6;
var separator = options.separator || options.seperator || "-";
var folder = !!options.folder;
var gitshasuffix = function (file, enc, callback) {
var stream = this,
dir = path.dirname(file.path),
ext = path.extname(file.relative),
firstname = path.basename(file.relative, ext),
finalname = null;
var appendSuffix = function () {
var shaSuffix = sha.substring(0, length),
finalSuffix = separator + shaSuffix;
if (file.isNull()) {
file.path = file.path + finalSuffix;
stream.push(file);
return callback();
}
if (folder) {
dir = path.join(dir, shaSuffix);
finalSuffix = '';
}
file.revOrigPath = file.path;
file.revOrigBase = file.base;
file.path = path.join(dir, firstname + finalSuffix + ext);
stream.push(file);
return callback();
};
if (!!sha) {
return appendSuffix();
}
git.getLatestSha(function (err, latestSha) {
if (err) {
stream.emit('error', new gutil.PluginError('gulp-gitshasuffix', err));
return callback();
}
sha = latestSha;
appendSuffix();
});
}
return through.obj(gitshasuffix, function (callback) {
sha = null;
return callback();
});
};