forked from mikaelbr/gulp-gitshasuffix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (99 loc) · 2.73 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var through = require('through2');
var gutil = require('gulp-util');
var path = require('path');
var fs = require('fs');
var git = require('./lib/git');
module.exports = function (options) {
'use strict';
var sha = null;
options = options || {};
var length = options.length || 6;
var separator = options.separator || '-';
var trimRelatedPath = options.trimRelatedPath || '';
var manifest = '';
var manifestData = {};
if (options.manifest) {
manifest = process.cwd() + '/' + options.manifest;
} else {
manifest = process.cwd() + '/./manifest.json';
}
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 += finalSuffix;
stream.push(file);
return callback();
}
if (folder) {
dir = path.join(dir, shaSuffix);
finalSuffix = '';
}
file.revOrigPath = file.path;
file.revOrigBase = file.base;
var relativePathKey = path.relative(process.cwd(), file.path);
file.path = path.join(dir, firstname + finalSuffix + ext);
var relativePathValue = path.relative(process.cwd(), file.path);
if (trimRelatedPath) {
relativePathKey = path.relative(trimRelatedPath, relativePathKey);
relativePathValue = path.relative(trimRelatedPath, relativePathValue);
}
manifestData[ relativePathKey ] = relativePathValue;
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();
});
};
function saveManifestFile (manifestPath, data, callback) {
if (manifestPath) {
try {
fs.statSync(manifestPath);
} catch (error) {
fs.writeFileSync(manifestPath, '');
}
var readedJson;
try {
readedJson = fs.readFileSync(manifestPath, 'utf8');
} catch (error) {
}
if (readedJson) {
readedJson = JSON.parse(readedJson);
data = JSON.parse(data);
Object.assign(readedJson, data);
readedJson = JSON.stringify(readedJson);
fs.writeFileSync(manifestPath, readedJson);
callback();
} else {
fs.writeFileSync(manifestPath, data);
callback();
}
} else {
callback();
}
}
return through.obj(gitshasuffix, function (callback) {
sha = null;
if (options.manifest) {
return saveManifestFile(manifest, JSON.stringify(manifestData), callback);
} else {
return callback();
}
});
};