X-Git-Url: https://git.r.bdr.sh/rbdr/blog/blobdiff_plain/5e265f9d81bcce30949e88850892ea5dacec7386..d92ac8ccf6516011bc40bff2b17ef0d0d766f9de:/lib/blog.js?ds=inline diff --git a/lib/blog.js b/lib/blog.js index 483577f..9024a00 100644 --- a/lib/blog.js +++ b/lib/blog.js @@ -1,27 +1,20 @@ 'use strict'; -const Fs = require('fs'); -const Markdown = require('markdown'); -const Mustache = require('mustache'); -const Ncp = require('ncp'); -const Path = require('path'); -const Rimraf = require('rimraf'); -const Util = require('util'); +const { exec } = require('child_process'); +const { access, mkdir, readdir, readFile, rmdir, writeFile } = require('fs/promises'); +const { template } = require('dot'); +const { ncp } = require('ncp'); +const { join } = require('path'); +const Marked = require('marked'); +const { debuglog, promisify } = require('util'); const internals = { // Promisified functions + ncp: promisify(ncp), - fs: { - access: Util.promisify(Fs.access), - mkdir: Util.promisify(Fs.mkdir), - readdir: Util.promisify(Fs.readdir), - readFile: Util.promisify(Fs.readFile), - writeFile: Util.promisify(Fs.writeFile) - }, - ncp: Util.promisify(Ncp.ncp), - rimraf: Util.promisify(Rimraf), - debuglog: Util.debuglog('blog'), + exec: promisify(exec), + debuglog: debuglog('blog'), // constants @@ -29,6 +22,7 @@ const internals = { kIndexName: 'index.html', kFileNotFoundError: 'ENOENT', kMarkdownRe: /\.md$/i, + kRemoveCommand: 'rm -rf', // Strings @@ -93,47 +87,61 @@ module.exports = class Blog { * @return {Promise} empty promise, returns no value * @instance */ - async publish() { + publish() { console.error('Publishing not yet implemented'); + return Promise.resolve(); } // Parses markdown for each page, copies assets and generates index. async _generate() { - const assetsTarget = Path.join(this.staticDirectory, internals.kAssetsDirectoryName); - const indexTarget = Path.join(this.staticDirectory, internals.kIndexName); - const indexLocation = Path.join(this.templatesDirectory, internals.kIndexName); + const assetsTarget = join(this.staticDirectory, internals.kAssetsDirectoryName); + const indexTarget = join(this.staticDirectory, internals.kIndexName); + const indexLocation = join(this.templatesDirectory, internals.kIndexName); const posts = []; internals.debuglog(`Removing ${assetsTarget}`); - await internals.rimraf(assetsTarget); + await rmdir(assetsTarget, { recursive: true }); for (let i = 0; i < this.maxPosts; ++i) { - const sourcePath = Path.join(this.postsDirectory, `${i}`); - const assetsSource = Path.join(sourcePath, internals.kAssetsDirectoryName); - const postContentPath = await this._findBlogContent(sourcePath); + const sourcePath = join(this.postsDirectory, `${i}`); - internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); - await internals.ncp(assetsSource, assetsTarget); + try { + await access(this.postsDirectory); + + const assetsSource = join(sourcePath, internals.kAssetsDirectoryName); + const postContentPath = await this._findBlogContent(sourcePath); + + internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); + await internals.ncp(assetsSource, assetsTarget); - internals.debuglog(`Reading ${postContentPath}`); - const postContent = await internals.fs.readFile(postContentPath, { encoding: 'utf8' }); + internals.debuglog(`Reading ${postContentPath}`); + const postContent = await readFile(postContentPath, { encoding: 'utf8' }); - internals.debuglog('Parsing markdown'); - posts.push({ - html: Markdown.markdown.toHTML(postContent), - id: i + 1 - }); + internals.debuglog('Parsing markdown'); + posts.push({ + html: Marked(postContent), + id: i + 1 + }); + } + catch (error) { + if (error.code === internals.kFileNotFoundError) { + internals.debuglog(`Skipping ${i}`); + continue; + } + + throw error; + } } internals.debuglog(`Reading ${indexLocation}`); - const indexTemplate = await internals.fs.readFile(indexLocation, { encoding: 'utf8' }); + const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' }); internals.debuglog('Generating HTML'); - const indexHtml = Mustache.render(indexTemplate, { posts }); - await internals.fs.writeFile(indexTarget, indexHtml); + const indexHtml = template(indexTemplate)({ posts }); + await writeFile(indexTarget, indexHtml); } // Shift the posts, delete any remainder. @@ -143,14 +151,14 @@ module.exports = class Blog { await this._ensurePostsDirectoryExists(); for (let i = this.maxPosts - 1; i > 0; --i) { - const targetPath = Path.join(this.postsDirectory, `${i}`); - const sourcePath = Path.join(this.postsDirectory, `${i - 1}`); + const targetPath = join(this.postsDirectory, `${i}`); + const sourcePath = join(this.postsDirectory, `${i - 1}`); try { - await internals.fs.access(sourcePath); + await access(sourcePath); internals.debuglog(`Removing ${targetPath}`); - await internals.rimraf(targetPath); + await rmdir(targetPath, { recursive: true }); internals.debuglog(`Shifting blog post ${sourcePath} to ${targetPath}`); await internals.ncp(sourcePath, targetPath); @@ -172,10 +180,10 @@ module.exports = class Blog { await this._ensurePostsDirectoryExists(); - const targetPath = Path.join(this.postsDirectory, '0'); + const targetPath = join(this.postsDirectory, '0'); internals.debuglog(`Removing ${targetPath}`); - await internals.rimraf(targetPath); + await rmdir(targetPath, { recursive: true }); internals.debuglog(`Adding ${postLocation} to ${targetPath}`); await internals.ncp(postLocation, targetPath); @@ -187,12 +195,12 @@ module.exports = class Blog { internals.debuglog(`Checking if ${this.postsDirectory} exists.`); try { - await internals.fs.access(this.postsDirectory); + await access(this.postsDirectory); } catch (error) { if (error.code === internals.kFileNotFoundError) { internals.debuglog('Creating posts directory'); - await internals.fs.mkdir(this.postsDirectory); + await mkdir(this.postsDirectory); return; } @@ -204,11 +212,11 @@ module.exports = class Blog { async _findBlogContent(directory) { - const entries = await internals.fs.readdir(directory); + const entries = await readdir(directory); const markdownEntries = entries .filter((entry) => internals.kMarkdownRe.test(entry)) - .map((entry) => Path.join(directory, entry)); + .map((entry) => join(directory, entry)); if (markdownEntries.length > 0) { internals.debuglog(`Found markdown file: ${markdownEntries[0]}`);