|
| 1 | +import { NodeFileSystem } from "@effect/platform-node" |
| 2 | +import { dirname, join, relative, resolve as pathResolve } from "path" |
| 3 | +import { realpathSync } from "fs" |
| 4 | +import { lookup } from "mime-types" |
| 5 | +import { Effect, FileSystem, Layer, Schema, ServiceMap } from "effect" |
| 6 | +import type { PlatformError } from "effect/PlatformError" |
| 7 | +import { Glob } from "../util/glob" |
| 8 | + |
| 9 | +export namespace AppFileSystem { |
| 10 | + export class FileSystemError extends Schema.TaggedErrorClass<FileSystemError>()("FileSystemError", { |
| 11 | + method: Schema.String, |
| 12 | + cause: Schema.optional(Schema.Defect), |
| 13 | + }) {} |
| 14 | + |
| 15 | + export type Error = PlatformError | FileSystemError |
| 16 | + |
| 17 | + export interface Interface extends FileSystem.FileSystem { |
| 18 | + readonly isDir: (path: string) => Effect.Effect<boolean, Error> |
| 19 | + readonly isFile: (path: string) => Effect.Effect<boolean, Error> |
| 20 | + readonly readJson: (path: string) => Effect.Effect<unknown, Error> |
| 21 | + readonly writeJson: (path: string, data: unknown, mode?: number) => Effect.Effect<void, Error> |
| 22 | + readonly ensureDir: (path: string) => Effect.Effect<void, Error> |
| 23 | + readonly writeWithDirs: (path: string, content: string | Uint8Array, mode?: number) => Effect.Effect<void, Error> |
| 24 | + readonly findUp: (target: string, start: string, stop?: string) => Effect.Effect<string[], Error> |
| 25 | + readonly up: (options: { targets: string[]; start: string; stop?: string }) => Effect.Effect<string[], Error> |
| 26 | + readonly globUp: (pattern: string, start: string, stop?: string) => Effect.Effect<string[], Error> |
| 27 | + readonly glob: (pattern: string, options?: Glob.Options) => Effect.Effect<string[], Error> |
| 28 | + readonly globMatch: (pattern: string, filepath: string) => boolean |
| 29 | + } |
| 30 | + |
| 31 | + export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/FileSystem") {} |
| 32 | + |
| 33 | + export const layer = Layer.effect( |
| 34 | + Service, |
| 35 | + Effect.gen(function* () { |
| 36 | + const fs = yield* FileSystem.FileSystem |
| 37 | + |
| 38 | + const isDir = Effect.fn("FileSystem.isDir")(function* (path: string) { |
| 39 | + const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void)) |
| 40 | + return info?.type === "Directory" |
| 41 | + }) |
| 42 | + |
| 43 | + const isFile = Effect.fn("FileSystem.isFile")(function* (path: string) { |
| 44 | + const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void)) |
| 45 | + return info?.type === "File" |
| 46 | + }) |
| 47 | + |
| 48 | + const readJson = Effect.fn("FileSystem.readJson")(function* (path: string) { |
| 49 | + const text = yield* fs.readFileString(path) |
| 50 | + return JSON.parse(text) |
| 51 | + }) |
| 52 | + |
| 53 | + const writeJson = Effect.fn("FileSystem.writeJson")(function* (path: string, data: unknown, mode?: number) { |
| 54 | + const content = JSON.stringify(data, null, 2) |
| 55 | + yield* fs.writeFileString(path, content) |
| 56 | + if (mode) yield* fs.chmod(path, mode) |
| 57 | + }) |
| 58 | + |
| 59 | + const ensureDir = Effect.fn("FileSystem.ensureDir")(function* (path: string) { |
| 60 | + yield* fs.makeDirectory(path, { recursive: true }) |
| 61 | + }) |
| 62 | + |
| 63 | + const writeWithDirs = Effect.fn("FileSystem.writeWithDirs")(function* ( |
| 64 | + path: string, |
| 65 | + content: string | Uint8Array, |
| 66 | + mode?: number, |
| 67 | + ) { |
| 68 | + const write = typeof content === "string" ? fs.writeFileString(path, content) : fs.writeFile(path, content) |
| 69 | + |
| 70 | + yield* write.pipe( |
| 71 | + Effect.catchIf( |
| 72 | + (e) => e.reason._tag === "NotFound", |
| 73 | + () => |
| 74 | + Effect.gen(function* () { |
| 75 | + yield* fs.makeDirectory(dirname(path), { recursive: true }) |
| 76 | + yield* write |
| 77 | + }), |
| 78 | + ), |
| 79 | + ) |
| 80 | + if (mode) yield* fs.chmod(path, mode) |
| 81 | + }) |
| 82 | + |
| 83 | + const glob = Effect.fn("FileSystem.glob")(function* (pattern: string, options?: Glob.Options) { |
| 84 | + return yield* Effect.tryPromise({ |
| 85 | + try: () => Glob.scan(pattern, options), |
| 86 | + catch: (cause) => new FileSystemError({ method: "glob", cause }), |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + const findUp = Effect.fn("FileSystem.findUp")(function* (target: string, start: string, stop?: string) { |
| 91 | + const result: string[] = [] |
| 92 | + let current = start |
| 93 | + while (true) { |
| 94 | + const search = join(current, target) |
| 95 | + if (yield* fs.exists(search)) result.push(search) |
| 96 | + if (stop === current) break |
| 97 | + const parent = dirname(current) |
| 98 | + if (parent === current) break |
| 99 | + current = parent |
| 100 | + } |
| 101 | + return result |
| 102 | + }) |
| 103 | + |
| 104 | + const up = Effect.fn("FileSystem.up")(function* (options: { targets: string[]; start: string; stop?: string }) { |
| 105 | + const result: string[] = [] |
| 106 | + let current = options.start |
| 107 | + while (true) { |
| 108 | + for (const target of options.targets) { |
| 109 | + const search = join(current, target) |
| 110 | + if (yield* fs.exists(search)) result.push(search) |
| 111 | + } |
| 112 | + if (options.stop === current) break |
| 113 | + const parent = dirname(current) |
| 114 | + if (parent === current) break |
| 115 | + current = parent |
| 116 | + } |
| 117 | + return result |
| 118 | + }) |
| 119 | + |
| 120 | + const globUp = Effect.fn("FileSystem.globUp")(function* (pattern: string, start: string, stop?: string) { |
| 121 | + const result: string[] = [] |
| 122 | + let current = start |
| 123 | + while (true) { |
| 124 | + const matches = yield* glob(pattern, { cwd: current, absolute: true, include: "file", dot: true }).pipe( |
| 125 | + Effect.catch(() => Effect.succeed([] as string[])), |
| 126 | + ) |
| 127 | + result.push(...matches) |
| 128 | + if (stop === current) break |
| 129 | + const parent = dirname(current) |
| 130 | + if (parent === current) break |
| 131 | + current = parent |
| 132 | + } |
| 133 | + return result |
| 134 | + }) |
| 135 | + |
| 136 | + return Service.of({ |
| 137 | + ...fs, |
| 138 | + isDir, |
| 139 | + isFile, |
| 140 | + readJson, |
| 141 | + writeJson, |
| 142 | + ensureDir, |
| 143 | + writeWithDirs, |
| 144 | + findUp, |
| 145 | + up, |
| 146 | + globUp, |
| 147 | + glob, |
| 148 | + globMatch: Glob.match, |
| 149 | + }) |
| 150 | + }), |
| 151 | + ) |
| 152 | + |
| 153 | + export const defaultLayer = layer.pipe(Layer.provide(NodeFileSystem.layer)) |
| 154 | + |
| 155 | + // Pure helpers that don't need Effect (path manipulation, sync operations) |
| 156 | + export function mimeType(p: string): string { |
| 157 | + return lookup(p) || "application/octet-stream" |
| 158 | + } |
| 159 | + |
| 160 | + export function normalizePath(p: string): string { |
| 161 | + if (process.platform !== "win32") return p |
| 162 | + try { |
| 163 | + return realpathSync.native(p) |
| 164 | + } catch { |
| 165 | + return p |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + export function resolve(p: string): string { |
| 170 | + const resolved = pathResolve(windowsPath(p)) |
| 171 | + try { |
| 172 | + return normalizePath(realpathSync(resolved)) |
| 173 | + } catch (e: any) { |
| 174 | + if (e?.code === "ENOENT") return normalizePath(resolved) |
| 175 | + throw e |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + export function windowsPath(p: string): string { |
| 180 | + if (process.platform !== "win32") return p |
| 181 | + return p |
| 182 | + .replace(/^\/([a-zA-Z]):(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`) |
| 183 | + .replace(/^\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`) |
| 184 | + .replace(/^\/cygdrive\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`) |
| 185 | + .replace(/^\/mnt\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`) |
| 186 | + } |
| 187 | + |
| 188 | + export function overlaps(a: string, b: string) { |
| 189 | + const relA = relative(a, b) |
| 190 | + const relB = relative(b, a) |
| 191 | + return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..") |
| 192 | + } |
| 193 | + |
| 194 | + export function contains(parent: string, child: string) { |
| 195 | + return !relative(parent, child).startsWith("..") |
| 196 | + } |
| 197 | +} |
0 commit comments