make new files

This commit is contained in:
Chris Wanstrath 2025-09-21 21:33:41 -07:00
parent 9427dd6c43
commit 54a4ff6287
2 changed files with 19 additions and 22 deletions

View File

@ -29,32 +29,29 @@ export default async function (path: string) {
files = files.filter(file => file.endsWith(`${project}.ts`) || file.endsWith(`${project}.tsx`)) files = files.filter(file => file.endsWith(`${project}.ts`) || file.endsWith(`${project}.tsx`))
} }
if (!files.includes(path)) if (files.includes(path))
return { error: `file not found: ${path}` } return await readFile(join(root, path))
else
return await readFile(join(root, path)) return await newFile(join(root, path))
} }
async function readFile(path: string): Promise<CommandOutput> { async function readFile(path: string): Promise<CommandOutput> {
const ext = extname(path).slice(1) const ext = extname(path).slice(1)
const file = Bun.file(path) const file = Bun.file(path)
switch (ext) { if (await isBinaryFile(path))
case "jpeg": case "jpg": case "png": case "gif": case "webp": throw "Cannot display binary file"
const img = await file.arrayBuffer()
return { html: `<img src="data:image/${ext};base64,${Buffer.from(img).toString('base64')}" />` } const text = await file.text()
case "mp3": case "wav": const rows = countChar(text, "\n") + 1
case "mp4": case "mov": case "avi": case "mkv": case "webm":
return "Not implemented" return {
default: html: `<textarea class="editor" spellcheck="false" rows=${rows} data-path="${path}">${text}</textarea>`
if (await isBinaryFile(path))
throw "Cannot display binary file"
const text = await file.text()
const rows = countChar(text, "\n") + 1
return {
html: `<textarea class="editor" spellcheck="false" rows=${rows} data-path="${path}" >${text}</textarea>`
}
} }
} }
function newFile(path: string): CommandOutput {
return {
html: `<textarea class="editor" spellcheck="false" rows=1 data-path="${path}"></textarea>`
}
}

View File

@ -13,8 +13,8 @@ export async function dispatchMessage(ws: any, msg: Message) {
send(ws, { id: msg.id, type: "output", data: result }) send(ws, { id: msg.id, type: "output", data: result })
} else if (msg.type === "save-file") { } else if (msg.type === "save-file") {
if (msg.id && typeof msg.data === "string" && isFile(msg.id)) { if (msg.id && typeof msg.data === "string") {
await Bun.write(msg.id, msg.data) await Bun.write(msg.id.replace("..", ""), msg.data, { createPath: true })
send(ws, { type: "output", data: { status: "ok", output: `saved ${basename(msg.id)}` } }) send(ws, { type: "output", data: { status: "ok", output: `saved ${basename(msg.id)}` } })
} }