-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
295 lines (262 loc) · 8.88 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* config : {
*
* // VALUE, default: '%MDS%'
* 'value' : '%MDS%',
*
* // REPLACE
* 'replaces' : [
* // if not an array, replace to global value (config.value)
* /#{VERSION}#/g,
* [/#{VERSION_REPlACE}#/g, '%TS%']
* ],
*
* // APPEND
* 'append' : {
*
* // keyword
* 'key' : '_v',
*
* // Whether to overwrite the existing parameters
* // - default: 0 (don't replace)
* 'cover' : 0,
*
* // Append to:ALL('all') or any specific types(ARRAY),
* // others will passing.
* 'to' : [
*
* // (STRING) If this option is a string, apply global replace rules
* 'css',
*
* // (OBJECT) With custom rules to be replaced, the
* // missing items will take the global settings in
* // the completion
* {
* 'type' : 'js',
* 'attr' : ['src', 'custom-src'] // String or Array, undefined this will use default. css: "href", js: ...
* 'key' : '_v',
* 'value' : '%DATE%',
* 'cover' : 1
* },
*
* // (ARRAY) More simple than the object, Just specify
* // the type and value
* ['image', '%TS%']
* },
*
* // Output to config file
* 'output' : {
* 'file' : 'version.json'
* }
* }
*
* --------------------------------------------
* Priority - Covering relations:
*
* (OBJECT)config.append.to[x].type == (ARRAY)config.append.to[x][0] == (STRING)config.append.to[x]
* config.append.to[x].key > config.append.key
* config.append.to[x].cover > config.append.cover
* config.append.to[x].value == config.append.to[x][1] [ (if cover == true) > (else) == config.replace[x][1] ] > config.value
*
* For details, please read the README
*/
'use strict';
var mapStream = require('map-stream');
var fsPath = require('fs-path');
var md5 = require('./lib/md5');
var randomString = require('./lib/randomString');
var leadZero = require('./lib/leadZero');
var parseURL = require('./lib/parseURL');
var renderingURL = require('./lib/renderingURL');
var queryToJson = require('./lib/queryToJson');
var jsonToQuery = require('./lib/jsonToQuery');
function version(v) {
if (typeof v === 'undefined') {
return null;
}
if (v.indexOf('%') > -1) {
v = v.toUpperCase();
}
var DT = new Date();
switch (v) {
case '%DATE%':
v = DT.getFullYear() + leadZero(DT.getMonth() + 1, 2) + leadZero(DT.getDate(), 2);
break;
case '%DT%':
v = DT.getFullYear() + leadZero(DT.getMonth() + 1, 2) + leadZero(DT.getDate(), 2) + leadZero(DT.getHours(), 2) + leadZero(DT.getMinutes(), 2) + leadZero(DT.getSeconds(), 2);
break;
case '%TS%':
v = DT.getTime().toString();
break;
case '%MD5%':
v = md5(DT.getTime().toString());
break;
case '%MDS%':
v = md5(md5(DT.getTime().toString()) + randomString(8));
break;
default:
break;
}
return v;
}
var DETECTION = {
css : /<link[^>]*rel=['"]?stylesheet['"]?[^>]*>/g,
js : /<script [^>]+>[^<]*<\/script>/g,
image: /<img [^>]+>/g,
preload: /<link[^>]*rel=['"]?preload['"]?[^>]*>/g
};
var DEFAULT_ATTR = {
css : 'href',
preload : 'href',
js : 'src',
image: 'src'
}
/**
* options:
* type:
* %DATE% - date
* %DT% - date + time
* %TS% - timestamp length:10
* %TSM% - timestamp(millisecond length:13)
* %MD5% - MD5(timestamp)
* %MDS% - MD5(MD5(timestamp)+salt)
*
* default: %TS%
*/
module.exports = function (options) {
var options = Object.assign({
'value': '%TS%' // default
}, options || {});
var versionNumberList = {
main: version(options.value)
};
function apply_replace(content, config) {
config = config || [];
if (config.length) {
for (var i = 0, len = config.length; i < len; i++) {
var rep, v;
if (Array.isArray(config[i])) {
rep = config[i][0];
v = version(config[i][1]);
if (v === null)
v = versionNumberList.main;
}
else {
rep = config[i];
v = versionNumberList.main;
}
content = content.replace(rep, v);
}
}
return content;
}
function apply_append(content, config) {
var apList = [];
if (config['to']) {
if (config.to === 'all') {
apList = ['css', 'js', 'image'];
}
else {
apList = config.to;
}
if (Array.isArray(apList)) {
var apRule = {};
for (var i = 0, key; i < apList.length; i++) {
if (typeof apList[i] === 'string') {
key = apList[i];
apRule[key] = {
'type': '' + apList[i]
};
}
else if (Object.prototype.toString.call(apList[i]) === '[object Array]') {
if (apList[i].length && apList[i][0]) {
key = apList[i][0];
apRule[key] = {
'type': '' + apList[i][0]
};
apList[i][1] && (apRule[key].value = '' + apList[i][1]);
}
}
else if (Object.prototype.toString.call(apList[i]) === '[object Object]') {
if (apList[i]['type']) {
key = apList[i].type;
apRule[key] = apList[i];
}
}
apRule[key].cover = !!config['cover'] || !!apRule[key].cover;
}
for (var type in apRule) {
!versionNumberList[type] && (versionNumberList[type] = apRule[type]['value'] ? version(apRule[type]['value']) : versionNumberList.main);
content = appendto.call(apRule[type], content, apRule[type]['key'] || config['key'] || '_v', versionNumberList[type]);
}
}
}
return content;
}
function appendto(content, k, v) {
this.attr = this['attr'] ? [].concat(this.attr) : [DEFAULT_ATTR[this.type]];
var sts = content.match(DETECTION[this.type]);
if (Array.isArray(sts) && sts.length && this['files']) {
var files = this.files;
sts = sts.filter(function(element) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (typeof file == 'string') {
if (element.indexOf(file) > -1) {
return true;
}
} else if (file instanceof RegExp) {
if (element.match(file)) {
return true;
}
}
}
return false;
});
}
if (Array.isArray(sts) && sts.length) {
var regExp = new RegExp('(' + this.attr.join('|') + ')' + '=[\'"]?([^>\'"]*)[\'"]?', 'g');
sts.forEach(function (_s) {
var _r = _s;
var _RULE;
while (_RULE = regExp.exec(_s)) {
if (_RULE[2]) {
var _UrlPs = parseURL(_RULE[2]);
var _Query = queryToJson(_UrlPs.query);
var _Append = {};
if (!_Query.hasOwnProperty(k) || this['cover']) {
_Append[k] = v;
}
_UrlPs.query = jsonToQuery(Object.assign(_Query, _Append));
_r = _r.replace(_RULE[2], renderingURL(_UrlPs));
}
}
content = content.replace(_s, _r);
}.bind(this));
}
return content;
}
/**
* output a json version file
*/
if (options.output && options.output.file) {
fsPath.writeFile(options.output.file, JSON.stringify(versionNumberList, null, 4), function (err) {
if (err)
throw err;
console.log('[gulp-version-number] Output to file: ' + options.output.file);
});
}
return mapStream(function (file, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
throw new Error('Streaming not supported');
}
var data = file.contents.toString();
options['replaces'] && ( data = apply_replace(data, options.replaces));
options['append'] && ( data = apply_append(data, options.append));
file.contents = new Buffer(data);
cb(null, file);
});
};