diff --git a/nose/bin/edit.ts b/nose/bin/edit.ts index 4768cc4..8183c32 100644 --- a/nose/bin/edit.ts +++ b/nose/bin/edit.ts @@ -29,32 +29,29 @@ export default async function (path: string) { files = files.filter(file => file.endsWith(`${project}.ts`) || file.endsWith(`${project}.tsx`)) } - if (!files.includes(path)) - return { error: `file not found: ${path}` } - - return await readFile(join(root, path)) + if (files.includes(path)) + return await readFile(join(root, path)) + else + return await newFile(join(root, path)) } async function readFile(path: string): Promise { const ext = extname(path).slice(1) const file = Bun.file(path) - switch (ext) { - case "jpeg": case "jpg": case "png": case "gif": case "webp": - const img = await file.arrayBuffer() - return { html: `` } - case "mp3": case "wav": - case "mp4": case "mov": case "avi": case "mkv": case "webm": - return "Not implemented" - default: - if (await isBinaryFile(path)) - throw "Cannot display binary file" - const text = await file.text() - const rows = countChar(text, "\n") + 1 - return { - html: `` - } + if (await isBinaryFile(path)) + throw "Cannot display binary file" + + const text = await file.text() + const rows = countChar(text, "\n") + 1 + + return { + html: `` } } - +function newFile(path: string): CommandOutput { + return { + html: `` + } +} diff --git a/src/dispatch.ts b/src/dispatch.ts index fa74bfb..bedfb86 100644 --- a/src/dispatch.ts +++ b/src/dispatch.ts @@ -13,8 +13,8 @@ export async function dispatchMessage(ws: any, msg: Message) { send(ws, { id: msg.id, type: "output", data: result }) } else if (msg.type === "save-file") { - if (msg.id && typeof msg.data === "string" && isFile(msg.id)) { - await Bun.write(msg.id, msg.data) + if (msg.id && typeof msg.data === "string") { + await Bun.write(msg.id.replace("..", ""), msg.data, { createPath: true }) send(ws, { type: "output", data: { status: "ok", output: `saved ${basename(msg.id)}` } }) }