Skip to content

Commit c04f9a0

Browse files
authored
add npm publish script for easier npm publish (#317)
* add npm publish script for easier npm publish * fix formatting * fix dist tag env * add readme to package
1 parent 6d5e455 commit c04f9a0

File tree

3 files changed

+79
-31
lines changed

3 files changed

+79
-31
lines changed

.github/workflows/publish-npm.yml

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ name: Publish NPM
33
on:
44
workflow_dispatch:
55
inputs:
6-
dry-run:
7-
type: boolean
8-
default: true
9-
description: Dry run publish
106
dist-tag:
117
type: choice
128
options:
@@ -30,32 +26,9 @@ jobs:
3026
cache: 'npm'
3127
registry-url: 'https://registry.npmjs.org'
3228

33-
- name: Install dependencies
34-
run: npm ci --ignore-scripts
35-
36-
- name: Set package name from vars
37-
if: ${{ vars.NPM_PACKAGE_NAME }}
38-
run: |
39-
cat package.json | jq -r '.name = "${{ vars.NPM_PACKAGE_NAME }}"' > package.json.tmp
40-
mv package.json.tmp package.json
41-
42-
- name: Set version from commit
43-
if: ${{ inputs.dist-tag == 'dev' }}
44-
run: |
45-
npm version --no-git-tag-version $(cat package.json | jq .version -r)-dev.$(git rev-parse --short HEAD)
46-
47-
- name: Create git tag
48-
run: git tag $(cat package.json | jq .version -r)
49-
50-
- name: Publish dev package (Dry run)
51-
if: ${{ inputs.dry-run == true }}
52-
run: npm publish --tag "${{ inputs.dist-tag }}" --dry-run
53-
54-
- name: Publish dev package
55-
if: ${{ inputs.dry-run == false }}
29+
- name: Publish package
5630
env:
5731
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
58-
run: |
59-
npm publish --tag "${{ inputs.dist-tag }}"
60-
git tag v$(cat package.json | jq .version -r)
61-
git push origin v$(cat package.json | jq .version -r)
32+
NPM_PUBLISH_TAG: ${{ inputs.dist-tag }}
33+
NPM_PACKAGE_SCOPE: ${{ vars.NPM_PACKAGE_SCOPE }}
34+
run: ./publish.sh

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,43 @@ Account Creation Workflow
135135
![Account Creation](/diagrams/account-creation-workflow.png)
136136
137137
![Account Creation Webpage](/diagrams/account-creation-webpage-workflow.png)
138+
139+
# Publishing
140+
141+
This package is published on npm: https://www.npmjs.com/package/signify-ts.
142+
143+
If you need to publish a version under your own scope, you can use the [publish script](./publish.sh). This enables you to create development packages. For example:
144+
145+
```bash
146+
NPM_PACKAGE_SCOPE=@myorg DRY_RUN=1 ./publish.sh
147+
npm notice Tarball Details
148+
npm notice name: @myorg/signify-ts
149+
npm notice version: 0.3.0-rc1-dev.8fa9919
150+
npm notice filename: myorg-signify-ts-0.3.0-rc1-dev.8fa9919.tgz
151+
npm notice package size: 81.0 kB
152+
npm notice unpacked size: 370.0 kB
153+
npm notice shasum: 8c160bc99d9ec552e6c478c20922cae3388a8ace
154+
npm notice integrity: sha512-WRuD5PKFN3WBl[...]xVieCIS0UpVeg==
155+
npm notice total files: 96
156+
npm notice
157+
npm notice Publishing to https://registry.npmjs.org/ with tag dev and default access (dry-run)
158+
159+
```
160+
161+
Set the `NPM_PUBLISH_TAG` to `latest` to skip the commit hash suffix in the version:
162+
163+
```bash
164+
NPM_PUBLISH_TAG=latest NPM_PACKAGE_SCOPE=@myorg DRY_RUN=1 ./publish.sh
165+
npm notice Tarball Details
166+
npm notice name: @myorg/signify-ts
167+
npm notice version: 0.3.0-rc1
168+
npm notice filename: myorg-signify-ts-0.3.0-rc1.tgz
169+
npm notice package size: 80.9 kB
170+
npm notice unpacked size: 370.0 kB
171+
npm notice shasum: 8c9e4edcf19802e8acaf5996a36061a9c335b1c4
172+
npm notice integrity: sha512-V1y2W3zs4Ccsn[...]vKY3WWgalcuBQ==
173+
npm notice total files: 96
174+
npm notice
175+
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access (dry-run)
176+
177+
```

publish.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
set -e
3+
4+
package_info=$(cat package.json)
5+
scope=${NPM_PACKAGE_SCOPE:-$(echo "$package_info" | jq .name -r | grep '/' | cut -d'/' -f1)}
6+
name=${NPM_PACKAGE_NAME:-$(echo "$package_info" | jq .name -r | cut -d'/' -f2)}
7+
8+
version=$(echo "$package_info" | jq .version -r)
9+
tag=${NPM_PUBLISH_TAG:-dev}
10+
11+
if [ "$scope" != "" ]; then
12+
name="${scope}/${name}"
13+
fi
14+
15+
if [ "$tag" = "dev" ]; then
16+
version="${version}-dev.$(git rev-parse --short HEAD)"
17+
fi
18+
19+
npm ci
20+
npm run build
21+
22+
# Creating a temporary directory for publishing.
23+
#
24+
# This allows us to modify the version and name of the published package
25+
# without having to commit changes to the repo. Which is useful for tagged
26+
# and scoped package releases.
27+
publish_dir="$(mktemp -d)"
28+
cp -r README.md LICENSE dist package.json package-lock.json "${publish_dir}/"
29+
jq ".version = \"${version}\" | .name = \"${name}\" | del(.scripts.prepare)" package.json > "${publish_dir}/package.json"
30+
31+
if [ -z "$DRY_RUN" ]; then
32+
npm publish "${publish_dir}" --tag "${tag}"
33+
else
34+
npm publish "${publish_dir}" --tag "${tag}" --dry-run
35+
fi

0 commit comments

Comments
 (0)