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`))
|
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))
|
return await readFile(join(root, path))
|
||||||
|
else
|
||||||
|
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) {
|
|
||||||
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))
|
if (await isBinaryFile(path))
|
||||||
throw "Cannot display binary file"
|
throw "Cannot display binary file"
|
||||||
|
|
||||||
const text = await file.text()
|
const text = await file.text()
|
||||||
const rows = countChar(text, "\n") + 1
|
const rows = countChar(text, "\n") + 1
|
||||||
|
|
||||||
return {
|
return {
|
||||||
html: `<textarea class="editor" spellcheck="false" rows=${rows} data-path="${path}" >${text}</textarea>`
|
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 })
|
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)}` } })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user