diff --git a/.github/workflows/actions/publish-npm-package/action.yml b/.github/workflows/actions/publish-npm-package/action.yml index c4812e77105..c850cb7a642 100644 --- a/.github/workflows/actions/publish-npm-package/action.yml +++ b/.github/workflows/actions/publish-npm-package/action.yml @@ -5,11 +5,8 @@ inputs: dry_run: description: Dry run will not publish to npm, just test it. required: true - package: - description: npm package name. - required: true - scope: - description: npm package scope (must not include '@' prefix). + package_dir: + description: directory containing the package.json file. required: true tag: description: npm package tag. @@ -26,40 +23,53 @@ inputs: runs: using: "composite" steps: + - uses: actions/setup-node@v6 + with: + node-version: "lts/*" + package-manager-cache: "false" + # Note: setting up the registry allows auth to be passed via env.NODE_AUTH_TOKEN + registry-url: https://registry.npmjs.org + + - name: Prepare + id: prepare + shell: bash + run: | + echo "package_name=$(jq -r ".name" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT + echo "package_local_version=$(jq -r ".version" < "${{ inputs.package_dir }}"/package.json)" >> $GITHUB_OUTPUT + - name: Check npm latest version id: check_version shell: bash run: | - echo "Check crate latest published version for '${{ inputs.package }}' package" + echo "Check npmjs.com latest published version for '${{ steps.prepare.outputs.package_name }}' package" if [ "${{ inputs.tag }}" != "latest" -a "${{ inputs.tag }}" != "next" ]; then echo "Tag '${{ inputs.tag }}' is not valid. It should be one of 'latest' or 'next'" exit 1 fi - LOCAL_VERSION=$(cat ${{ inputs.package }}/package.json | jq -r '.version') - NEXT_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.next 2> /dev/null || true) - LATEST_REMOTE_VERSION=$(npm view @${{ inputs.scope }}/${{ inputs.package }} dist-tags.latest 2> /dev/null || true) + NEXT_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.next 2> /dev/null || true) + LATEST_REMOTE_VERSION=$(npm view ${{ steps.prepare.outputs.package_name }} dist-tags.latest 2> /dev/null || true) - echo "Latest crate.io version: '$LATEST_REMOTE_VERSION'" - echo "Next crate.io version: '$NEXT_REMOTE_VERSION'" - echo "Local version: '$LOCAL_VERSION'" + echo "Latest npmjs.com version: '$LATEST_REMOTE_VERSION'" + echo "Next npmjs.com version: '$NEXT_REMOTE_VERSION'" + echo "Local version: '${{ steps.prepare.outputs.package_local_version }}'" if [ "${{ inputs.tag }}" == "latest" ]; then - if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then + if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then echo "Local version and remote version are the same: no need to publish to npm registry" DEPLOY_MODE='none' - elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then + elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then DEPLOY_MODE='promote' else DEPLOY_MODE='publish' fi else # input.tag == 'next' - if [ "$LOCAL_VERSION" == "$LATEST_REMOTE_VERSION" ]; then + if [ "${{ steps.prepare.outputs.package_local_version }}" == "$LATEST_REMOTE_VERSION" ]; then # A latest already published: no need to tag with next echo "Local version and remote version are the same: no need to publish to npm registry" DEPLOY_MODE='none' - elif [ "$LOCAL_VERSION" == "$NEXT_REMOTE_VERSION" ]; then + elif [ "${{ steps.prepare.outputs.package_local_version }}" == "$NEXT_REMOTE_VERSION" ]; then echo "Local version and remote version are the same: no need to publish to npm registry" DEPLOY_MODE='none' else @@ -70,43 +80,43 @@ runs: echo "Deploy mode: '$DEPLOY_MODE'" echo "Dry run: '${{ inputs.dry_run }}'" echo "deploy_mode=$DEPLOY_MODE" >> $GITHUB_OUTPUT - echo "package_version=$LOCAL_VERSION" >> $GITHUB_OUTPUT - name: Build package shell: bash - working-directory: ${{ inputs.package }} + working-directory: ${{ inputs.package_dir }} env: - WASM_PACK_ARGS: --release --scope ${{ inputs.scope }} + WASM_PACK_ARGS: --release run: | - echo "Build '@${{ inputs.scope }}/${{ inputs.package }}' package" + echo "::group::Build '${{ steps.prepare.outputs.package_name }}' package" make build + echo "::endgroup::" - name: Prepare publish shell: bash run: | - cp ./LICENSE ${{ inputs.package }} - cp -f ${{ inputs.package }}/npm/README.md ${{ inputs.package }}/ + cp ./LICENSE ${{ inputs.package_dir }} + cp -f ${{ inputs.package_dir }}/npm/README.md ${{ inputs.package_dir }}/ - name: List package shell: bash run: | - echo "List '@${{ inputs.scope }}/${{ inputs.package }}' package" - ls -al -R ${{ inputs.package }}/dist + echo "List '${{ steps.prepare.outputs.package_name }}' package" + ls -al -R ${{ inputs.package_dir }}/dist - name: Publish package new version if: steps.check_version.outputs.deploy_mode == 'publish' shell: bash + working-directory: ${{ inputs.package_dir }} env: - NPM_TOKEN: ${{ inputs.api_token }} + NODE_AUTH_TOKEN: ${{ inputs.api_token }} run: | - echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package" - npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" - if [ -z "${NPM_TOKEN}" -a "${{ inputs.dry_run }}" == "true" ]; then + echo "Publish '${{ steps.prepare.outputs.package_name }}' package" + if [ -z "${NODE_AUTH_TOKEN}" -a "${{ inputs.dry_run }}" == "true" ]; then echo "Warning: An NPM access token is required for authentication and has not been provided." else npm whoami fi - cd ${{ inputs.package }}/ + if [ "${{ inputs.dry_run }}" == "false" ]; then dry_run_option="" else @@ -115,14 +125,18 @@ runs: npm publish --tag ${{ inputs.tag }} --access ${{ inputs.access }} $dry_run_option - name: Promote package distribution tag to 'latest' - if: inputs.dry_run == 'false' && steps.check_version.outputs.deploy_mode == 'promote' + if: steps.check_version.outputs.deploy_mode == 'promote' shell: bash + working-directory: ${{ inputs.package_dir }} env: - NPM_TOKEN: ${{ inputs.api_token }} + NODE_AUTH_TOKEN: ${{ inputs.api_token }} run: | - echo "Publish '@${{ inputs.scope }}/${{ inputs.package }}' package" - npm set "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" - npm whoami - cd ${{ inputs.package }}/ - npm dist-tag add @${{ inputs.scope }}/${{ inputs.package }}@${{ steps.check_version.outputs.package_version }} latest - npm dist-tag rm @${{ inputs.scope }}/${{ inputs.package }}@${{ steps.check_version.outputs.package_version }} next + echo "Promote '${{ steps.prepare.outputs.package_name }}' package version '${{ steps.prepare.outputs.package_local_version }}' from 'next' to 'latest'" + if [ "${{ inputs.dry_run }}" == "false" ]; then + npm whoami + npm dist-tag add ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} latest + npm dist-tag rm ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} next + else + echo "Dry run: npm dist-tag add ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} latest + echo "Dry run: npm dist-tag rm ${{ steps.prepare.outputs.package_name }}"@${{ steps.prepare.outputs.package_local_version }} next + fi diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5bbe3010a74..25da5b2924e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -136,7 +136,7 @@ jobs: shell: bash working-directory: mithril-client-wasm env: - WASM_PACK_ARGS: --release --scope mithril-dev + WASM_PACK_ARGS: --release run: make build - name: Prepare 'mithril-client-wasm' package @@ -594,7 +594,6 @@ jobs: package: [mithril-client-wasm] include: - package: mithril-client-wasm - scope: mithril-dev tag: latest access: public api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM @@ -613,12 +612,11 @@ jobs: cargo-tools: wasm-pack github-token: ${{ secrets.GITHUB_TOKEN }} - - name: Publish package to npm + - name: Publish package to npm (dry-run) uses: ./.github/workflows/actions/publish-npm-package with: dry_run: "true" - package: ${{ matrix.package }} - scope: ${{ matrix.scope }} + package_dir: ${{ matrix.package }} access: ${{ matrix.access }} api_token: ${{ secrets[matrix.api_token_secret_name] }} diff --git a/.github/workflows/manual-publish-npm.yml b/.github/workflows/manual-publish-npm.yml index bcd22ba932d..122fbf803bc 100644 --- a/.github/workflows/manual-publish-npm.yml +++ b/.github/workflows/manual-publish-npm.yml @@ -39,7 +39,6 @@ jobs: package: [mithril-client-wasm] include: - package: mithril-client-wasm - scope: mithril-dev tag: latest access: public api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM @@ -65,8 +64,7 @@ jobs: uses: ./.github/workflows/actions/publish-npm-package with: dry_run: ${{ inputs.dry_run }} - package: ${{ matrix.package }} - scope: ${{ matrix.scope }} + package_dir: ${{ matrix.package }} tag: ${{ matrix.tag }} access: ${{ matrix.access }} api_token: ${{ secrets[matrix.api_token_secret_name] }} diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 8316e32b52c..a21a0c49f12 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -316,7 +316,7 @@ jobs: dry_run: "true" package: ${{ matrix.package }} - publish-wasm-test: + publish-next-wasm-package: strategy: fail-fast: false max-parallel: 1 @@ -324,7 +324,6 @@ jobs: package: [mithril-client-wasm] include: - package: mithril-client-wasm - scope: mithril-dev tag: next access: public api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM @@ -345,8 +344,7 @@ jobs: uses: ./.github/workflows/actions/publish-npm-package with: dry_run: "false" - package: ${{ matrix.package }} - scope: ${{ matrix.scope }} + package_dir: ${{ matrix.package }} tag: next access: ${{ matrix.access }} api_token: ${{ secrets[matrix.api_token_secret_name] }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e3f3e3c5550..24b2ee8937b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -248,7 +248,7 @@ jobs: package: ${{ matrix.package }} api_token: ${{ secrets[matrix.api_token_secret_name] }} - publish-wasm: + promote-wasm-package-to-latest: strategy: fail-fast: false max-parallel: 1 @@ -256,7 +256,6 @@ jobs: package: [mithril-client-wasm] include: - package: mithril-client-wasm - scope: mithril-dev tag: latest access: public api_token_secret_name: NPM_API_TOKEN_MITHRIL_CLIENT_WASM @@ -273,12 +272,11 @@ jobs: cargo-tools: wasm-pack github-token: ${{ secrets.GITHUB_TOKEN }} - - name: Publish package to npm + - name: Promote npm package uses: ./.github/workflows/actions/publish-npm-package with: dry_run: "false" - package: ${{ matrix.package }} - scope: ${{ matrix.scope }} + package_dir: ${{ matrix.package }} tag: latest access: ${{ matrix.access }} api_token: ${{ secrets[matrix.api_token_secret_name] }} diff --git a/mithril-client-wasm/package.json b/mithril-client-wasm/package.json index abbb43c9c01..a6ff644580a 100644 --- a/mithril-client-wasm/package.json +++ b/mithril-client-wasm/package.json @@ -25,11 +25,11 @@ "types": "dist/node/mithril_client_wasm.d.ts", "scripts": { "build": "npm run build:node && npm run build:web && npm run build:bundler", - "build:bundler": "wasm-pack build --target bundler --out-dir dist/bundler $WASM_PACK_ARGS", + "build:bundler": "wasm-pack build --target bundler --out-dir dist/bundler --scope mithril-dev $WASM_PACK_ARGS", "postbuild:bundler": "rm dist/bundler/.gitignore", - "build:node": "wasm-pack build --target nodejs --out-dir dist/node $WASM_PACK_ARGS", + "build:node": "wasm-pack build --target nodejs --out-dir dist/node --scope mithril-dev $WASM_PACK_ARGS", "postbuild:node": "rm dist/node/.gitignore", - "build:web": "wasm-pack build --target web --out-dir dist/web $WASM_PACK_ARGS", + "build:web": "wasm-pack build --target web --out-dir dist/web --scope mithril-dev $WASM_PACK_ARGS", "postbuild:web": "rm dist/web/.gitignore" } }