Skip to content

Commit c63af3f

Browse files
Copilotmrjf
andauthored
Use Bun-native APIs in serve.ts, add return types
Address code review feedback: - Replace copyFileSync with Bun.write() for async file copy - Use Bun.file().exists() instead of Node.js existsSync for TS check - Add explicit return types to all functions - Use console.info instead of console.log for status messages Agent-Logs-Url: https://github.com/githubnext/tsessebe/sessions/526f7361-16a1-4b0c-8ef4-4c405e36044b Co-authored-by: mrjf <180956+mrjf@users.noreply.github.com>
1 parent 55fd773 commit c63af3f

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

playground/serve.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* then serves the playground on http://localhost:3000.
88
*/
99

10-
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
10+
import { existsSync, mkdirSync } from "node:fs";
1111
import { extname, join } from "node:path";
1212

1313
const PORT = 3000;
@@ -24,49 +24,48 @@ const MIME_TYPES: Record<string, string> = {
2424
".svg": "image/svg+xml",
2525
};
2626

27-
async function buildBundle() {
28-
console.log("Building tsb browser bundle…");
27+
async function buildBundle(): Promise<void> {
28+
console.info("Building tsb browser bundle…");
2929
const result = await Bun.build({
3030
entrypoints: [join(PROJECT_ROOT, "src", "index.ts")],
3131
outdir: DIST_DIR,
3232
target: "browser",
3333
minify: true,
3434
});
3535
if (!result.success) {
36-
console.error("Build failed:");
3736
for (const log of result.logs) {
3837
console.error(log);
3938
}
4039
process.exit(1);
4140
}
42-
console.log(" → playground/dist/index.js");
41+
console.info(" → playground/dist/index.js");
4342
}
4443

45-
function copyTypeScript() {
44+
async function copyTypeScript(): Promise<void> {
4645
const src = join(PROJECT_ROOT, "node_modules", "typescript", "lib", "typescript.js");
4746
const dest = join(DIST_DIR, "typescript.js");
48-
if (!existsSync(src)) {
49-
console.warn("⚠ TypeScript compiler not found at", src);
50-
console.warn(" Run `npm install` first. CDN fallback will be used.");
47+
const srcFile = Bun.file(src);
48+
if (!(await srcFile.exists())) {
49+
console.warn("⚠ TypeScript compiler not found. Run `npm install` first.");
5150
return;
5251
}
53-
copyFileSync(src, dest);
54-
console.log(" → playground/dist/typescript.js");
52+
await Bun.write(dest, srcFile);
53+
console.info(" → playground/dist/typescript.js");
5554
}
5655

57-
async function main() {
56+
async function main(): Promise<void> {
5857
if (!existsSync(DIST_DIR)) {
5958
mkdirSync(DIST_DIR, { recursive: true });
6059
}
6160

6261
await buildBundle();
63-
copyTypeScript();
62+
await copyTypeScript();
6463

65-
console.log(`\nPlayground ready at http://localhost:${PORT}\n`);
64+
console.info(`\nPlayground ready at http://localhost:${PORT}\n`);
6665

6766
Bun.serve({
6867
port: PORT,
69-
fetch(req) {
68+
fetch(req: Request): Response {
7069
const url = new URL(req.url);
7170
const pathname = url.pathname === "/" ? "/index.html" : url.pathname;
7271
const filePath = join(PLAYGROUND_DIR, pathname);
@@ -79,7 +78,7 @@ async function main() {
7978
headers: { "Content-Type": contentType },
8079
});
8180
},
82-
error() {
81+
error(): Response {
8382
return new Response("Not found", { status: 404 });
8483
},
8584
});

0 commit comments

Comments
 (0)