Skip to content

Commit a6fe89b

Browse files
committed
feat: add gitpkg sdk packaging
Signed-off-by: yuguorui <yuguorui@pku.edu.cn>
1 parent 63e8789 commit a6fe89b

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

.github/workflows/sdk-gitpkg.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
name: sdk-gitpkg
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
gitpkg:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
19+
- uses: ./.github/actions/setup-bun
20+
21+
- uses: actions/setup-node@v4
22+
with:
23+
node-version: "24"
24+
25+
- name: Read metadata
26+
id: meta
27+
run: |
28+
VERSION=$(node -p "require('./packages/sdk/js/package.json').version")
29+
SHA=$(git rev-parse --short HEAD)
30+
echo "version=$VERSION" >> $GITHUB_OUTPUT
31+
echo "sha=$SHA" >> $GITHUB_OUTPUT
32+
33+
- name: Check release
34+
env:
35+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
run: |
37+
TAG="@opencode-ai/sdk-${{ steps.meta.outputs.version }}-gitpkg"
38+
if gh release view "$TAG" >/dev/null 2>&1; then
39+
echo "Release already exists: $TAG"
40+
exit 1
41+
fi
42+
if git ls-remote --tags origin "refs/tags/$TAG" | grep -q .; then
43+
echo "Tag already exists: $TAG"
44+
exit 1
45+
fi
46+
47+
- name: Build sdk
48+
run: bun run --cwd packages/sdk/js build
49+
50+
- name: Prepare gitpkg exports
51+
run: bun ./packages/sdk/js/script/gitpkg.ts
52+
53+
- name: Pack sdk
54+
working-directory: packages/sdk/js
55+
run: bun pm pack
56+
57+
- name: Rename tarball
58+
id: pack
59+
working-directory: packages/sdk/js
60+
run: |
61+
FILE=$(ls opencode-ai-sdk-*.tgz)
62+
NAME="opencode-ai-sdk-${{ steps.meta.outputs.version }}-gitpkg-${{ steps.meta.outputs.sha }}.tgz"
63+
mv "$FILE" "$NAME"
64+
echo "tarball=$NAME" >> $GITHUB_OUTPUT
65+
66+
- name: Commit gitpkg
67+
run: |
68+
git config user.email "github-actions@users.noreply.github.com"
69+
git config user.name "github-actions"
70+
git add -f packages/sdk/js/dist
71+
git add packages/sdk/js/package.json
72+
git commit -m "chore: prepare sdk gitpkg ${{ steps.meta.outputs.version }}"
73+
74+
- name: Create root tag commit
75+
run: |
76+
git subtree split --prefix=packages/sdk/js -b sdk-gitpkg-root
77+
78+
- name: Tag gitpkg
79+
run: |
80+
TAG="@opencode-ai/sdk-${{ steps.meta.outputs.version }}-gitpkg"
81+
git tag "$TAG" sdk-gitpkg-root
82+
git push origin "$TAG"
83+
84+
- name: Create release
85+
env:
86+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87+
run: |
88+
TAG="@opencode-ai/sdk-${{ steps.meta.outputs.version }}-gitpkg"
89+
FILE="packages/sdk/js/${{ steps.pack.outputs.tarball }}"
90+
gh release create "$TAG" "$FILE" --title "$TAG" --notes "Commit: ${{ steps.meta.outputs.sha }}"

packages/sdk/js/script/gitpkg.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bun
2+
3+
const dir = new URL("..", import.meta.url).pathname
4+
process.chdir(dir)
5+
6+
const pkg = await import("../package.json").then((m) => m.default)
7+
const next = JSON.parse(JSON.stringify(pkg))
8+
9+
const items = Object.entries(next.exports)
10+
for (const item of items) {
11+
const key = item[0]
12+
const value = item[1]
13+
const data =
14+
typeof value === "object" && value !== null && "import" in value
15+
? (value as { import?: unknown }).import
16+
: undefined
17+
const text = typeof value === "string" ? value : data
18+
if (typeof text !== "string") continue
19+
const file = text.replace("./src/", "./dist/").replace(".ts", "")
20+
next.exports[key] = {
21+
import: file + ".js",
22+
types: file + ".d.ts",
23+
}
24+
}
25+
26+
await Bun.write("package.json", JSON.stringify(next, null, 2))

0 commit comments

Comments
 (0)