forked from Dragory/gulp-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (141 loc) · 4.17 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var crypto = require('crypto'),
through2 = require('through2'),
gutil = require('gulp-util'),
assign = require('lodash.assign'),
template = require('lodash.template'),
path = require('path'),
Promise = require('es6-promise').Promise,
fs = require('fs'),
applySourceMap = require('vinyl-sourcemaps-apply');
var exportObj = function(options) {
options = assign({}, {
algorithm: 'sha1',
hashLength: 8,
template: '<%= name %>-<%= hash %><%= ext %>',
version: ''
}, options);
return through2.obj(function(file, enc, cb) {
// generate source maps if plugin source-map present
console.log("file Has sourcemap: " + file.sourceMap);
console.log("file Has map: " + file.map);
if (file.sourceMap) {
options.makeSourceMaps = true;
}
if (file.isDirectory()) {
this.push(file);
cb();
return;
}
var fileExt = path.extname(file.relative),
fileName = path.basename(file.relative, fileExt);
var hasher = crypto.createHash(options.algorithm);
var piped = file.pipe(through2(
function(chunk, enc, updateCb) {
hasher.update(chunk);
updateCb(null, chunk);
},
function(flushCb) {
if (options.version !== '') hasher.update(String(options.version));
file.hash = hasher.digest('hex').slice(0, options.hashLength);
file.origPath = file.relative;
file.path = path.join(path.dirname(file.path), template(options.template, {
hash: file.hash,
name: fileName,
ext: fileExt
}));
// apply source map to the chain
//console.log(file);
if (file.sourceMap) {
console.log("file has sourceMap");
applySourceMap(file, file.sourceMap);
}
this.push(file);
cb();
flushCb();
}.bind(this)
));
if (file.isStream()) {
var newContents = through2();
piped.pipe(newContents);
file.contents = newContents;
}
});
};
var origManifestContents = {};
var appendQueue = Promise.resolve();
// Normalizes a path for the manifest file (i.e. backslashes -> slashes)
function formatManifestPath(mPath) {
return path.normalize(mPath).replace(/\\/g, '/');
}
exportObj.manifest = function(manifestPath, options) {
var space = null;
var append = true;
var sourceDir = __dirname;
var deleteOld = false;
if (arguments.length === 2 && typeof options === 'object') {
// New signature
if (options.append != null) append = options.append;
if (options.space != null) space = options.space;
if (options.sourceDir) sourceDir = options.sourceDir;
deleteOld = !!options.deleteOld;
} else {
// Old signature
if (arguments[1] != null) append = arguments[1];
if (arguments[2] != null) space = arguments[2];
}
var newManifest = {};
if (append && ! origManifestContents[manifestPath]) {
try {
var content = fs.readFileSync(manifestPath, {encoding: 'utf8'});
origManifestContents[manifestPath] = JSON.parse(content);
} catch (e) {
origManifestContents[manifestPath] = {};
}
}
function deleteOldFiles(oldFiles, newFiles, dirPath) {
for (var prop in oldFiles) {
if (newFiles.hasOwnProperty(prop) === false || oldFiles[prop] !== newFiles[prop]) {
try {
fs.unlinkSync(path.join(dirPath, oldFiles[prop]));
} catch (e) {
console.warn(e.message);
}
}
}
}
return through2.obj(
function(file, enc, cb) {
if (typeof file.origPath !== 'undefined') {
var manifestSrc = formatManifestPath(file.origPath);
var manifestDest = formatManifestPath(file.relative);
newManifest[manifestSrc] = manifestDest;
}
cb();
},
function(cb) {
var finish = function (data) {
if (deleteOld) {
deleteOldFiles(origManifestContents[manifestPath], data, sourceDir);
}
origManifestContents[manifestPath] = data;
this.push(new gutil.File({
path: manifestPath,
contents: new Buffer(JSON.stringify(origManifestContents[manifestPath], undefined, space))
}));
cb();
}.bind(this);
if (append) {
appendQueue.then(new Promise(function(resolve) {
finish(assign({}, origManifestContents[manifestPath], newManifest));
resolve();
}));
} else {
finish(newManifest);
}
}
);
};
exportObj.resetManifestCache = function() {
origManifestContents = {};
};
module.exports = exportObj;