Skip to content

Commit b40175f

Browse files
committed
feat: initialize NPM package and add publishing workflow
1 parent 4a1082e commit b40175f

File tree

5 files changed

+538
-3
lines changed

5 files changed

+538
-3
lines changed

.github/workflows/npm-publish.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Publish NPM Package
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
- '[0-9]+.[0-9]+.[0-9]+'
8+
9+
jobs:
10+
publish-npm:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout repository
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Node.js
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: '20'
24+
registry-url: 'https://registry.npmjs.org'
25+
26+
- name: Extract version from tag
27+
id: get_version
28+
run: |
29+
TAG_NAME="${GITHUB_REF#refs/tags/}"
30+
# Remove 'v' prefix if present
31+
VERSION="${TAG_NAME#v}"
32+
echo "version=$VERSION" >> $GITHUB_OUTPUT
33+
echo "Publishing version: $VERSION"
34+
35+
- name: Install dependencies
36+
run: npm ci
37+
38+
- name: Run tests
39+
run: npm test
40+
41+
- name: Update package version
42+
working-directory: ./src/stimulus/assets
43+
run: |
44+
npm version ${{ steps.get_version.outputs.version }} --no-git-tag-version
45+
echo "Updated package.json to version ${{ steps.get_version.outputs.version }}"
46+
47+
- name: Publish to NPM
48+
working-directory: ./src/stimulus/assets
49+
run: npm publish --provenance
50+
env:
51+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
52+
53+
- name: Create GitHub Release
54+
uses: actions/github-script@v7
55+
with:
56+
script: |
57+
const tag = context.ref.replace('refs/tags/', '');
58+
const version = tag.replace(/^v/, '');
59+
60+
await github.rest.repos.createRelease({
61+
owner: context.repo.owner,
62+
repo: context.repo.repo,
63+
tag_name: tag,
64+
name: `@web-auth/webauthn-stimulus v${version}`,
65+
body: `Published version ${version} to NPM: https://www.npmjs.com/package/@web-auth/webauthn-stimulus/v/${version}`,
66+
draft: false,
67+
prerelease: version.includes('-')
68+
});

src/stimulus/PUBLISHING.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Publishing the NPM Package @web-auth/webauthn-stimulus
2+
3+
## Automatic Publishing (Recommended)
4+
5+
The package is automatically published to NPM when a Git tag is created. The GitHub Actions workflow triggers automatically.
6+
7+
### Process:
8+
9+
1. Create a Git tag with the version number:
10+
```bash
11+
git tag 5.2.3
12+
git push origin 5.2.3
13+
```
14+
15+
2. The GitHub Actions workflow will:
16+
- Detect the new tag
17+
- Install dependencies
18+
- Run tests
19+
- Update the version in package.json
20+
- Publish to NPM
21+
- Create a GitHub release
22+
23+
## Manual Publishing
24+
25+
If you need to publish manually (e.g., for testing):
26+
27+
### Prerequisites:
28+
- NPM account with write access to the `@web-auth/webauthn-stimulus` package
29+
- Configured NPM token or local NPM authentication
30+
31+
### Steps:
32+
33+
1. **Navigate to the project root:**
34+
```bash
35+
cd /path/to/webauthn-framework
36+
```
37+
38+
2. **Install dependencies:**
39+
```bash
40+
npm ci
41+
```
42+
43+
3. **Run tests:**
44+
```bash
45+
# Tests must be run from the root because the project uses npm workspaces
46+
npm test
47+
# OR use castor (recommended):
48+
# php castor.php js
49+
```
50+
51+
4. **Update the version:**
52+
```bash
53+
cd src/stimulus/assets
54+
# Replace X.Y.Z with the desired version (must match the Git tag)
55+
npm version X.Y.Z --no-git-tag-version
56+
cd ../../..
57+
```
58+
59+
5. **Log in to NPM (if necessary):**
60+
```bash
61+
npm login
62+
# OR with a token:
63+
# export NPM_TOKEN=your-npm-token
64+
# echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
65+
```
66+
67+
6. **Publish to NPM:**
68+
```bash
69+
cd src/stimulus/assets
70+
npm publish --access public
71+
cd ../../..
72+
```
73+
74+
7. **Verify the publication:**
75+
```bash
76+
npm view @web-auth/webauthn-stimulus
77+
```
78+
79+
### Important notes on workspaces:
80+
81+
The project uses **npm workspaces**, so:
82+
- Development dependencies (Babel, Jest, etc.) are in the root `package.json`
83+
- Tests must be run from the root with `npm test`
84+
- The package itself is in `src/stimulus/assets/` and that's where you publish from
85+
86+
### Test Publishing (dry-run):
87+
88+
To test the publication without actually publishing:
89+
90+
```bash
91+
cd src/stimulus/assets
92+
npm pack --dry-run
93+
```
94+
95+
This will show what would be included in the package without publishing it.
96+
97+
### Unpublish a Publication (within 72h):
98+
99+
If you need to cancel a publication:
100+
101+
```bash
102+
npm unpublish @web-auth/webauthn-stimulus@X.Y.Z
103+
```
104+
105+
⚠️ **Warning**: This should only be used in case of a critical error.
106+
107+
## Important Notes:
108+
109+
- The version in `package.json` is `0.0.0-dev` by default
110+
- It is replaced by the tag version during publication
111+
- The package is published from `src/stimulus/assets/`
112+
- Only files listed in the `files` property of `package.json` are included
113+
- Tests must pass before publication (enforced by CI/CD)
114+
115+
## Post-Publication Verification:
116+
117+
1. Check on NPM: https://www.npmjs.com/package/@web-auth/webauthn-stimulus
118+
2. Test installation in a project:
119+
```bash
120+
npm install @web-auth/webauthn-stimulus@X.Y.Z
121+
```

0 commit comments

Comments
 (0)