-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
47 lines (34 loc) · 1.45 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
var gutil = require('gulp-util');
var through = require('through2');
var toolFactory = require('./tool');
module.exports = function(options) {
options = options || {};
options.patternIndex = options.patternIndex || /^\/index\.[a-f0-9]{8}\.html(\.gz)*$/gi;
var tool = options.tool || toolFactory(options);
var first = true;
return through.obj(function (file, enc, callback) {
if (first) {
options.dirRoot = options.dirRoot || file.base.replace(/\/$/, "");
gutil.log('gulp-cloudfront:', 'Root directory [', options.dirRoot, ']');
first = !first;
}
// Update the default root object once we've found the index.html file
var filename = file.path.substr(options.dirRoot.length);
if (filename.match(options.patternIndex)) {
gutil.log('gulp-cloudfront:', 'Identified index [', filename, ']');
// Trim the '.gz' if gzipped
if (filename.substr(filename.length - 3) === '.gz') {
filename = filename.substr(0, filename.length - 3);
}
tool.updateDefaultRootObject(filename)
.then(function() {
return callback(null, file);
}, function(err) {
gutil.log(new gutil.PluginError('gulp-cloudfront', err));
callback(null, file);
});
} else {
return callback(null, file);
}
});
};