forked from bleech/gulp-rev-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (52 loc) · 1.85 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
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var tools = require('./tools');
var through = require('through2');
var chalk = require('chalk');
var gutil = require('gulp-util');
module.exports = function(options) {
options = options || {};
var first = true
, ignoreExtensions = [].concat(options.ignoreExtensions)
;
return through.obj(function (file, enc, callback) {
if (options.rootDir === undefined) options.rootDir = file.base;
if (first) {
gutil.log('gulp-rev-all:', 'Root directory [', options.rootDir, ']');
first = !first;
}
if (file.isNull()) {
callback(null, file);
return;
} else if (file.isStream()) {
throw new Error('Streams are not supported!');
callback(null, file);
return;
}
// Only process references in these types of files, otherwise we'll corrupt images etc
switch(path.extname(file.path)) {
case '.js':
case '.css':
case '.html':
case '.php':
tools.revReferencesInFile(file, options.rootDir, ignoreExtensions);
}
if (ignoreExtensions.indexOf(path.extname(file.path)) === -1) {
var filenameReved = path.basename(tools.revFile(file.path));
var base = path.dirname(file.path);
var oldPath = file.path;
file.path = path.join(base, filenameReved);
if (options.remove) {
fs.unlink(oldPath, function(err) {
if (err) {
gutil.log('gulp-rev-all:', 'Error deleting file [', oldPath, ']', err);
} else {
gutil.log('gulp-rev-all:', 'Deleted file [', oldPath, ']');
}
});
}
}
callback(null, file);
});
};