Skip to content

Commit 608a77a

Browse files
committed
feat: add Node.js entry point and build script
Adds src/node.ts as a Node.js entry point that starts the server on port 1338, and script/build-node.ts that bundles it with DB migrations embedded at build time via Bun.build. Note: depends on the Hono/node-server server refactoring (opencode-2-0) for Server.listen() to return a proper async listener.
1 parent 6fcc970 commit 608a77a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bun
2+
3+
import fs from "fs"
4+
import path from "path"
5+
import { fileURLToPath } from "url"
6+
7+
const __filename = fileURLToPath(import.meta.url)
8+
const __dirname = path.dirname(__filename)
9+
const dir = path.resolve(__dirname, "..")
10+
11+
process.chdir(dir)
12+
13+
// Load migrations from migration directories
14+
const migrationDirs = (
15+
await fs.promises.readdir(path.join(dir, "migration"), {
16+
withFileTypes: true,
17+
})
18+
)
19+
.filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
20+
.map((entry) => entry.name)
21+
.sort()
22+
23+
const migrations = await Promise.all(
24+
migrationDirs.map(async (name) => {
25+
const file = path.join(dir, "migration", name, "migration.sql")
26+
const sql = await Bun.file(file).text()
27+
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
28+
const timestamp = match
29+
? Date.UTC(
30+
Number(match[1]),
31+
Number(match[2]) - 1,
32+
Number(match[3]),
33+
Number(match[4]),
34+
Number(match[5]),
35+
Number(match[6]),
36+
)
37+
: 0
38+
return { sql, timestamp, name }
39+
}),
40+
)
41+
console.log(`Loaded ${migrations.length} migrations`)
42+
43+
await Bun.build({
44+
target: "node",
45+
entrypoints: ["./src/node.ts"],
46+
outdir: "./dist",
47+
format: "esm",
48+
external: ["jsonc-parser"],
49+
define: {
50+
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
51+
},
52+
})
53+
54+
console.log("Build complete")

packages/opencode/src/node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Server } from "./server/server"
2+
3+
const result = await Server.listen({
4+
port: 1338,
5+
hostname: "0.0.0.0",
6+
})
7+
8+
console.log(result)

0 commit comments

Comments
 (0)