diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-02-14 16:58:56 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-02-14 16:58:56 +0100 |
| commit | 6cd62e795e3716aa0cbd2d1ff8c1b3a345803563 (patch) | |
| tree | f35cfd157529d890d8d5e8e4280c9d44d77a5e01 /lib/generators | |
| parent | 02f408c24d82d1fac4e55c146c4fa57cbdcdeca4 (diff) | |
Use modules, use XDG dirs
Diffstat (limited to 'lib/generators')
| -rw-r--r-- | lib/generators/html.js | 14 | ||||
| -rw-r--r-- | lib/generators/rss.js | 16 | ||||
| -rw-r--r-- | lib/generators/static.js | 50 | ||||
| -rw-r--r-- | lib/generators/txt.js | 15 |
4 files changed, 47 insertions, 48 deletions
diff --git a/lib/generators/html.js b/lib/generators/html.js index 9579cb2..f7c7966 100644 --- a/lib/generators/html.js +++ b/lib/generators/html.js @@ -1,9 +1,9 @@ 'use strict'; -const { template, templateSettings } = require('dot'); -const { readFile, writeFile } = require('fs/promises'); -const { join } = require('path'); -const { debuglog } = require('util'); +import Dot from 'dot'; +import { readFile, writeFile } from 'fs/promises'; +import { join } from 'path'; +import { debuglog } from 'util'; const internals = { debuglog: debuglog('blog'), @@ -19,7 +19,7 @@ const internals = { * @param {string} target the target directory * @param {Array.<Blog.tPost>} posts the list of posts */ -module.exports = async function HTMLGenerator(source, target, posts) { +export default async function HTMLGenerator(source, target, posts) { internals.debuglog('Generating HTML'); const indexTarget = join(target, internals.kIndexName); @@ -29,8 +29,8 @@ module.exports = async function HTMLGenerator(source, target, posts) { const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' }); internals.debuglog('Writing HTML'); - const indexHtml = template(indexTemplate, { - ...templateSettings, + const indexHtml = Dot.template(indexTemplate, { + ...Dot.templateSettings, strip: false })({ posts }); await writeFile(indexTarget, indexHtml); diff --git a/lib/generators/rss.js b/lib/generators/rss.js index 4ae3925..a46a394 100644 --- a/lib/generators/rss.js +++ b/lib/generators/rss.js @@ -1,10 +1,10 @@ 'use strict'; -const { template, templateSettings } = require('dot'); -const { encodeXML } = require('entities'); -const { readFile, writeFile } = require('fs/promises'); -const { join } = require('path'); -const { debuglog } = require('util'); +import Dot from 'dot'; +import { encodeXML } from 'entities'; +import { readFile, writeFile } from 'fs/promises'; +import { join } from 'path'; +import { debuglog } from 'util'; const internals = { debuglog: debuglog('blog'), @@ -29,7 +29,7 @@ const internals = { * @param {string} target the target directory * @param {Array.<Blog.tPost>} posts the list of posts */ -module.exports = async function RSSGenerator(source, target, posts) { +export default async function RSSGenerator(source, target, posts) { internals.debuglog('Generating RSS'); const feedTarget = join(target, internals.kFeedName); @@ -45,8 +45,8 @@ module.exports = async function RSSGenerator(source, target, posts) { title: internals.extractTitle(post.raw), html: encodeXML(post.html) })); - const feedXml = template(feedTemplate, { - ...templateSettings, + const feedXml = Dot.template(feedTemplate, { + ...Dot.templateSettings, strip: false })({ posts }); await writeFile(feedTarget, feedXml); diff --git a/lib/generators/static.js b/lib/generators/static.js index 416076c..60719e2 100644 --- a/lib/generators/static.js +++ b/lib/generators/static.js @@ -1,10 +1,10 @@ 'use strict'; -const { access, cp } = require('fs/promises'); -const { join } = require('path'); -const { debuglog } = require('util'); -const { rmIfExists } = require('../utils'); -const { kFileNotFoundError } = require('../constants'); +import { access, cp, readdir } from 'fs/promises'; +import { constants } from 'fs'; +import { join } from 'path'; +import { debuglog } from 'util'; +import { kFileNotFoundError } from '../constants.js'; const internals = { debuglog: debuglog('blog'), @@ -19,31 +19,31 @@ const internals = { * @param {string} target the target directory * @param {Array.<Blog.tPost>} posts the list of posts */ -module.exports = async function StaticGenerator(source, target, posts) { +export default async function StaticGenerator(source, target, _) { - const assetsTarget = join(target, internals.kAssetsDirectoryName); + try { + await access(source, constants.R_OK); - internals.debuglog(`Removing ${assetsTarget}`); - await rmIfExists(assetsTarget); + const entries = await readdir(source, { withFileTypes: true }); + for (const entry of entries) { + const sourceAsset = join(source, entry.name); + const targetAsset = join(target, entry.name); - for (let i = 0; i < posts.length; ++i) { - const postSourcePath = join(source, `${i}`); + internals.debuglog(`Copying ${sourceAsset} to ${targetAsset}`); - try { - await access(postSourcePath); - - const assetsSource = join(postSourcePath, internals.kAssetsDirectoryName); - - internals.debuglog(`Copying ${assetsSource} to ${assetsTarget}`); - await cp(assetsSource, assetsTarget, { recursive: true }); - } - catch (error) { - if (error.code === kFileNotFoundError) { - internals.debuglog(`Skipping ${i}`); - continue; + if (entry.isDirectory()) { + await cp(sourceAsset, targetAsset, { recursive: true }); + } else { + await cp(sourceAsset, targetAsset); } - - throw error; } } + catch (error) { + if (error.code === kFileNotFoundError) { + internals.debuglog(`No static directory found in ${source}`); + return; + } + + throw error; + } }; diff --git a/lib/generators/txt.js b/lib/generators/txt.js index af5ec9c..dddcbaf 100644 --- a/lib/generators/txt.js +++ b/lib/generators/txt.js @@ -1,9 +1,9 @@ 'use strict'; -const { template, templateSettings } = require('dot'); -const { readFile, writeFile } = require('fs/promises'); -const { join } = require('path'); -const { debuglog } = require('util'); +import Dot from 'dot'; +import { readFile, writeFile } from 'fs/promises'; +import { join } from 'path'; +import { debuglog } from 'util'; const internals = { debuglog: debuglog('blog'), @@ -19,7 +19,7 @@ const internals = { * @param {string} target the target directory * @param {Array.<Blog.tPost>} posts the list of posts */ -module.exports = async function TXTGenerator(source, target, posts) { +export default async function TXTGenerator(source, target, posts) { internals.debuglog('Generating TXT'); const textTarget = join(target, internals.kTextName); @@ -29,10 +29,9 @@ module.exports = async function TXTGenerator(source, target, posts) { const textTemplate = await readFile(textLocation, { encoding: 'utf8' }); internals.debuglog('Writing TXT'); - const text = template(textTemplate, { - ...templateSettings, + const text = Dot.template(textTemplate, { + ...Dot.templateSettings, strip: false })({ posts }); await writeFile(textTarget, text); }; - |