made feeds, blog and sitemap be created on script startup too

This commit is contained in:
Ellpeck 2019-10-10 12:04:08 +02:00
parent 61b5707057
commit 7731646970
3 changed files with 104 additions and 86 deletions

View file

@ -14,9 +14,16 @@ const converter = new showdown.Converter({
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
createBlogPages(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
createBlogPages(folder);
});
}
function createBlogPages(folder) {
console.log("Refreshing blog sub-sites..."); console.log("Refreshing blog sub-sites...");
fs.readFile(folder + "index.html", function (_, data) { fs.readFile(folder + "index.html", function (_, data) {
@ -68,5 +75,4 @@ module.exports = function () {
} }
}); });
}); });
});
} }

View file

@ -13,16 +13,22 @@ const converter = new showdown.Converter({
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
createFeeds(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
createFeeds(folder);
});
}
function createFeeds(folder) {
console.log("Refreshing feeds..."); console.log("Refreshing feeds...");
createFeed(function (feed) { createFeed(function (feed) {
fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {}); fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {});
fs.writeFile(folder + "rss.xml", feed.rss2(), function (_, _) {}); fs.writeFile(folder + "rss.xml", feed.rss2(), function (_, _) {});
fs.writeFile(folder + "atom.xml", feed.atom1(), function (_, _) {}); fs.writeFile(folder + "atom.xml", feed.atom1(), function (_, _) {});
}); });
});
} }
function createFeed(callback) { function createFeed(callback) {

View file

@ -5,9 +5,16 @@ const fs = require("fs");
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
refreshSitemap(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
refreshSitemap(folder);
});
}
function refreshSitemap(folder) {
console.log("Refreshing sitemap..."); console.log("Refreshing sitemap...");
let sitemap = createSitemap({ let sitemap = createSitemap({
@ -48,5 +55,4 @@ module.exports = function () {
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {}); fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
}); });
});
} }