gulp-sitemap
Advanced tools
Comparing version 1.0.1 to 1.0.2
65
index.js
@@ -9,42 +9,32 @@ 'use strict'; | ||
var sitemap = require('./lib/sitemap'); | ||
var pluginName = 'gulp-sitemap'; | ||
module.exports = function (params) { | ||
params = params || {}; | ||
if (!params.siteUrl) { | ||
throw new gutil.PluginError(pluginName, 'siteUrl is a required param'); | ||
} | ||
var config = defaults(params, { | ||
//set newline separator | ||
module.exports = function(params) { | ||
var config = defaults(params || {}, { | ||
newLine: gutil.linefeed, | ||
//default output filename | ||
fileName: 'sitemap.xml', | ||
//set default change frequency | ||
changeFreq: 'daily', | ||
//set xml spacing. can be \t for tabs | ||
spacing: ' ', | ||
//set default priority | ||
priority: '0.5' | ||
}); | ||
//enforce priority to be a string | ||
if (!config.siteUrl) { | ||
throw new gutil.PluginError(pluginName, 'siteUrl is a required param'); | ||
} | ||
config.priority = config.priority.toString(); | ||
//ensure siteUrl ends with a slash | ||
if (config.siteUrl.slice(-1) !== '/') { | ||
config.siteUrl += '/'; | ||
} | ||
//first file to capture cwd | ||
var firstFile, entries = []; | ||
var entries = []; | ||
var firstFile; | ||
return through.obj(function (file, enc, cb) { | ||
return through.obj(function(file, enc, cb) { | ||
//we handle null files (that have no contents), but not dirs | ||
if (file.isDirectory()) { | ||
this.push(file); | ||
return cb(); | ||
cb(file); | ||
return; | ||
} | ||
//we don't handle streams for now | ||
if (file.isStream()) { | ||
this.emit('error', new gutil.PluginError(pluginName, 'Streaming not supported')); | ||
return cb(); | ||
cb(new gutil.PluginError(pluginName, 'Streaming not supported')); | ||
return; | ||
} | ||
@@ -54,36 +44,35 @@ | ||
if (/404\.html?$/i.test(file.relative)) { | ||
return cb(); | ||
cb(); | ||
return; | ||
} | ||
//assign first file to get relative cwd/path | ||
if (!firstFile) { | ||
firstFile = file; | ||
} | ||
firstFile = firstFile || file; | ||
//if file has stat.mtime use it | ||
if (file.stat && file.stat.mtime) { | ||
entries = entries.concat(sitemap.processFile(file.relative, file.stat.mtime, config)); | ||
return cb(); | ||
cb(); | ||
return; | ||
} | ||
//otherwise get it from file using fs | ||
fs.stat(file.path, function (err, stats) { | ||
fs.stat(file.path, function(err, stats) { | ||
if (err || !stats || !stats.mtime) { | ||
//file not found - skip it | ||
if (err.code === 'ENOENT') { | ||
return cb(); | ||
cb(); | ||
return; | ||
} | ||
err = err || 'No stats found for file ' + file.path; | ||
this.emit('error', new gutil.PluginError(pluginName, err)); | ||
return cb(); | ||
cb(new gutil.PluginError(pluginName, err)); | ||
return; | ||
} | ||
//add file to xml | ||
entries = entries.concat(sitemap.processFile(file.relative, stats.mtime, config)); | ||
return cb(); | ||
cb(); | ||
}.bind(this)); | ||
}, | ||
function (cb) { | ||
function(cb) { | ||
if (!firstFile) { | ||
//no files | ||
return cb(); | ||
cb(); | ||
return; | ||
} | ||
@@ -98,4 +87,4 @@ //create and push new vinyl file for sitemap | ||
return cb(); | ||
cb(); | ||
}); | ||
}; |
@@ -5,3 +5,2 @@ 'use strict'; | ||
//TODO: export this to an external module | ||
var xmlHeader = [ | ||
@@ -13,3 +12,2 @@ '<?xml version="1.0" encoding="UTF-8"?>', | ||
var prepareSitemap = function (entries, config) { | ||
//insert xml heaader | ||
entries = xmlHeader.concat(entries).concat(['</urlset>']); | ||
@@ -16,0 +14,0 @@ return entries.join(config.newLine).toString(); |
{ | ||
"name": "gulp-sitemap", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Generate a search engine friendly sitemap.xml using a Gulp stream", | ||
@@ -5,0 +5,0 @@ "repository": "pgilad/gulp-sitemap", |
@@ -39,2 +39,21 @@ # [gulp](https://github.com/wearefractal/gulp)-sitemap | ||
Let's see an example of how we can create and output a sitemap, and then return to the original stream files: | ||
```js | ||
var gulp = require('gulp'); | ||
var sitemap = require('gulp-sitemap'); | ||
var save = require('gulp-save'); | ||
gulp.task('html', function() { | ||
gulp.src('*.html') | ||
.pipe(save('before-sitemap')) | ||
.pipe(sitemap({ | ||
siteUrl: 'http://www.amazon.com' | ||
})) // Returns sitemap.xml | ||
.pipe(gulp.dest('./dist')) | ||
.pipe(save.restore('before-sitemap')) //restore all files to the state when we cached them | ||
// -> continue stream with original html files | ||
// ... | ||
}); | ||
``` | ||
## Options | ||
@@ -41,0 +60,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9793
140
119