mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
feat(core): add append to Environment.Files (#64517)
This commit is contained in:
committed by
GitHub
Unverified
parent
cb41d3da12
commit
64630cfd65
@@ -145,6 +145,8 @@ export const execDefaults = (spawner: ChildProcessSpawner["Service"]): FilesImpl
|
||||
move: (from, to) =>
|
||||
run(from, moveScript, [to]).pipe(Effect.flatMap((result) => classifyPlain(from, result, () => undefined))),
|
||||
mkdir: (path) => run(path, `mkdir -p -- "$1"`).pipe(Effect.flatMap((result) => complete(path, result))),
|
||||
append: (path, bytes) =>
|
||||
run(path, `cat >> "$1"`, [], bytes).pipe(Effect.flatMap((result) => complete(path, result))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,8 @@ export interface FilesImpl {
|
||||
readonly remove: (path: string) => Effect.Effect<void, Failed>
|
||||
readonly move: (from: string, to: string) => Effect.Effect<void, NotFound | Failed>
|
||||
readonly mkdir: (path: string) => Effect.Effect<void, Failed>
|
||||
/** Adds bytes to the end of a file, creating it when missing. */
|
||||
readonly append: (path: string, bytes: Uint8Array) => Effect.Effect<void, Failed>
|
||||
}
|
||||
|
||||
export interface Files extends FilesImpl {}
|
||||
|
||||
@@ -64,6 +64,7 @@ export const makeLocalDriver = (spawner: ChildProcessSpawner["Service"]): Driver
|
||||
yield* attempt(from, () => fs.rename(from, destination))
|
||||
}),
|
||||
mkdir: (value) => attempt(value, () => fs.mkdir(value, { recursive: true }).then(() => undefined)),
|
||||
append: (value, bytes) => attempt(value, () => fs.appendFile(value, bytes)),
|
||||
}
|
||||
|
||||
return { spawner, overrides }
|
||||
|
||||
@@ -137,6 +137,21 @@ export const makeMemoryDriver = (): MemoryDriver => {
|
||||
})
|
||||
}),
|
||||
mkdir: (value) => Effect.try({ try: () => mkdirSync(value), catch: (cause) => failed(value, cause) }),
|
||||
append: (value, bytes) =>
|
||||
Effect.try({
|
||||
try: () => {
|
||||
const target = resolveKey(value, true) ?? key(value)
|
||||
const current = nodes.get(target)
|
||||
if (current && current.type !== "file") throw new Error(`Path is not a file: ${value}`)
|
||||
if (!current) requireParent(target)
|
||||
const before = current?.bytes ?? new Uint8Array()
|
||||
const next = new Uint8Array(before.length + bytes.length)
|
||||
next.set(before)
|
||||
next.set(bytes, before.length)
|
||||
nodes.set(target, { type: "file", bytes: next, mtimeMs: Date.now() })
|
||||
},
|
||||
catch: (cause) => failed(value, cause),
|
||||
}),
|
||||
}
|
||||
|
||||
const spawner = make((command) =>
|
||||
|
||||
@@ -310,6 +310,7 @@ const layer = (options: Options) =>
|
||||
remove: (path) => withFiles(path, (files) => files.remove(path)),
|
||||
move: (from, to) => withFiles(from, (files) => files.move(from, to)),
|
||||
mkdir: (path) => withFiles(path, (files) => files.mkdir(path)),
|
||||
append: (path, bytes) => withFiles(path, (files) => files.append(path, bytes)),
|
||||
}
|
||||
return { spawner, overrides }
|
||||
}),
|
||||
|
||||
@@ -178,6 +178,15 @@ export const environmentConformance = <E>(
|
||||
}),
|
||||
)
|
||||
|
||||
check("appends to a file, creating it when missing", (harness) =>
|
||||
Effect.gen(function* () {
|
||||
const target = `${harness.root}/bytes.txt`
|
||||
yield* harness.files.append(target, bytes("abc"))
|
||||
yield* harness.files.append(target, bytes("def"))
|
||||
expect(text((yield* harness.files.read(target)).bytes)).toBe("abcdef")
|
||||
}),
|
||||
)
|
||||
|
||||
check("moves files and removes trees idempotently", (harness) =>
|
||||
Effect.gen(function* () {
|
||||
const source = `${harness.root}/source/file`
|
||||
|
||||
Reference in New Issue
Block a user