Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/opencode/script/build-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bun

import fs from "fs"
import path from "path"
import { fileURLToPath } from "url"

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const dir = path.resolve(__dirname, "..")

process.chdir(dir)

// Load migrations from migration directories
const migrationDirs = (
await fs.promises.readdir(path.join(dir, "migration"), {
withFileTypes: true,
})
Comment on lines +15 to +17
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Multi-word variable and single-use variable — AGENTS.md naming rules

Per the repo's style guide, multi-word camelCase names should be avoided in favour of single-word alternatives, and variables used only once should be inlined to reduce total variable count.

  • migrationDirsdirs
  • __filename is used exactly once (to produce __dirname); __dirname is also used exactly once (to produce dir). Both can be collapsed:
Suggested change
await fs.promises.readdir(path.join(dir, "migration"), {
withFileTypes: true,
})
const dir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

)
.filter((entry) => entry.isDirectory() && /^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}/.test(entry.name))
.map((entry) => entry.name)
.sort()

const migrations = await Promise.all(
migrationDirs.map(async (name) => {
const file = path.join(dir, "migration", name, "migration.sql")
const sql = await Bun.file(file).text()
const match = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/.exec(name)
const timestamp = match
? Date.UTC(
Number(match[1]),
Number(match[2]) - 1,
Number(match[3]),
Number(match[4]),
Number(match[5]),
Number(match[6]),
)
: 0
return { sql, timestamp, name }
}),
)
console.log(`Loaded ${migrations.length} migrations`)

await Bun.build({
target: "node",
entrypoints: ["./src/node.ts"],
outdir: "./dist",
format: "esm",
external: ["jsonc-parser"],
define: {
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
},
})

Comment on lines +44 to +53
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Bun.build() result unchecked — build failures will be silently swallowed

Bun.build() (without the compile option) does not throw on failure — it resolves with { success: false, logs: [...] }. As written, the script will always print "Build complete" regardless of whether the bundle actually succeeded, making build errors invisible in CI or local runs.

Suggested change
target: "node",
entrypoints: ["./src/node.ts"],
outdir: "./dist",
format: "esm",
external: ["jsonc-parser"],
define: {
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
},
})
const result = await Bun.build({
target: "node",
entrypoints: ["./src/node.ts"],
outdir: "./dist",
format: "esm",
external: ["jsonc-parser"],
define: {
OPENCODE_MIGRATIONS: JSON.stringify(migrations),
},
})
if (!result.success) {
for (const log of result.logs) console.error(log)
process.exit(1)
}

console.log("Build complete")
1 change: 1 addition & 0 deletions packages/opencode/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Server } from "./server/server"
Loading