forked from smysnk/gulp-rev-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (84 loc) · 2.79 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
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var toolFactory = require('./tool');
var through = require('through2');
var chalk = require('chalk');
var gutil = require('gulp-util');
var plugin = function (options) {
options = options || {};
options.hashLength = options.hashLength || 8;
options.ignore = options.ignore || options.ignoredExtensions || [ /^\/favicon.ico$/g ];
var tool = new toolFactory(options);
return through.obj(function (file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
} else if (file.isStream()) {
throw new Error('Streams are not supported!');
}
tool.revisionFile(file);
callback(null, file);
}, function (cb) {
if(options.getTool) {
options.getTool(tool);
}
cb();
});
};
plugin.versionFile = function(options) {
var options = options || {};
var tool = new toolFactory();
var hash = '';
var firstFile = null;
var fileName = options.fileName || 'version.json';
return through.obj(function (file, enc, cb) {
// ignore all non-rev'd files
if (file.path && file.revOrigPath) {
firstFile = firstFile || file;
hash = tool.md5(hash + file.path);
}
cb();
}, function (cb) {
var out = {
hash: hash,
timestamp: new Date()
};
if (firstFile) {
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, fileName),
contents: new Buffer(JSON.stringify(out, null, ' '))
}));
}
cb();
});
};
// Borrowed from: https://github.com/sindresorhus/gulp-rev
plugin.manifest = function (options) {
var options = options || {};
var manifest = {};
var firstFile = null;
var tool = new toolFactory();
var fileName = options.fileName || 'rev-manifest.json';
var prefix = options.prefix || '';
return through.obj(function (file, enc, cb) {
// ignore all non-rev'd files
if (file.path && file.revOrigPath) {
firstFile = firstFile || file;
manifest[tool.getRelativeFilename(file.revOrigBase, file.revOrigPath, true)] = prefix + tool.getRelativeFilename(file.base, file.path, true);
}
cb();
}, function (cb) {
if (firstFile) {
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, fileName),
contents: new Buffer(JSON.stringify(manifest, null, ' '))
}));
}
cb();
});
};
module.exports = plugin;