Skip to content

Commit

Permalink
feat: use fs promises
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian committed Apr 7, 2024
1 parent f14980e commit 0953dec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
63 changes: 34 additions & 29 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {}
}
});

Expand All @@ -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}`);
}

Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions example/.eleventy.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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',
},
Expand Down
4 changes: 2 additions & 2 deletions src/OgImage.js
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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 {
Expand Down

0 comments on commit 0953dec

Please sign in to comment.