(api: Hono, adapter: StudioApiAdapter)
| 1580 | // ── Route registration ────────────────────────────────────────────────────── |
| 1581 | |
| 1582 | export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void { |
| 1583 | // ── Read ── |
| 1584 | |
| 1585 | api.get("/projects/:id/files/*", async (c) => { |
| 1586 | const res = await resolveProjectFile(c, adapter); |
| 1587 | if ("error" in res) return res.error; |
| 1588 | |
| 1589 | if (!existsSync(res.absPath)) { |
| 1590 | if (c.req.query("optional") === "1") { |
| 1591 | return c.json({ filename: res.filePath, content: "" }); |
| 1592 | } |
| 1593 | return c.json({ error: "not found" }, 404); |
| 1594 | } |
| 1595 | |
| 1596 | const content = readFileSync(res.absPath, "utf-8"); |
| 1597 | return c.json({ filename: res.filePath, content }); |
| 1598 | }); |
| 1599 | |
| 1600 | // ── Write (overwrite) ── |
| 1601 | |
| 1602 | api.put("/projects/:id/files/*", async (c) => { |
| 1603 | const res = await resolveProjectFile(c, adapter); |
| 1604 | if ("error" in res) return res.error; |
| 1605 | |
| 1606 | ensureDir(res.absPath); |
| 1607 | const body = await c.req.text(); |
| 1608 | const backup = snapshotBeforeWrite(res.project.dir, res.absPath); |
| 1609 | if (backup.error) console.warn(`Failed to create backup for ${res.filePath}: ${backup.error}`); |
| 1610 | writeFileSync(res.absPath, body, "utf-8"); |
| 1611 | |
| 1612 | return c.json({ |
| 1613 | ok: true, |
| 1614 | path: res.filePath, |
| 1615 | backupPath: backupPathForResponse(res.project.dir, backup.backupPath), |
| 1616 | }); |
| 1617 | }); |
| 1618 | |
| 1619 | // ── Create (fail if exists) ── |
| 1620 | |
| 1621 | api.post("/projects/:id/files/*", async (c) => { |
| 1622 | const res = await resolveProjectFile(c, adapter); |
| 1623 | if ("error" in res) return res.error; |
| 1624 | |
| 1625 | if (existsSync(res.absPath)) { |
| 1626 | return c.json({ error: "already exists" }, 409); |
| 1627 | } |
| 1628 | |
| 1629 | ensureDir(res.absPath); |
| 1630 | const body = await c.req.text().catch(() => ""); |
| 1631 | writeFileSync(res.absPath, body, "utf-8"); |
| 1632 | |
| 1633 | return c.json({ ok: true, path: res.filePath }, 201); |
| 1634 | }); |
| 1635 | |
| 1636 | // ── Delete ── |
| 1637 | |
| 1638 | api.delete("/projects/:id/files/*", async (c) => { |
| 1639 | const res = await resolveProjectFile(c, adapter, { mustExist: true }); |
no test coverage detected