Skip to content

Commit 5aaa765

Browse files
committed
chore: release and publish from github actions
1 parent 484dd19 commit 5aaa765

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: CI
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
- '8.0' # remove this after 8.0 is merged into main
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
cache: 'pnpm'
19+
20+
- uses: pnpm/action-setup@v4
21+
with:
22+
version: 10.14.0
23+
run_install: false
24+
25+
- run: pnpm install --frozen-lockfile
26+
- run: pnpm run lint
27+
- run: pnpm run typecheck
28+
- run: pnpm run build
29+
- run: pnpm run publint

.github/workflows/release.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Release
2+
3+
on:
4+
workflow_dispatch:
5+
6+
permissions:
7+
id-token: write
8+
contents: write
9+
10+
jobs:
11+
publish:
12+
runs-on: ubuntu-latest
13+
environment: npm-publish
14+
15+
steps:
16+
# ───── 基础环境 ─────
17+
- uses: actions/checkout@v4
18+
19+
- uses: actions/setup-node@v4
20+
with:
21+
node-version: 22
22+
cache: pnpm
23+
24+
- uses: pnpm/action-setup@v4
25+
with:
26+
version: 10.14.0
27+
run_install: false
28+
29+
- run: pnpm install --frozen-lockfile
30+
31+
- id: pkg
32+
run: echo "version=$(node -p \"require('./package.json').version\")" >> "$GITHUB_OUTPUT"
33+
34+
- id: tag
35+
run: echo "tag=$(pnpm exec jiti scripts/dist-tag.ts '${{ steps.pkg.outputs.version }}')" >> "$GITHUB_OUTPUT"
36+
37+
- run: pnpm exec jiti scripts/changelog.ts '${{ steps.pkg.outputs.version }}' > tmp_release_body.md
38+
39+
- run: corepack prepare [email protected] --activate
40+
41+
- name: Publish to npm
42+
run: |
43+
pnpm exec [email protected] publish \
44+
--tag '${{ steps.tag.outputs.tag }}' \
45+
--access public
46+
47+
- uses: softprops/action-gh-release@v2
48+
with:
49+
tag_name: v${{ steps.pkg.outputs.version }}
50+
body_path: tmp_release_body.md
51+
prerelease: ${{ startsWith(steps.pkg.outputs.version, '0') || contains(steps.pkg.outputs.version, '-') }}
52+
env:
53+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

pnpm-workspace.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
onlyBuiltDependencies:
2+
- esbuild

scripts/changelog.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { readFileSync } from "node:fs";
2+
import { join } from "node:path";
3+
4+
/**
5+
* Extract the changelog section for the given version from CHANGELOG.md
6+
* and print it to stdout (without leading/trailing blank lines).
7+
*
8+
* Usage:
9+
* npx jiti scripts/changelog.ts 7.0.3 > tmp_release_body.md
10+
*/
11+
12+
const version = process.argv[2];
13+
14+
if (!version) {
15+
console.error("Usage: jiti scripts/changelog.ts <version>");
16+
process.exit(1);
17+
}
18+
19+
const header = `## ${version}`;
20+
const changelogPath = join(process.cwd(), "CHANGELOG.md");
21+
const lines = readFileSync(changelogPath, "utf8").split(/\r?\n/);
22+
23+
let capturing = false;
24+
const section: string[] = [];
25+
26+
for (const line of lines) {
27+
if (line.startsWith("## ")) {
28+
if (capturing) break; // reached next version header
29+
if (line.trim() === header) {
30+
capturing = true; // start capturing after header
31+
continue; // skip header line itself
32+
}
33+
}
34+
if (capturing) section.push(line);
35+
}
36+
37+
// Trim leading / trailing blank lines
38+
while (section.length && section[0].trim() === "") section.shift();
39+
while (section.length && section[section.length - 1].trim() === "")
40+
section.pop();
41+
42+
if (section.length === 0) {
43+
console.error(`No changelog entry found for version ${version}`);
44+
process.exit(1);
45+
}
46+
47+
console.log(section.join("\n"));

scripts/dist-tag.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** Resolve npm dist-tag from a semver string.
2+
* Usage: jiti scripts/dist-tag.ts 1.2.3-beta.0 # → beta
3+
* jiti scripts/dist-tag.ts # picks version from package.json
4+
*/
5+
import { readFileSync } from "node:fs";
6+
import { join } from "node:path";
7+
8+
// Prefer CLI arg, otherwise read package.json
9+
const version: string =
10+
process.argv[2] ??
11+
JSON.parse(readFileSync(join(process.cwd(), "package.json"), "utf8")).version;
12+
13+
// Capture first recognised prerelease label
14+
const tag =
15+
version.match(/-(alpha|beta|rc)\b/i)?.[1].toLowerCase() ?? "latest";
16+
17+
console.log(tag);

0 commit comments

Comments
 (0)