forked from pgilad/gulp-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (74 loc) · 2.49 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var defaults = require('lodash.defaults');
var sitemap = require('./lib/sitemap');
var pluginName = 'gulp-sitemap';
var chalk = require('chalk');
module.exports = function (options) {
var config = defaults(options || {}, {
changefreq: null,
fileName: 'sitemap.xml',
lastmod: null,
mappings: [],
newLine: gutil.linefeed,
priority: null,
spacing: ' ',
verbose: false
});
var entries = [];
var firstFile;
var msg;
if (!config.siteUrl) {
msg = 'siteUrl is a required param';
throw new gutil.PluginError(pluginName, msg);
}
if (options.changeFreq) {
msg = chalk.magenta('changeFreq') + ' has been deprecated. Please use ' + chalk.cyan('changefreq');
gutil.log(pluginName, msg);
config.changefreq = options.changeFreq;
}
if (config.siteUrl.slice(-1) !== '/') {
config.siteUrl = config.siteUrl + '/';
}
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);
}
//skip 404 file
if (/404\.html?$/i.test(file.relative)) {
return callback();
}
if (!firstFile) {
firstFile = file;
}
var mtime = file.stat ? file.stat.mtime : null;
var entry = sitemap.getEntryConfig(file.relative, mtime, config);
entries.push(entry);
callback();
},
function (callback) {
if (!firstFile) {
return callback();
}
var contents = sitemap.prepareSitemap(entries, config);
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();
});
};