-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·56 lines (52 loc) · 1.74 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var defaults = require('lodash.defaults');
var pluginName = 'gulp-filelist-json';
module.exports = function (options) {
var config = defaults(options || {}, {
filename: 'filelist.json',
verbose: false,
compact: true
});
var entries = [];
var firstFile;
var msg;
return through.obj(function (file, enc, callback) {
//we handle null files (that have no contents), but not dirs
if (file.isDirectory()) {
return callback(null, file);
}
if (file.isStream()) {
msg = 'Streaming not supported';
return callback(new gutil.PluginError(pluginName), msg);
}
if (!firstFile) {
firstFile = file;
}
var mtime = file.stat ? file.stat.mtime : null;
var entry = file.relative;
entries.push(entry)
callback();
},
function (callback) {
if (!firstFile) {
return callback();
}
var space = config.compact ? '' : ' ';
var contents = JSON.stringify(entries, null, space);
if (options.verbose) {
msg = 'Files in sitemap: ' + entries.length;
gutil.log(pluginName, msg);
}
//create and push new vinyl file for sitemap
this.push(new gutil.File({
cwd: firstFile.cwd,
base: firstFile.cwd,
path: path.join(firstFile.cwd, config.filename),
contents: new Buffer(contents)
}));
callback();
});
};