Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
139 lines (109 loc) · 4.12 KB
/
Copy pathutils.ts
File metadata and controls
139 lines (109 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { chmod, copyFile, mkdtemp, rm } from "node:fs/promises"
import { tmpdir } from "node:os"
import { dirname, join } from "node:path"
import { Option, Schema } from "effect"
const CLI_VERSION = "dev"
const Manifest = Schema.fromJsonString(Schema.Struct({ version: Schema.NonEmptyString }))
export type Channel = "dev" | "beta" | "prod"
export function resolveChannel(): Channel {
const raw = process.env.OPENCODE_CHANNEL
if (raw === "dev" || raw === "beta" || raw === "prod") return raw
if (raw === "latest") return "prod"
return "dev"
}
export const CLI_BINARIES: Array<{ target: string; package: string; os: string; cpu: string }> = [
{
target: "darwin-arm64",
package: "@opencode/cli-darwin-arm64",
os: "darwin",
cpu: "arm64",
},
{
target: "darwin-x64",
package: "@opencode/cli-darwin-x64-baseline",
os: "darwin",
cpu: "x64",
},
{
target: "windows-arm64",
package: "@opencode/cli-windows-arm64",
os: "win32",
cpu: "arm64",
},
{
target: "windows-x64",
package: "@opencode/cli-windows-x64-baseline",
os: "win32",
cpu: "x64",
},
{
target: "linux-x64",
package: "@opencode/cli-linux-x64-baseline",
os: "linux",
cpu: "x64",
},
{
target: "linux-arm64",
package: "@opencode/cli-linux-arm64",
os: "linux",
cpu: "arm64",
},
]
function nativeTarget() {
const { platform, arch } = process
if (platform === "darwin") return arch === "arm64" ? "darwin-arm64" : "darwin-x64"
if (platform === "win32") return arch === "arm64" ? "windows-arm64" : "windows-x64"
if (platform === "linux") return arch === "arm64" ? "linux-arm64" : "linux-x64"
throw new Error(`Unsupported platform: ${platform}/${arch}`)
}
export function getCurrentCli(target = process.env.OPENCODE_TARGET ?? nativeTarget()) {
const binaryConfig = CLI_BINARIES.find((item) => item.target === target)
if (!binaryConfig) throw new Error(`CLI configuration not available for target '${target}'`)
return binaryConfig
}
export function nodePtyPackage(target?: string) {
const cli = getCurrentCli(target)
return `@lydell/node-pty-${cli.os}-${cli.cpu}`
}
export async function downloadCliToResources(version = CLI_VERSION, dest = windowsify("resources/opencode-cli")) {
const { $ } = await import("bun")
const cli = getCurrentCli()
const directory = await mkdtemp(join(tmpdir(), "opencode-cli-"))
try {
await $`bun install --no-save --cwd ${directory} ${`${cli.package}@${version}`} ${`--os=${cli.os}`} ${`--cpu=${cli.cpu}`}`
await copyCliToResources(join(directory, "node_modules", cli.package), dest)
} finally {
await rm(directory, { recursive: true, force: true })
}
console.log(`Copied ${cli.package}@${version} to ${dest}`)
}
export async function copyBuiltCliToResources(root: string, dest = windowsify("resources/opencode-cli")) {
const cli = getCurrentCli()
const directory = cli.package.replace("@opencode/", "")
await copyCliToResources(join(root, directory), dest)
}
// The package directory is an npm package: its package.json version is the string the executable
// prints for --version. Writing it next to the executable spares the desktop a ~400 ms spawn of the
// 200 MB binary on first launch.
async function copyCliToResources(pkg: string, dest: string) {
const cli = getCurrentCli()
await copyFile(join(pkg, "bin", cli.os === "win32" ? "opencode.exe" : "opencode"), dest)
await prepareCli(dest)
const manifest = Option.getOrUndefined(
Schema.decodeUnknownOption(Manifest)(await Bun.file(join(pkg, "package.json")).text()),
)
if (!manifest) throw new Error(`Bundled CLI package has no version: ${pkg}`)
await Bun.write(versionFile(dest), manifest.version)
}
export function versionFile(cli: string) {
return join(dirname(cli), "opencode-cli.version")
}
async function prepareCli(dest: string) {
const { $ } = await import("bun")
if (process.platform !== "win32") await chmod(dest, 0o755)
if (process.platform === "darwin") await $`codesign --force --sign - ${dest}`
}
export function windowsify(path: string) {
if (path.endsWith(".exe")) return path
return `${path}${process.platform === "win32" ? ".exe" : ""}`
}