From: Ruben Beltran del Rio Date: Wed, 14 Feb 2024 16:15:11 +0000 (+0100) Subject: Lint X-Git-Tag: 6.0.0~2 X-Git-Url: https://git.r.bdr.sh/rbdr/blog/commitdiff_plain/10a76a5bf0a9d051aa9432566088034c5ced714b?hp=6cd62e795e3716aa0cbd2d1ff8c1b3a345803563 Lint --- diff --git a/.eslintrc b/.eslintrc index fa9328d..efbe3ab 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,15 @@ { + "root": true, "plugins": ["@hapi"], + "extends": "plugin:@hapi/recommended", "parserOptions": { "ecmaVersion": "latest", "sourceType": "module" + }, + "rules": { + "indent": [ + 2, + 2 + ] } } diff --git a/config/config.js b/config/config.js index 531f3e9..cf2a386 100644 --- a/config/config.js +++ b/config/config.js @@ -93,5 +93,5 @@ export default { archiveOutputDirectory: resolve(join(internals.outputDirectory, 'archive')), // Internal config: This can't be modified. - defaultTemplatesDirectory: resolve(join(__dirname, '../templates')), + defaultTemplatesDirectory: resolve(join(__dirname, '../templates')) }; diff --git a/lib/archivers/gemlog.js b/lib/archivers/gemlog.js index d56a1f6..1bb0c10 100644 --- a/lib/archivers/gemlog.js +++ b/lib/archivers/gemlog.js @@ -11,22 +11,26 @@ const internals = { debuglog: debuglog('blog'), buildUrl(id, slug) { + return `./${id}/${slug}`; }, - buildTitle (id, slug) { + buildTitle(id, slug) { + const date = new Date(Number(id)); - const shortDate = date.toISOString().split('T')[0] + const shortDate = date.toISOString().split('T')[0]; const title = slug.split('-').join(' '); - return `${shortDate} ${title}` + return `${shortDate} ${title}`; }, buildLink(id, slug) { + return `=> ${internals.buildUrl(id,slug)} ${internals.buildTitle(id,slug)}`; } }; -export default async function(templateDirectory, source, target) { +export default async function (templateDirectory, source, target) { + internals.debuglog(`Reading archive ${source}`); const postIds = (await readdir(source)) .sort((a, b) => Number(b) - Number(a)); @@ -34,10 +38,10 @@ export default async function(templateDirectory, source, target) { for (const id of postIds) { const postDirectory = join(source, id); const slug = (await readdir(postDirectory)) - .filter((entry) => internals.kGeminiRe.test(entry))[0]; + .filter((entry) => internals.kGeminiRe.test(entry))[0]; - posts.push({ id, slug }) - }; + posts.push({ id, slug }); + } internals.debuglog(`Read ${posts.length} posts`); @@ -52,7 +56,7 @@ export default async function(templateDirectory, source, target) { ...Dot.templateSettings, strip: false })({ - posts: posts.map((post) => internals.buildLink(post.id, post.slug)).join('\n'), + posts: posts.map((post) => internals.buildLink(post.id, post.slug)).join('\n') }); try { @@ -68,4 +72,4 @@ export default async function(templateDirectory, source, target) { internals.debuglog('Copying posts to archive'); await cp(source, target, { recursive: true }); } -}; +} diff --git a/lib/blog.js b/lib/blog.js index 04106b0..e1a76a8 100644 --- a/lib/blog.js +++ b/lib/blog.js @@ -1,6 +1,6 @@ import { access, cp, readdir, readFile, writeFile } from 'fs/promises'; import { exec } from 'child_process'; -import { basename, resolve, join } from 'path'; +import { basename, join } from 'path'; import ParseGemini from 'gemini-to-html/parse.js'; import RenderGemini from 'gemini-to-html/render.js'; import { debuglog, promisify } from 'util'; @@ -72,9 +72,10 @@ export default class Blog { try { await this.syncDown(); } - catch {}; + catch {} + await this._shift(); - const firstDirectory = join(this.postsDirectory, '0'); + const firstDirectory = join(this.postsDirectory, '0'); await rmIfExists(firstDirectory); await ensureDirectoryExists(firstDirectory); await this._update(postLocation); @@ -94,8 +95,9 @@ export default class Blog { try { await this.syncDown(); } - catch {}; - const metadata = await this._update(postLocation); + catch {} + + await this._update(postLocation); } /** @@ -168,8 +170,9 @@ export default class Blog { * @instance */ async addRemote(remote) { + await ensureDirectoryExists(this.configDirectory); - await Remote.add(this.remoteConfig, remote) + await Remote.add(this.remoteConfig, remote); } /** @@ -181,7 +184,8 @@ export default class Blog { * @instance */ async removeRemote() { - await Remote.remove(this.remoteConfig) + + await Remote.remove(this.remoteConfig); } @@ -194,9 +198,10 @@ export default class Blog { * @instance */ async syncDown() { + internals.debuglog('Pulling remote state'); await ensureDirectoryExists(this.dataDirectory); - await Remote.syncDown(this.remoteConfig, this.dataDirectory) + await Remote.syncDown(this.remoteConfig, this.dataDirectory); internals.debuglog('Pulled remote state'); } @@ -209,9 +214,10 @@ export default class Blog { * @instance */ async syncUp() { + internals.debuglog('Pushing remote state'); await ensureDirectoryExists(this.dataDirectory); - await Remote.syncUp(this.remoteConfig, this.dataDirectory) + await Remote.syncUp(this.remoteConfig, this.dataDirectory); internals.debuglog('Pushed remote state'); } @@ -230,7 +236,7 @@ export default class Blog { try { await this.syncUp(); } - catch {}; + catch {} } @@ -288,27 +294,28 @@ export default class Blog { // Reads an individual post - async _readPost(index=0) { - const postSourcePath = join(this.postsDirectory, `${index}`); + async _readPost(index = 0) { - internals.debuglog(`Reading ${postSourcePath}`); + const postSourcePath = join(this.postsDirectory, `${index}`); - await access(postSourcePath); + internals.debuglog(`Reading ${postSourcePath}`); - const metadata = await this._getMetadata(index); + await access(postSourcePath); - const postContentPath = await this._findBlogContent(postSourcePath); - internals.debuglog(`Reading ${postContentPath}`); - const postContent = await readFile(postContentPath, { encoding: 'utf8' }); + const metadata = await this._getMetadata(index); - internals.debuglog('Parsing Gemini'); - return { - ...metadata, - location: postSourcePath, - index, - html: RenderGemini(ParseGemini(postContent)), - raw: postContent - }; + const postContentPath = await this._findBlogContent(postSourcePath); + internals.debuglog(`Reading ${postContentPath}`); + const postContent = await readFile(postContentPath, { encoding: 'utf8' }); + + internals.debuglog('Parsing Gemini'); + return { + ...metadata, + location: postSourcePath, + index, + html: RenderGemini(ParseGemini(postContent)), + raw: postContent + }; } // Shift the posts, delete any remainder. @@ -342,6 +349,7 @@ export default class Blog { // Moves older posts to the archive async _archive() { + internals.debuglog('Archiving post'); const post = await this._readPost(0); await ensureDirectoryExists(this.archiveDirectory); @@ -423,6 +431,7 @@ export default class Blog { // Gets the template directory for a given template. async _templateDirectoryFor(template) { + try { await access(join(this.templatesDirectory, template)); return this.templatesDirectory; @@ -436,4 +445,4 @@ export default class Blog { throw error; } } -}; +} diff --git a/lib/generators/html.js b/lib/generators/html.js index f7c7966..2004696 100644 --- a/lib/generators/html.js +++ b/lib/generators/html.js @@ -1,5 +1,3 @@ -'use strict'; - import Dot from 'dot'; import { readFile, writeFile } from 'fs/promises'; import { join } from 'path'; @@ -34,5 +32,4 @@ export default async function HTMLGenerator(source, target, posts) { strip: false })({ posts }); await writeFile(indexTarget, indexHtml); -}; - +} diff --git a/lib/generators/rss.js b/lib/generators/rss.js index a46a394..c47ba03 100644 --- a/lib/generators/rss.js +++ b/lib/generators/rss.js @@ -1,5 +1,3 @@ -'use strict'; - import Dot from 'dot'; import { encodeXML } from 'entities'; import { readFile, writeFile } from 'fs/promises'; @@ -50,4 +48,4 @@ export default async function RSSGenerator(source, target, posts) { strip: false })({ posts }); await writeFile(feedTarget, feedXml); -}; +} diff --git a/lib/generators/static.js b/lib/generators/static.js index 60719e2..45f5a04 100644 --- a/lib/generators/static.js +++ b/lib/generators/static.js @@ -1,5 +1,3 @@ -'use strict'; - import { access, cp, readdir } from 'fs/promises'; import { constants } from 'fs'; import { join } from 'path'; @@ -33,7 +31,8 @@ export default async function StaticGenerator(source, target, _) { if (entry.isDirectory()) { await cp(sourceAsset, targetAsset, { recursive: true }); - } else { + } + else { await cp(sourceAsset, targetAsset); } } @@ -46,4 +45,4 @@ export default async function StaticGenerator(source, target, _) { throw error; } -}; +} diff --git a/lib/generators/txt.js b/lib/generators/txt.js index dddcbaf..c3d7778 100644 --- a/lib/generators/txt.js +++ b/lib/generators/txt.js @@ -1,5 +1,3 @@ -'use strict'; - import Dot from 'dot'; import { readFile, writeFile } from 'fs/promises'; import { join } from 'path'; @@ -34,4 +32,4 @@ export default async function TXTGenerator(source, target, posts) { strip: false })({ posts }); await writeFile(textTarget, text); -}; +} diff --git a/lib/remote.js b/lib/remote.js index 780ea91..ad0a4fc 100644 --- a/lib/remote.js +++ b/lib/remote.js @@ -54,4 +54,4 @@ export default { throw new Error(internals.strings.configurationNotFound); } } -} +}; diff --git a/lib/remotes/git.js b/lib/remotes/git.js index f6d0c07..75ef43e 100644 --- a/lib/remotes/git.js +++ b/lib/remotes/git.js @@ -3,7 +3,7 @@ import { promisify } from 'util'; const internals = { // Promisified functions - exec: promisify(exec), + exec: promisify(exec) }; export default { @@ -28,7 +28,8 @@ export default { await internals.exec(`cd ${blogDirectory} && git checkout .`); } catch {} + await internals.exec(`cd ${blogDirectory} && git clean . -f`); await internals.exec(`cd ${blogDirectory} && git pull ${remote} main`); } -} +}; diff --git a/lib/utils.js b/lib/utils.js index 25d204a..cee3f47 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,7 +3,7 @@ import { kFileNotFoundError } from './constants.js'; // File system utilities -export async function rmIfExists(location) { +export const rmIfExists = async function rmIfExists(location) { try { await access(location, constants.F_OK); @@ -16,9 +16,9 @@ export async function rmIfExists(location) { throw error; } -} +}; -export async function ensureDirectoryExists(directory) { +export const ensureDirectoryExists = async function ensureDirectoryExists(directory) { try { await access(directory); @@ -31,4 +31,4 @@ export async function ensureDirectoryExists(directory) { throw error; } -} +};