diff --git a/tool/compile-mdx.ts b/tool/compile-mdx.ts index a3486385..de52d2b6 100644 --- a/tool/compile-mdx.ts +++ b/tool/compile-mdx.ts @@ -8,6 +8,7 @@ import remarkPresetLintRecommended from "remark-preset-lint-recommended"; import { VFile } from "vfile"; import { matter } from "vfile-matter"; import { reporter } from "vfile-reporter"; +import { map } from "./promises.ts"; // Change the directory so that relative paths are based on the file, not the CWD. Deno.chdir(dirname(fromFileUrl(Deno.mainModule))); @@ -18,31 +19,22 @@ const contentDir = `${srcDir}/content`; const utilsDir = `${srcDir}/utils`; async function run(): Promise { - const initialFiles = await getSolutions(); - const files = await compileSolutions(initialFiles); + const initialFiles = await map( + Deno.readDir(contentDir), + (entry) => getSolution(entry.name), + (entry) => entry.isFile && entry.name.match(/mdx?/) !== null, + ); + const files = await map(initialFiles, compileSolution); await Promise.all([ new Promise((): void => lint(files)), - writeSolutions(files), + map(files, writeSolution), staticImports(files), ]); console.info(`Compiled ${files.length} MDX files into JS.`); } -// biome-ignore lint/nursery/useAwait: false-positive -async function getSolutions(): Promise { - const promises = []; - for await (const entry of Deno.readDir(contentDir)) { - if (entry.isFile && entry.name.match(/mdx?/)) { - const promise = getSolution(entry.name); - promises.push(promise); - } - } - - return Promise.all(promises); -} - async function getSolution(fileName: string): Promise { // Get the file. const fileContent = await Deno.readTextFile(resolve(contentDir, fileName)); @@ -90,16 +82,6 @@ const compileOptions: CompileOptions = { remarkPlugins, }; -function compileSolutions(files: VFile[]): Promise { - const promises = []; - for (const file of files) { - const promise = compileSolution(file); - promises.push(promise); - } - - return Promise.all(promises); -} - async function compileSolution(file: VFile): Promise { // Extract the frontmatter into `data.matter`. matter(file); @@ -111,16 +93,6 @@ async function compileSolution(file: VFile): Promise { return compiled; } -async function writeSolutions(solutions: VFile[]): Promise { - const promises = []; - for (const solution of solutions) { - const promise = writeSolution(solution); - promises.push(promise); - } - - await Promise.all(promises); -} - // Write the file to the disk. function writeSolution(solution: VFile): Promise { return Deno.writeTextFile( diff --git a/tool/promises.ts b/tool/promises.ts new file mode 100644 index 00000000..88e3f841 --- /dev/null +++ b/tool/promises.ts @@ -0,0 +1,15 @@ +export async function map( + iterable: Iterable | AsyncIterable, + func: (iter: T) => Promise, + run: (iter: T) => boolean = (_) => true, +): Promise { + const promises = []; + for await (const entry of iterable) { + if (run(entry)) { + const promise = func(entry); + promises.push(promise); + } + } + + return Promise.all(promises); +}