-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathpublish.ts
More file actions
executable file
·178 lines (169 loc) · 6.93 KB
/
Copy pathpublish.ts
File metadata and controls
executable file
·178 lines (169 loc) · 6.93 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bun
import { $ } from "bun"
import pkg from "../package.json"
import { Script } from "@synsci/script"
import { fileURLToPath } from "url"
const dir = fileURLToPath(new URL("..", import.meta.url))
process.chdir(dir)
const binaries: Record<string, string> = {}
for (const filepath of new Bun.Glob("@synsci/*/package.json").scanSync({ cwd: "./dist" })) {
const platform = await Bun.file(`./dist/${filepath}`).json()
// never let the meta package list itself (re-runs re-glob the dist dir)
if (platform.name === pkg.name) continue
binaries[platform.name] = platform.version
}
console.log("binaries", binaries)
if (Object.keys(binaries).length === 0) {
throw new Error("No binary packages found in dist/. Did the build step run?")
}
const version = Object.values(binaries)[0]
await $`mkdir -p ./dist/${pkg.name}`
await $`cp -r ./bin ./dist/${pkg.name}/bin`
await $`cp ./script/preinstall.mjs ./dist/${pkg.name}/preinstall.mjs`
await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
await Bun.file(`./dist/${pkg.name}/package.json`).write(
JSON.stringify(
{
name: pkg.name,
bin: {
openscience: `./bin/openscience`,
},
scripts: {
// best-effort: clears a stale global @synsci/cli whose `openscience`
// bin link would make npm refuse the install (EEXIST); never fails
preinstall: "node ./preinstall.mjs || exit 0",
postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
},
version: version,
// npm provenance refuses packages whose repository.url doesn't match
// the repo the workflow ran from (case-sensitive)
repository: {
type: "git",
url: "git+https://github.com/synthetic-sciences/openscience.git",
},
optionalDependencies: binaries,
},
null,
2,
),
)
// Publish platform packages SEQUENTIALLY with retries. Each tarball is ~90MB;
// publishing all 11 in parallel saturates the uplink and npm times out.
const results: PromiseSettledResult<string>[] = []
for (const [name] of Object.entries(binaries)) {
try {
if (!name.includes("windows")) {
await $`chmod 755 ./dist/${name}/bin/openscience`
}
await $`bun pm pack`.cwd(`./dist/${name}`)
let published = false
let lastErr: unknown
for (let attempt = 1; attempt <= 5 && !published; attempt++) {
try {
await $`npm publish *.tgz --access public --tag ${Script.channel}`.cwd(`./dist/${name}`)
published = true
} catch (e) {
lastErr = e
const msg = e instanceof Error ? e.message : String(e)
if (msg.includes("cannot publish over") || msg.includes("previously published")) {
published = true // already on the registry
break
}
console.warn(` retry ${name} (attempt ${attempt})`)
}
}
if (!published) throw lastErr
console.log(` published ${name}`)
results.push({ status: "fulfilled", value: name })
} catch (e) {
results.push({ status: "rejected", reason: e })
}
}
const failed = results.filter((r) => r.status === "rejected")
const succeeded = results.filter((r) => r.status === "fulfilled")
if (failed.length > 0) {
console.error(`${failed.length}/${results.length} binary packages failed to publish:`)
for (const f of failed) console.error(" ", (f as PromiseRejectedResult).reason)
throw new Error("Refusing to publish @synsci/openscience wrapper because one or more platform packages failed")
}
if (succeeded.length > 0) {
console.log(`${succeeded.length}/${results.length} binary packages published successfully`)
}
await $`cd ./dist/${pkg.name} && bun pm pack && npm publish *.tgz --access public --tag ${Script.channel}`
// registries (Homebrew tap, AUR) — non-fatal, npm publish above is what matters
if (!Script.preview) {
try {
// Calculate SHA values
const arm64Sha = await $`sha256sum ./dist/openscience-linux-arm64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const x64Sha = await $`sha256sum ./dist/openscience-linux-x64.tar.gz | cut -d' ' -f1`.text().then((x) => x.trim())
const macX64Sha = await $`sha256sum ./dist/openscience-darwin-x64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const macArm64Sha = await $`sha256sum ./dist/openscience-darwin-arm64.zip | cut -d' ' -f1`.text().then((x) => x.trim())
const [pkgver, _subver = ""] = Script.version.split(/(-.*)/, 2)
// Homebrew formula
const homebrewFormula = [
"# typed: false",
"# frozen_string_literal: true",
"",
"# This file was generated by GoReleaser. DO NOT EDIT.",
"class Openscience < Formula",
` desc "The AI research agent built for the terminal."`,
` homepage "https://github.com/synthetic-sciences/OpenScience"`,
` version "${Script.version.split("-")[0]}"`,
"",
` depends_on "ripgrep"`,
"",
" on_macos do",
" if Hardware::CPU.intel?",
` url "https://github.com/synthetic-sciences/OpenScience/releases/download/v${Script.version}/openscience-darwin-x64.zip"`,
` sha256 "${macX64Sha}"`,
"",
" def install",
' bin.install "openscience"',
" end",
" end",
" if Hardware::CPU.arm?",
` url "https://github.com/synthetic-sciences/OpenScience/releases/download/v${Script.version}/openscience-darwin-arm64.zip"`,
` sha256 "${macArm64Sha}"`,
"",
" def install",
' bin.install "openscience"',
" end",
" end",
" end",
"",
" on_linux do",
" if Hardware::CPU.intel? and Hardware::CPU.is_64_bit?",
` url "https://github.com/synthetic-sciences/OpenScience/releases/download/v${Script.version}/openscience-linux-x64.tar.gz"`,
` sha256 "${x64Sha}"`,
" def install",
' bin.install "openscience"',
" end",
" end",
" if Hardware::CPU.arm? and Hardware::CPU.is_64_bit?",
` url "https://github.com/synthetic-sciences/OpenScience/releases/download/v${Script.version}/openscience-linux-arm64.tar.gz"`,
` sha256 "${arm64Sha}"`,
" def install",
' bin.install "openscience"',
" end",
" end",
" end",
"end",
"",
"",
].join("\n")
const token = process.env.GITHUB_TOKEN
if (!token) {
console.warn("GITHUB_TOKEN not set, skipping homebrew tap update")
} else {
const tap = `https://x-access-token:${token}@github.com/syntheticsciences/homebrew-tap.git`
await $`rm -rf ./dist/homebrew-tap`
await $`git clone ${tap} ./dist/homebrew-tap`
await Bun.file("./dist/homebrew-tap/openscience.rb").write(homebrewFormula)
await $`cd ./dist/homebrew-tap && git add openscience.rb`
await $`cd ./dist/homebrew-tap && git commit -m "Update to v${Script.version}"`
await $`cd ./dist/homebrew-tap && git push`
}
} catch (e) {
console.warn("Registry update (Homebrew/AUR) failed, skipping:", e)
}
}