From 06cef1fd81b05a44769b88d5480b9f3ddcc976e1 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sun, 23 Mar 2025 22:38:50 -0500 Subject: [PATCH 1/2] fix: ensure output directory exists before running generators --- src/generators.mjs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/generators.mjs b/src/generators.mjs index b276d779..a42d7bc2 100644 --- a/src/generators.mjs +++ b/src/generators.mjs @@ -3,6 +3,7 @@ import publicGenerators from './generators/index.mjs'; import astJs from './generators/ast-js/index.mjs'; import oramaDb from './generators/orama-db/index.mjs'; +import { mkdirSync, statSync } from 'node:fs'; const availableGenerators = { ...publicGenerators, @@ -51,6 +52,16 @@ const createGenerator = markdownInput => { * @param {import('./generators/types.d.ts').GeneratorOptions} options The options for the generator runtime */ const runGenerators = async ({ generators, ...extra }) => { + try { + if (!statSync(extra.output).isDirectory()) { + throw new Error('Output is not a directory'); + } + } catch (err) { + if (err.code === 'ENOENT') { + mkdirSync(extra.output, { recursive: true }); + } + } + // Note that this method is blocking, and will only execute one generator per-time // but it ensures all dependencies are resolved, and that multiple bottom-level generators // can reuse the already parsed content from the top-level/dependency generators From 7f08381b52ce471ef47a08bee97d12674df1fb05 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Mon, 24 Mar 2025 14:20:40 -0500 Subject: [PATCH 2/2] move async functions --- src/generators.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/generators.mjs b/src/generators.mjs index a42d7bc2..596782ba 100644 --- a/src/generators.mjs +++ b/src/generators.mjs @@ -3,7 +3,7 @@ import publicGenerators from './generators/index.mjs'; import astJs from './generators/ast-js/index.mjs'; import oramaDb from './generators/orama-db/index.mjs'; -import { mkdirSync, statSync } from 'node:fs'; +import { mkdir, stat } from 'node:fs/promises'; const availableGenerators = { ...publicGenerators, @@ -53,12 +53,12 @@ const createGenerator = markdownInput => { */ const runGenerators = async ({ generators, ...extra }) => { try { - if (!statSync(extra.output).isDirectory()) { + if (!(await stat(extra.output).isDirectory())) { throw new Error('Output is not a directory'); } } catch (err) { if (err.code === 'ENOENT') { - mkdirSync(extra.output, { recursive: true }); + mkdir(extra.output, { recursive: true }); } }