This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (87 loc) · 3.33 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
'use-strict';
var path = require('path');
var logger = require('fancy-log')
var through = require('through2');
var vfile = require('vinyl-file');
var revHash = require('rev-hash');
var modifyFilename = require('modify-filename');
var glob = require('glob');
function transformFilename(file, lastVersionFile) {
file.versionStamp = getVersionStamp(lastVersionFile);
file.path = modifyFilename(file.path, function (filename, extension) {
var extIndex = filename.indexOf('.');
filename = extIndex === -1
? filename + '.' + file.versionStamp
: filename.slice(0, extIndex) + '.' + file.versionStamp + filename.slice(extIndex);
return filename + extension;
});
}
function getVersionStamp(file) {
var currentDatespamp = getUTCDatestamp();
if (file == null) {
return currentDatespamp + '-' + '00';
}
var filename = path.basename(file.path);
var stamp = filename.slice(filename.indexOf('.')+1);
stamp = stamp.slice(0, stamp.indexOf('.'));
stamp = stamp.split('-');
if (stamp[0] == currentDatespamp) {
return currentDatespamp + '-' + ('0'+(parseInt(stamp[1])+1)).slice(-2);
} else {
return currentDatespamp + '-' + '00';
}
}
function getLastVersionFile(file, destination) {
const baseFilename = path.basename(file.path);
const extIndex = baseFilename.indexOf('.');
const fileMask = extIndex === -1
? baseFilename + '.????????-??'
: baseFilename.slice(0, extIndex) + '.????????-??' + baseFilename.slice(extIndex);
const versionFiles = glob.sync(path.join(destination, fileMask)).sort();
return versionFiles[versionFiles.length-1];
}
function isNeedNewVersion(currentFile, lastVersionFile) {
return lastVersionFile == null || revHash(currentFile.contents) != revHash(lastVersionFile.contents);
}
function getUTCDatestamp() {
var date = new Date();
return date.getUTCFullYear() + ('0' + (date.getUTCMonth() + 1)).slice(-2) + ('0' + date.getUTCDate()).slice(-2);
}
var plugin = function (destination, options) {
var sourcemaps = [];
options = options || {};
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new Error('gulp-date-rev: Streaming not supported'));
return;
}
if (path.extname(file.path) === '.map') {
sourcemaps.push(file);
cb();
return;
}
var lastVersionFilename = getLastVersionFile(file, destination);
var lastVersionFile = lastVersionFilename ? vfile.readSync(lastVersionFilename) : null;
if (isNeedNewVersion(file, lastVersionFile)) {
transformFilename(file, lastVersionFile);
logger('gulp-date-rev:', 'Created new version:', path.basename(file.path));
if (options.callback != undefined && typeof(options.callback) === 'function') {
options.callback(file.versionStamp);
}
cb(null, file);
} else {
logger('gulp-date-rev:', 'Version of file', path.basename(lastVersionFilename), 'is actual');
if (options.callback != undefined && typeof(options.callback) === 'function') {
var currentVersion = lastVersionFilename.slice(lastVersionFilename.indexOf('.')+1);
currentVersion = currentVersion.slice(0, currentVersion.indexOf('.'));
options.callback(currentVersion);
}
cb(null, options.continue ? file : null);
}
});
};
module.exports = plugin;