make new files
This commit is contained in:
parent
9427dd6c43
commit
54a4ff6287
|
|
@ -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<CommandOutput> {
|
||||
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: `<img src="data:image/${ext};base64,${Buffer.from(img).toString('base64')}" />` }
|
||||
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: `<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>`
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)}` } })
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user