mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 09:29:38 +02:00
Use Predicate and FileSystem in final Effect cleanup
This commit is contained in:
parent
c40bd406f8
commit
976e7ecfc5
6 changed files with 164 additions and 32 deletions
|
|
@ -1,3 +1,10 @@
|
|||
import * as BunFileSystem from "@effect/platform-bun/BunFileSystem";
|
||||
import { Effect, ManagedRuntime } from "effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import type { PlatformError } from "effect/PlatformError";
|
||||
|
||||
const fileSystemRuntime = ManagedRuntime.make(BunFileSystem.layer);
|
||||
|
||||
export function joinPath(...segments: string[]): string {
|
||||
const joined = segments
|
||||
.filter((segment) => segment.length > 0)
|
||||
|
|
@ -15,26 +22,52 @@ export function dirnamePath(path: string): string {
|
|||
return normalized.slice(0, index);
|
||||
}
|
||||
|
||||
export const ensureDirectoryEffect = (path: string): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) =>
|
||||
fs.makeDirectory(path, { recursive: true })
|
||||
);
|
||||
|
||||
export function ensureDirectory(path: string): Promise<void> {
|
||||
return Bun.$`mkdir -p ${path}`.quiet().then(() => undefined);
|
||||
return fileSystemRuntime.runPromise(ensureDirectoryEffect(path));
|
||||
}
|
||||
|
||||
export const readTextFileEffect = (path: string): Effect.Effect<string, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(path));
|
||||
|
||||
export function readTextFile(path: string): Promise<string> {
|
||||
return Bun.file(path).text();
|
||||
return fileSystemRuntime.runPromise(readTextFileEffect(path));
|
||||
}
|
||||
|
||||
export const readBinaryFileEffect = (path: string): Effect.Effect<Uint8Array, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFile(path));
|
||||
|
||||
export function readBinaryFile(path: string): Promise<Uint8Array> {
|
||||
return Bun.file(path).arrayBuffer().then((buffer) => new Uint8Array(buffer));
|
||||
return fileSystemRuntime.runPromise(readBinaryFileEffect(path));
|
||||
}
|
||||
|
||||
export const writeTextFileEffect = (
|
||||
path: string,
|
||||
data: string,
|
||||
): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.writeFileString(path, data));
|
||||
|
||||
export function writeTextFile(path: string, data: string): Promise<void> {
|
||||
return Bun.write(path, data).then(() => undefined);
|
||||
return fileSystemRuntime.runPromise(writeTextFileEffect(path, data));
|
||||
}
|
||||
|
||||
export const writeBinaryFileEffect = (
|
||||
path: string,
|
||||
data: Uint8Array,
|
||||
): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.writeFile(path, data));
|
||||
|
||||
export function writeBinaryFile(path: string, data: Uint8Array): Promise<void> {
|
||||
return Bun.write(path, data).then(() => undefined);
|
||||
return fileSystemRuntime.runPromise(writeBinaryFileEffect(path, data));
|
||||
}
|
||||
|
||||
export const removePathEffect = (path: string): Effect.Effect<void, PlatformError, FileSystem.FileSystem> =>
|
||||
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.remove(path));
|
||||
|
||||
export function removePath(path: string): Promise<void> {
|
||||
return Bun.file(path).delete();
|
||||
return fileSystemRuntime.runPromise(removePathEffect(path));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue