Skip to content

Commit 55140cb

Browse files
committed
bun: xmit.json, cleanups
1 parent 16734d7 commit 55140cb

4 files changed

Lines changed: 34 additions & 31 deletions

File tree

‎bun/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xmit.co/bob",
3-
"version": "0.0.1",
3+
"version": "0.0.3",
44
"description": "The JS code for Oncle Bob",
55
"type": "module",
66
"bin": {
@@ -46,7 +46,8 @@
4646
"bun": ">=1.0.0"
4747
},
4848
"dependencies": {
49-
"@iarna/toml": "^2.2.5"
49+
"@iarna/toml": "^2.2.5",
50+
"json5": "^2.2.3"
5051
},
5152
"devDependencies": {
5253
"@types/bun": "latest"

‎bun/src/cli.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bun
22
import { serve } from "./server";
3-
import { readFileSync, existsSync } from "fs";
3+
import { file } from "bun";
44
import { resolve, join } from "path";
55
import type { PackageJson } from "./config";
66

@@ -29,7 +29,7 @@ async function main() {
2929

3030
while (currentDir !== "/") {
3131
const candidatePath = join(currentDir, "package.json");
32-
if (existsSync(candidatePath)) {
32+
if (await file(candidatePath).exists()) {
3333
packageJsonPath = candidatePath;
3434
break;
3535
}
@@ -43,7 +43,7 @@ async function main() {
4343

4444
let pkg: PackageJson;
4545
try {
46-
const pkgContent = readFileSync(packageJsonPath, "utf-8");
46+
const pkgContent = await file(packageJsonPath).text();
4747
pkg = JSON.parse(pkgContent);
4848
} catch (err) {
4949
console.error(`Error reading package.json: ${err}`);
@@ -59,7 +59,7 @@ async function main() {
5959
const projectRoot = resolve(packageJsonPath, "..");
6060
const absoluteDirectory = resolve(projectRoot, directory);
6161

62-
if (!existsSync(absoluteDirectory)) {
62+
if (!(await file(absoluteDirectory).exists())) {
6363
console.error(`Error: Directory "${directory}" does not exist (resolved to ${absoluteDirectory})`);
6464
process.exit(1);
6565
}

‎bun/src/server.ts‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { file, type Server } from "bun";
22
import { parse as parseToml } from "@iarna/toml";
3+
import JSON5 from "json5";
34
import { join, resolve } from "path";
4-
import { existsSync, statSync } from "fs";
55
import type { XmitConfig } from "./config";
66
import { v4 as uuidv4 } from "./uuid";
77

@@ -11,14 +11,10 @@ interface ServeOptions {
1111
hostname?: string;
1212
}
1313

14-
function openFile(path: string): string | null {
15-
try {
16-
const stat = statSync(path);
17-
if (!stat.isDirectory()) {
18-
return path;
19-
}
20-
} catch {
21-
// File doesn't exist or can't be accessed
14+
async function openFile(path: string): Promise<string | null> {
15+
const f = file(path);
16+
if (await f.exists() && f.size > 0) {
17+
return path;
2218
}
2319
return null;
2420
}
@@ -106,19 +102,25 @@ export async function createServer(options: ServeOptions): Promise<Server> {
106102
port,
107103
hostname,
108104
async fetch(request: Request): Promise<Response> {
109-
// Load xmit.toml configuration
105+
// Load xmit.json (JSON5) or xmit.toml configuration
110106
let config: XmitConfig = {};
111-
const cfgPath = join(absDirectory, "xmit.toml");
107+
const jsonPath = join(absDirectory, "xmit.json");
108+
const tomlPath = join(absDirectory, "xmit.toml");
109+
110+
const jsonFile = file(jsonPath);
111+
const tomlFile = file(tomlPath);
112112

113-
try {
114-
const cfgFile = file(cfgPath);
115-
if (await cfgFile.exists()) {
116-
const cfgText = await cfgFile.text();
117-
config = parseToml(cfgText) as XmitConfig;
113+
if (await jsonFile.exists()) {
114+
try {
115+
config = JSON5.parse(await jsonFile.text()) as XmitConfig;
116+
} catch (err) {
117+
console.warn(`⚠️ ${jsonPath}: ${err}`);
118118
}
119-
} catch (err) {
120-
if (existsSync(cfgPath)) {
121-
console.warn(`⚠️ ${cfgPath}: ${err}`);
119+
} else if (await tomlFile.exists()) {
120+
try {
121+
config = parseToml(await tomlFile.text()) as XmitConfig;
122+
} catch (err) {
123+
console.warn(`⚠️ ${tomlPath}: ${err}`);
122124
}
123125
}
124126

@@ -197,18 +199,18 @@ export async function createServer(options: ServeOptions): Promise<Server> {
197199
const path = url.pathname.slice(1); // Remove leading /
198200
const originalPath = join(absDirectory, path);
199201
let realPath = originalPath;
200-
let foundFile = openFile(realPath);
202+
let foundFile = await openFile(realPath);
201203

202204
// Try index.html in directory
203205
if (!foundFile) {
204206
realPath = join(originalPath, "index.html");
205-
foundFile = openFile(realPath);
207+
foundFile = await openFile(realPath);
206208
}
207209

208210
// Try .html extension
209211
if (!foundFile) {
210212
realPath = originalPath + ".html";
211-
foundFile = openFile(realPath);
213+
foundFile = await openFile(realPath);
212214
}
213215

214216
// Try redirects
@@ -230,15 +232,15 @@ export async function createServer(options: ServeOptions): Promise<Server> {
230232
// Try fallback
231233
if (!foundFile && config.fallback) {
232234
realPath = join(absDirectory, config.fallback);
233-
foundFile = openFile(realPath);
235+
foundFile = await openFile(realPath);
234236
}
235237

236238
// Try 404 page
237239
if (!foundFile) {
238240
status = 404;
239241
if (config["404"]) {
240242
realPath = join(absDirectory, config["404"]);
241-
foundFile = openFile(realPath);
243+
foundFile = await openFile(realPath);
242244
}
243245
}
244246

‎flake.nix‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
{
1919
devShell = pkgs.mkShell {
2020
buildInputs = with pkgs; [
21-
deno
21+
bun
2222
flutter
2323
pnpm
2424
];

0 commit comments

Comments
 (0)