Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WorkspaceFileSystem.ts

55 lines1.9 KB

1import { Effect, FileSystem, Layer, Path } from "effect";
2​
3import {
4 WorkspaceFileSystem,
5 WorkspaceFileSystemError,
6 type WorkspaceFileSystemShape,
7} from "../Services/WorkspaceFileSystem.ts";
8import { WorkspaceEntries } from "../Services/WorkspaceEntries.ts";
9import { WorkspacePaths } from "../Services/WorkspacePaths.ts";
10​
11export const makeWorkspaceFileSystem = Effect.gen(function* () {
12 const fileSystem = yield* FileSystem.FileSystem;
13 const path = yield* Path.Path;
14 const workspacePaths = yield* WorkspacePaths;
15 const workspaceEntries = yield* WorkspaceEntries;
16​
17 const writeFile: WorkspaceFileSystemShape["writeFile"] = Effect.fn(
18 "WorkspaceFileSystem.writeFile",
19 )(function* (input) {
20 const target = yield* workspacePaths.resolveRelativePathWithinRoot({
21 workspaceRoot: input.cwd,
22 relativePath: input.relativePath,
23 });
24​
25 yield* fileSystem.makeDirectory(path.dirname(target.absolutePath), { recursive: true }).pipe(
26 Effect.mapError(
27 (cause) =>
28 new WorkspaceFileSystemError({
29 cwd: input.cwd,
30 relativePath: input.relativePath,
31 operation: "workspaceFileSystem.makeDirectory",
32 detail: cause.message,
33 cause,
34 }),
35 ),
36 );
37 yield* fileSystem.writeFileString(target.absolutePath, input.contents).pipe(
38 Effect.mapError(
39 (cause) =>
40 new WorkspaceFileSystemError({
41 cwd: input.cwd,
42 relativePath: input.relativePath,
43 operation: "workspaceFileSystem.writeFile",
44 detail: cause.message,
45 cause,
46 }),
47 ),
48 );
49 yield* workspaceEntries.invalidate(input.cwd);
50 return { relativePath: target.relativePath };
51 });
52 return { writeFile } satisfies WorkspaceFileSystemShape;
53});
54​
55export const WorkspaceFileSystemLive = Layer.effect(WorkspaceFileSystem, makeWorkspaceFileSystem);