Skip to content

Commit 9039243

Browse files
authored
Add script and CI for publishing JS clients (#76)
1 parent 4adf466 commit 9039243

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

.changeset/clever-geese-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-solana-program": patch
3+
---
4+
5+
Add script and CI workflow for publishing JS clients
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Publish JS Client
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
level:
7+
description: Version level
8+
required: true
9+
default: patch
10+
type: choice
11+
options:
12+
- patch
13+
- minor
14+
- major
15+
- prerelease
16+
- prepatch
17+
- preminor
18+
- premajor
19+
tag:
20+
description: NPM Tag (and preid for pre-releases)
21+
required: true
22+
type: string
23+
default: latest
24+
create_release:
25+
description: Create a GitHub release
26+
required: true
27+
type: boolean
28+
default: true
29+
30+
jobs:
31+
test_js:
32+
name: Test JS client
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Git Checkout
36+
uses: actions/checkout@v4
37+
38+
- name: Setup Environment
39+
uses: ./.github/actions/setup
40+
with:
41+
cargo-cache-key: cargo-programs
42+
solana: true
43+
{% if programFramework === 'anchor' %}
44+
anchor: true
45+
{% endif %}
46+
47+
- name: Format JS Client
48+
run: pnpm clients:js:format
49+
50+
- name: Lint JS Client
51+
run: pnpm clients:js:lint
52+
53+
- name: Build Programs
54+
run: pnpm programs:build
55+
56+
- name: Test JS Client
57+
run: pnpm clients:js:test
58+
59+
publish_js:
60+
name: Publish JS client
61+
runs-on: ubuntu-latest
62+
needs: test_js
63+
permissions:
64+
contents: write
65+
steps:
66+
- name: Git Checkout
67+
uses: actions/checkout@v4
68+
69+
- name: Setup Environment
70+
uses: ./.github/actions/setup
71+
72+
- name: Ensure NPM_TOKEN variable is set
73+
env:
74+
token: {% raw %}${{ secrets.NPM_TOKEN }}{% endraw %}
75+
if: {% raw %}${{ env.token == '' }}{% endraw %}
76+
run: |
77+
echo "The NPM_TOKEN secret variable is not set"
78+
echo "Go to \"Settings\" -> \"Secrets and variables\" -> \"Actions\" -> \"New repository secret\"."
79+
exit 1
80+
81+
- name: NPM Authentication
82+
run: pnpm config set '//registry.npmjs.org/:_authToken' "${NODE_AUTH_TOKEN}"
83+
env:
84+
NODE_AUTH_TOKEN: {% raw %}${{ secrets.NPM_TOKEN }}{% endraw %}
85+
86+
- name: Set Git Author
87+
run: |
88+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
89+
git config --global user.name "github-actions[bot]"
90+
91+
- name: Publish JS Client
92+
id: publish
93+
run: pnpm clients:js:publish {% raw %}${{ inputs.level }} ${{ inputs.tag }}{% endraw %}
94+
95+
- name: Push Commit and Tag
96+
run: git push origin --follow-tags
97+
98+
- name: Create GitHub release
99+
if: github.event.inputs.create_release == 'true'
100+
uses: ncipollo/release-action@v1
101+
with:
102+
tag: {% raw %}js@v${{ steps.publish.outputs.new_version }}{% endraw %}

template/clients/js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"scripts": {
33
"clients:js:format": "zx ./scripts/client/format-js.mjs",
44
"clients:js:lint": "zx ./scripts/client/lint-js.mjs",
5+
"clients:js:publish": "zx ./scripts/client/publish-js.mjs",
56
"clients:js:test": "zx ./scripts/client/test-js.mjs"
67
},
78
"devDependencies": {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env zx
2+
import 'zx/globals';
3+
import { cliArguments, workingDirectory } from '../utils.mjs';
4+
5+
const [level, tag = 'latest'] = cliArguments();
6+
if (!level) {
7+
throw new Error('A version level — e.g. "path" — must be provided.');
8+
}
9+
10+
// Go to the client directory and install the dependencies.
11+
cd(path.join(workingDirectory, 'clients', 'js'));
12+
await $`pnpm install`;
13+
14+
// Update the version.
15+
const versionArgs = [
16+
'--no-git-tag-version',
17+
...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
18+
];
19+
let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
20+
const newVersion = stdout.slice(1).trim();
21+
22+
// Expose the new version to CI if needed.
23+
if (process.env.CI) {
24+
await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
25+
}
26+
27+
// Publish the package.
28+
// This will also build the package before publishing (see prepublishOnly script).
29+
await $`pnpm publish --no-git-checks --tag ${tag}`;
30+
31+
// Commit the new version.
32+
await $`git commit -am "Publish JS client v${newVersion}"`;
33+
34+
// Tag the new version.
35+
await $`git tag -a js@v${newVersion} -m "JS client v${newVersion}"`;

0 commit comments

Comments
 (0)