Skip to content

Commit

Permalink
refactor: extract out mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jan 23, 2024
1 parent 8737fcc commit 7bf10ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
44 changes: 8 additions & 36 deletions tool/compile-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -18,31 +19,22 @@ const contentDir = `${srcDir}/content`;
const utilsDir = `${srcDir}/utils`;

async function run(): Promise<void> {
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>((): 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<VFile[]> {
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<VFile> {
// Get the file.
const fileContent = await Deno.readTextFile(resolve(contentDir, fileName));
Expand Down Expand Up @@ -90,16 +82,6 @@ const compileOptions: CompileOptions = {
remarkPlugins,
};

function compileSolutions(files: VFile[]): Promise<VFile[]> {
const promises = [];
for (const file of files) {
const promise = compileSolution(file);
promises.push(promise);
}

return Promise.all(promises);
}

async function compileSolution(file: VFile): Promise<VFile> {
// Extract the frontmatter into `data.matter`.
matter(file);
Expand All @@ -111,16 +93,6 @@ async function compileSolution(file: VFile): Promise<VFile> {
return compiled;
}

async function writeSolutions(solutions: VFile[]): Promise<void> {
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<void> {
return Deno.writeTextFile(
Expand Down
15 changes: 15 additions & 0 deletions tool/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export async function map<T, U>(
iterable: Iterable<T> | AsyncIterable<T>,
func: (iter: T) => Promise<U>,
run: (iter: T) => boolean = (_) => true,
): Promise<U[]> {
const promises = [];
for await (const entry of iterable) {
if (run(entry)) {
const promise = func(entry);
promises.push(promise);
}
}

return Promise.all(promises);
}

0 comments on commit 7bf10ea

Please sign in to comment.