src / tools / write.ts
import { promises as fs } from "fs";
import * as path from "path";
import { atomicWrite, fsError } from "../fsutil";
import { resolveInScope } from "../paths";
export async function runWrite(root: string, args: { path: string; content: string }): Promise<string> {
const r = await resolveInScope(root, args.path);
if (!r.ok) return r.error;
try {
const st = await fs.stat(r.abs).catch(() => null);
if (st?.isDirectory()) return `Error: is a directory, not a file: ${args.path}`;
await fs.mkdir(path.dirname(r.abs), { recursive: true });
await atomicWrite(r.abs, args.content);
} catch (e) {
return fsError(e, args.path);
}
return `written ${Buffer.byteLength(args.content, "utf8")} bytes to ${args.path}`;
}
src / tools / write.ts
import { promises as fs } from "fs";
import * as path from "path";
import { atomicWrite, fsError } from "../fsutil";
import { resolveInScope } from "../paths";
export async function runWrite(root: string, args: { path: string; content: string }): Promise<string> {
const r = await resolveInScope(root, args.path);
if (!r.ok) return r.error;
try {
const st = await fs.stat(r.abs).catch(() => null);
if (st?.isDirectory()) return `Error: is a directory, not a file: ${args.path}`;
await fs.mkdir(path.dirname(r.abs), { recursive: true });
await atomicWrite(r.abs, args.content);
} catch (e) {
return fsError(e, args.path);
}
return `written ${Buffer.byteLength(args.content, "utf8")} bytes to ${args.path}`;
}