From 83af2c096c9fb4505831c51feb1030d8739abd52 Mon Sep 17 00:00:00 2001 From: Eli <88557639+lishaduck@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:51:30 -0500 Subject: [PATCH] wip: some effects --- tool/compile-mdx.ts | 63 +++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/tool/compile-mdx.ts b/tool/compile-mdx.ts index 4b662422..c1da3fa4 100644 --- a/tool/compile-mdx.ts +++ b/tool/compile-mdx.ts @@ -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"; @@ -45,7 +46,7 @@ const utilsDir = join(srcDir, "utils"); /** * Compile the MDX files into JS. */ -async function run(): Promise { +const program = Effect.gen(function* () { const initialFiles = getSolutions(contentDir); const compiledFiles = compileSolutions(initialFiles); @@ -64,7 +65,8 @@ async function run(): Promise { ]); console.info(`Compiled ${files.length} MDX files into JS.`); -} + yield* Console.log("Hello, World!"); +}); /** * Get all of the MDX files in a directory. @@ -75,19 +77,25 @@ async function run(): Promise { * @remarks * This is an async generator because it's recursive. */ -async function* getSolutions( +const getSolutions = ( basePath: string, currentPath = "", -): AsyncGenerator { - 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 => + 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. @@ -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 { - 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 = { @@ -326,4 +333,4 @@ async function writeGenFile( ); } -await run(); +Effect.runFork(program);