Skip to content

Commit

Permalink
wip: some effects
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Jun 11, 2024
1 parent 60ca369 commit 83af2c0
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions tool/compile-mdx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// deno-lint-ignore-file explicit-function-return-type
import { type CompileOptions, compile } from "@mdx-js/mdx";
import { dirname, fromFileUrl, join, resolve } from "@std/path";
import {} from "effect";
import { Console, Effect, Stream } from "effect";
import rehypeMathjax from "rehype-mathjax";
import remarkFrontmatter from "remark-frontmatter";
import remarkLintCheckboxContentIndent from "remark-lint-checkbox-content-indent";
Expand Down Expand Up @@ -45,7 +46,7 @@ const utilsDir = join(srcDir, "utils");
/**
* Compile the MDX files into JS.
*/
async function run(): Promise<void> {
const program = Effect.gen(function* () {
const initialFiles = getSolutions(contentDir);
const compiledFiles = compileSolutions(initialFiles);

Expand All @@ -64,7 +65,8 @@ async function run(): Promise<void> {
]);

console.info(`Compiled ${files.length} MDX files into JS.`);
}
yield* Console.log("Hello, World!");
});

/**
* Get all of the MDX files in a directory.
Expand All @@ -75,19 +77,25 @@ async function run(): Promise<void> {
* @remarks
* This is an async generator because it's recursive.
*/
async function* getSolutions(
const getSolutions = (
basePath: string,
currentPath = "",
): AsyncGenerator<VFile, void, unknown> {
for await (const entry of Deno.readDir(resolve(basePath, currentPath))) {
const fullPath = resolve(basePath, currentPath, entry.name);
if (entry.isFile && entry.name.match(/\.mdx?$/) !== null) {
yield getSolution(fullPath, currentPath, entry.name);
} else if (entry.isDirectory) {
yield* getSolutions(basePath, join(currentPath, entry.name));
}
}
}
): Stream.Stream<unknown, unknown, unknown> =>
Stream.fromAsyncIterable(Deno.readDir(resolve(basePath, currentPath)), () =>
Console.debug(""),
).pipe(
Stream.mapConcatEffect((entry: Deno.DirEntry) => {
const fullPath = resolve(basePath, currentPath, entry.name);
if (entry.isFile && entry.name.match(/\.mdx?$/) !== null) {
return Stream.succeed(getSolution(fullPath, currentPath, entry.name));
}
if (entry.isDirectory) {
return getSolutions(basePath, join(currentPath, entry.name));
}

return Stream.empty;
}),
);

/**
* Get the contents of a file.
Expand All @@ -97,20 +105,19 @@ async function* getSolutions(
* @param fileName - The name of the file.
* @returns The file's contents.
*/
async function getSolution(
fullPath: string,
relPath: string,
fileName: string,
): Promise<VFile> {
const fileContent = await Deno.readTextFile(fullPath);

return new VFile({
value: fileContent,
dirname: relPath,
basename: fileName,
cwd: fullPath,
const getSolution = (fullPath: string, relPath: string, fileName: string) =>
Effect.gen(function* () {
const fileContent = yield* Effect.tryPromise(() =>
Deno.readTextFile(fullPath),
);

return new VFile({
value: fileContent,
dirname: relPath,
basename: fileName,
cwd: fullPath,
});
});
}

/** Options for the lint reporter. */
const lintReportOptions = {
Expand Down Expand Up @@ -326,4 +333,4 @@ async function writeGenFile(
);
}

await run();
Effect.runFork(program);

0 comments on commit 83af2c0

Please sign in to comment.