eunomia-controller
Version:
69 lines (64 loc) • 2.44 kB
JavaScript
const fs = require(`fs`);
const ejs = require(`ejs`);
const common = require('./../../common')
const eunomia = require(`./../../variables`)(`./eunomia.json`);
exports.generate = (publicPath) => require(`./../../task/index`)(`sitemap`, (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 {
const routes = JSON.parse(fs.readFileSync(eunomia.generate.sitemap.routes, `utf8`));
const fullPath = `${__dirname}/sitemap.ejs`
const template = fs.readFileSync(fullPath, `utf8`);
const render = ejs.compile(template, { filename: fullPath });
const urls = routes
.filter(url => !url.path.includes(`:`))
.map(url => ({
loc: url.path
}));
const output = render({config: eunomia.generate.sitemap, urls});
fs.writeFileSync(`${publicPath}/sitemap.xml`, output, `utf8`);
resolve()
}
catch (e) {
console.log(e)
reject("Failed to create a sitemap.txt")
}
}
}
}
});