eunomia-controller
Version:
71 lines (65 loc) • 2.64 kB
JavaScript
;
var fs = require('fs');
var ejs = require('ejs');
var common = require('./../../common');
var eunomia = require('./../../variables')('./eunomia.json');
exports.generate = function (publicPath) {
return require('./../../task/index')('sitemap', function (resolve, reject) {
// If the eunomia.json file does not exist
if (!fs.existsSync('./eunomia.json')) {
reject("Could not find the `eunomia.json` file in your project root");
} else {
// Broken eunomia json structure
if (typeof eunomia === 'undefined' || eunomia === null) {
reject("The `eunomia.json` contains broken JSON");
return false;
}
// Missing generate structure within your json structure
if (typeof eunomia.generate === 'undefined') {
reject("Missing a top-level `generate` field within the eunomia.json file");
return false;
}
// Missing sitemap structure within your generate json object
if (typeof eunomia.generate.sitemap === 'undefined') {
reject("Missing sitemap json object within the generate json object");
return false;
}
// If the module is disabled
if (typeof eunomia.generate.sitemap.enabled !== 'undefined') {
if (!eunomia.generate.sitemap.enabled) {
resolve();
return false;
}
}
// If the sitemap routes object are undefined
if (typeof eunomia.generate.sitemap.routes === 'undefined') {
reject("The eunomia json structure requires a generate.sitemap.routes json object");
} else {
// If the routes file does not exist
if (!fs.existsSync(eunomia.generate.sitemap.routes)) {
reject("file " + eunomia.generate.sitemap.routes + " does not exists");
} else {
try {
var routes = JSON.parse(fs.readFileSync(eunomia.generate.sitemap.routes, 'utf8'));
var fullPath = __dirname + '/sitemap.ejs';
var template = fs.readFileSync(fullPath, 'utf8');
var render = ejs.compile(template, { filename: fullPath });
var urls = routes.filter(function (url) {
return !url.path.includes(':');
}).map(function (url) {
return {
loc: url.path
};
});
var output = render({ config: eunomia.generate.sitemap, urls: urls });
fs.writeFileSync(publicPath + '/sitemap.xml', output, 'utf8');
resolve();
} catch (e) {
console.log(e);
reject("Failed to create a sitemap.txt");
}
}
}
}
});
};