Skip to content

Publish to npm

Publish to npm #58

Workflow file for this run

name: Publish to npm
on:
workflow_dispatch:
inputs:
version:
description: 'npm version to publish'
type: choice
required: true
options:
# these are defined in ${{vars.versions}}, but ghw doesn't support yet the use of expressions in inputs.
- 'patch (x.y.z+1, bug fixes)'
- 'minor (x.y+1.0, new functionality)'
- 'major (x+1.0.0, api breaking changes)'
dry_run:
description: 'Perform all steps except npm publish'
type: boolean
default: true
required: true
jobs:
build:
if: false
uses: ./.github/workflows/build.yml
publish-npm:
# needs: build
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
with:
ref: github-workflows
fetch-depth: 0
fetch-tags: true
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm ci
#- run: npm run dist
- name: Setup versions in ${{'${{env}}'}}
run: |
set -x
git tag -d v3.2.0 || true
git push --delete origin v3.2.0 || true # for testing purposes only
# It sounds like a joke, but npm version doesn't have a way to print the version of the current package.
# Using python here instead of nodejs because it's too convoluted to just read the whole stdin.
PREV_TAG_VERSION=$(npm version --json | python3 -c 'import json; import sys; print(json.loads(sys.stdin.read()).get("jschardet"))')
PREV_PKG_VERSION="$(git tag --list 'v*' --sort='-version:refname' | head -1)"
echo "::warning::latest tagged version doesn't match current published version ($PREV_PKG_VERSION ≠ $PREV_TAG_VERSION)"
# CHANGELOG="$(git log --pretty='format:%h %s' $PREV_PKG_VERSION..HEAD | grep -v '.yml')"
# echo "PREV_PKG_VERSION=$PREV_PKG_VERSION" >> "$GITHUB_ENV"
echo "PKG_VERSION=${{fromJSON(vars.versions).values[inputs.version]}}" >> "$GITHUB_ENV"
# echo "CHANGELOG<<EOF" >> "$GITHUB_ENV"
# echo "$CHANGELOG" >> "$GITHUB_ENV"
# echo "EOF" >> "$GITHUB_ENV"
- name: Version package
run: "npm version ${{env.PKG_VERSION}} -m \"\
Version %s (${{env.PKG_VERSION}})\
\nx\""
# run: "npm version ${{env.PKG_VERSION}} -m \"\
# Version %s (${{env.PKG_VERSION}})\
# \n\
# \n\
# Changes since ${{env.PREV_PKG_VERSION}}:\
# \n\
# ${{env.CHANGELOG}}\""
- name: Push new version
run: git push --atomic origin ${{github.ref_name}} $(git tag --points-at HEAD)
- name: Publish to npm
if: inputs.dry_run == false
run: echo "npm publish"
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}