From 0953decd79b591f18a8b0344c43dd821c0898fef Mon Sep 17 00:00:00 2001 From: Kilian Finger Date: Mon, 8 Apr 2024 00:09:02 +0200 Subject: [PATCH] feat: use fs promises --- .eleventy.js | 63 ++++++++++++++++++++++++-------------------- example/.eleventy.js | 4 +-- src/OgImage.js | 4 +-- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/.eleventy.js b/.eleventy.js index b0f3716..27cbdd0 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -1,4 +1,5 @@ -import fs from 'node:fs'; +/* eslint-disable no-empty */ +import { promises as fs } from 'node:fs'; import path from 'node:path'; import { TemplatePath } from '@11ty/eleventy-utils'; import { mergeOptions } from './src/utils/index.js'; @@ -31,21 +32,21 @@ export default async function (eleventyConfig, pluginOptions) { /** @type {boolean} */ let previewMode; - eleventyConfig.on('eleventy.before', ({ runMode }) => { - if (!fs.existsSync(mergedOptions.outputDir)) { - fs.mkdirSync(mergedOptions.outputDir, { recursive: true }); - } + eleventyConfig.on('eleventy.before', async ({ runMode }) => { + try { + await fs.mkdir(mergedOptions.outputDir, { recursive: true }); + } catch {} previewMode = ['watch', 'serve'].includes(runMode); - const previewDirExists = fs.existsSync(mergedOptions.previewDir); - if (previewMode) { - if (!previewDirExists) { - fs.mkdirSync(mergedOptions.previewDir, { recursive: true }); - } - } else if (previewDirExists) { - fs.rmSync(mergedOptions.previewDir, { recursive: true, force: true }); + try { + await fs.mkdir(mergedOptions.previewDir, { recursive: true }); + } catch {} + } else { + try { + await fs.rm(mergedOptions.previewDir, { recursive: true, force: true }); + } catch {} } }); @@ -67,7 +68,9 @@ export default async function (eleventyConfig, pluginOptions) { const joinedInputPath = TemplatePath.standardizeFilePath(path.join(directoriesConfig.input, shortcodeInputPath)); - if (!fs.existsSync(joinedInputPath)) { + try { + await fs.access(joinedInputPath); + } catch { throw new Error(`Could not find file for the \`ogImage\` shortcode, looking for: ${joinedInputPath}`); } @@ -89,32 +92,34 @@ export default async function (eleventyConfig, pluginOptions) { }); const outputFilePath = await ogImage.outputFilePath(); + const cacheFilePath = await ogImage.cacheFilePath(); - if (!fs.existsSync(outputFilePath)) { - const cacheFilePath = await ogImage.cacheFilePath(); + if (cacheFilePath !== outputFilePath) { + try { + await fs.copyFile(cacheFilePath, outputFilePath); + } catch {} + } - if (cacheFilePath !== outputFilePath && fs.existsSync(cacheFilePath)) { - fs.copyFileSync(cacheFilePath, outputFilePath); - } else { - const image = await ogImage.render(); + try { + await fs.access(outputFilePath); + } catch { + const image = await ogImage.render(); - await image.toFile(outputFilePath); + await image.toFile(outputFilePath); - eleventyConfig.logger.log( - `Writing ${TemplatePath.stripLeadingDotSlash(outputFilePath)} from ${joinedInputPath}`, - ); - } + eleventyConfig.logger.log( + `Writing ${TemplatePath.stripLeadingDotSlash(outputFilePath)} from ${joinedInputPath}`, + ); } if (previewMode) { const previewFilePath = ogImage.previewFilePath(); - const dir = path.dirname(previewFilePath); - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir, { recursive: true }); - } + try { + await fs.mkdir(path.dirname(previewFilePath), { recursive: true }); + } catch {} - fs.copyFileSync(outputFilePath, previewFilePath); + await fs.copyFile(outputFilePath, previewFilePath); } return ogImage.generateHtml(); diff --git a/example/.eleventy.js b/example/.eleventy.js index bfc5f2b..aed673f 100644 --- a/example/.eleventy.js +++ b/example/.eleventy.js @@ -1,4 +1,4 @@ -import fs from 'node:fs'; +import { promises as fs } from 'node:fs'; import module from 'node:module'; import twemoji from 'twemoji'; import EleventyPluginOgImage from '../.eleventy.js'; @@ -15,7 +15,7 @@ export default async function (eleventyConfig) { fonts: [ { name: 'Inter', - data: fs.readFileSync(require.resolve('@fontsource/inter/files/inter-latin-700-normal.woff')), + data: await fs.readFile(require.resolve('@fontsource/inter/files/inter-latin-700-normal.woff')), weight: 700, style: 'normal', }, diff --git a/src/OgImage.js b/src/OgImage.js index 9f42888..b06371b 100644 --- a/src/OgImage.js +++ b/src/OgImage.js @@ -1,4 +1,4 @@ -import fs from 'node:fs'; +import { promises as fs } from 'node:fs'; import module from 'node:module'; import { File } from '@11ty/eleventy/src/Plugins/RenderPlugin.js'; /* eslint-disable import/no-unresolved */ @@ -17,7 +17,7 @@ import { sortObject } from './utils/index.js'; const require = module.createRequire(import.meta.url); -const Yoga = await initYoga(fs.readFileSync(require.resolve('yoga-wasm-web/dist/yoga.wasm'))); +const Yoga = await initYoga(await fs.readFile(require.resolve('yoga-wasm-web/dist/yoga.wasm'))); init(Yoga); export class OgImage {