From 83c86cd69a303fdbf68b97ccce97ff8ce3778d3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Mon, 8 Jul 2024 12:19:25 +0200 Subject: [PATCH] Use NPM for publishing packages instead of Lerna (#1581) ## Motivation for the change, related issues Every few months there's a problem with publishing packages via Lerna, e.g. here's the latest one: https://github.com/WordPress/wordpress-playground/issues/1572 Debugging those issues takes a lot of time and typically there are no useful resources out there. This PR limits the use of Lerna to bumping the packages version in the repo and tagging the updated code. ## Implementation details Instead of using `lerna publish`, we run a simple, custom bash script. ## Testing Instructions (or ideally a Blueprint) Merge and run. It seems to be working locally and I don't see any other way of testing it in GitHub. --- .github/workflows/publish-npm-packages.yml | 2 +- package.json | 6 ++- .../site/docs/11-architecture/01-index.md | 28 +++++++++-- packages/meta/bin/publish-packages.sh | 49 +++++++++++++++++++ packages/playground/website/package.json | 1 + 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 packages/meta/bin/publish-packages.sh diff --git a/.github/workflows/publish-npm-packages.yml b/.github/workflows/publish-npm-packages.yml index 838061b7ec..2492f2588e 100644 --- a/.github/workflows/publish-npm-packages.yml +++ b/.github/workflows/publish-npm-packages.yml @@ -52,4 +52,4 @@ jobs: # Version bump, release, tag a new version on GitHub - name: Release new version of NPM packages shell: bash - run: npx lerna@6.6.2 publish ${{ inputs.version_bump }} --yes --no-private --loglevel=verbose --dist-tag=${{ inputs.dist_tag }} + run: npm run release:${{ inputs.version_bump }} -- --dist-tag=${{ inputs.dist_tag }} diff --git a/package.json b/package.json index 959f2f7a82..707e02f71c 100644 --- a/package.json +++ b/package.json @@ -13,9 +13,9 @@ "format": "nx format", "format:uncommitted": "nx format --fix --parallel --uncommitted", "lint": "nx run-many --all --target=lint", - "prepublishOnly": "npm run build", "mindmap": "cd packages/meta/src/mindmap/v2 && php -S 127.0.0.1:5269", "preview": "nx preview playground-website", + "publish-to-npm": "bash ./packages/meta/bin/publish-packages.sh", "recompile:php:web": "nx recompile-php:light:all php-wasm-web && nx recompile-php:kitchen-sink:all php-wasm-web ", "recompile:php:web:light": "nx recompile-php:light:all php-wasm-web ", "recompile:php:web:light:7.0": "nx recompile-php:light php-wasm-web --PHP_VERSION=7.0", @@ -47,7 +47,9 @@ "recompile:php:node:8.1": "nx recompile-php php-wasm-node --PHP_VERSION=8.1", "recompile:php:node:8.2": "nx recompile-php php-wasm-node --PHP_VERSION=8.2", "recompile:php:node:8.3": "nx recompile-php php-wasm-node --PHP_VERSION=8.3", - "release": "lerna publish patch --yes --no-private --loglevel=verbose", + "release:patch": "npx lerna version patch --no-private --force-publish --yes && npm run build && npm run publish-to-npm", + "release:minor": "npx lerna version minor --no-private --force-publish --yes && npm run build && npm run publish-to-npm", + "release:major": "npx lerna version major --no-private --force-publish --yes && npm run build && npm run publish-to-npm", "test": "node --expose-gc node_modules/nx/bin/nx run-many --all --target=test", "fix-asyncify": "node packages/php-wasm/node/bin/rebuild-while-asyncify-functions-missing.mjs", "typecheck": "nx run-many --all --target=typecheck", diff --git a/packages/docs/site/docs/11-architecture/01-index.md b/packages/docs/site/docs/11-architecture/01-index.md index f64e70afbc..9393e2e722 100644 --- a/packages/docs/site/docs/11-architecture/01-index.md +++ b/packages/docs/site/docs/11-architecture/01-index.md @@ -21,10 +21,32 @@ The dependencies between Playground packages and projects [are too complex](http To learn more, head over to the [NX developer docs](https://nx.dev/getting-started/intro). -### Lerna: publishing packages and projects +### Publishing packages and projects WordPress Playground includes several NPM packages, a VS Code extension, WordPress plugins, a web app, and other GitHub releases, all managed across two monorepos: the main [wordpress-playground](https://github.com/WordPress/wordpress-playground) and [Playground Tools](https://github.com/WordPress/playground-tools/). -We use [Lerna](https://lerna.js.org) to build, manage, and publish all JavaScript/TypeScript packages. Lerna handles everything simultaneously: it increments the version number, sets a new tag, and publishes the modified packages to `npm`. +We use a few tools for publishing these packages: -The published packages share the same version number, so when updating a single package, Lerna bumps the version number of all dependent packages. +- [Lerna](https://lerna.js.org) to increment packages version numbers and set git tags. +- npm to publish the packages to NPM. + +The published packages share the same version number, so when updating a single package, Lerna bumps the version number of all dependent packages. We do not use Lerna for publishing because every now and then it starts crashing with cryptic error messages and does not provide much information on what went wrong. + +Here are the commands you'll need to publish the packages: + +```bash +## Increment the version number of the packages +npx lerna version --no-private --force-publish --yes + +## Build all the packages +npm run build + +## Publish the built packages to NPM +npm run publish-to-npm +``` + +Or you can do it all in one go: + +```bash +npm run release:patch # or minor or major +``` diff --git a/packages/meta/bin/publish-packages.sh b/packages/meta/bin/publish-packages.sh new file mode 100644 index 0000000000..c4e112262a --- /dev/null +++ b/packages/meta/bin/publish-packages.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Default value for dist-tag +DIST_TAG="" + +# Parse CLI arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --dist-tag=*) DIST_TAG="${1#*=}"; shift ;; + *) echo "Unknown parameter passed: $1"; exit 1 ;; + esac +done + +# Function to check if the package.json is for a public NPM package and publish it +publish_if_public_package() { + if [ -f "$1/package.json" ]; then + # Use Node.js to check the 'private' field in package.json + is_private=$(node -e " + const fs = require('fs'); + const path = '$1/package.json'; + const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); + console.log(pkg.private === true ? 'true' : 'false'); + ") + + # If "private" is false, publish the package + if [ "$is_private" = "false" ]; then + echo "Publishing package in $1" + if [ -n "$DIST_TAG" ]; then + npm publish "$1" --tag "$DIST_TAG" + else + npm publish "$1" + fi + else + echo "$1 is marked as private. Skipping publish." + fi + else + echo "No package.json found in $1. Skipping." + fi +} + +# Iterate over all directories in dist/packages/php-wasm/* +for dir in dist/packages/php-wasm/*/; do + publish_if_public_package "$dir" +done + +# Iterate over all directories in dist/packages/playground/* +for dir in dist/packages/playground/*/; do + publish_if_public_package "$dir" +done diff --git a/packages/playground/website/package.json b/packages/playground/website/package.json index a045577b51..3cab5a22ed 100644 --- a/packages/playground/website/package.json +++ b/packages/playground/website/package.json @@ -1,4 +1,5 @@ { + "name": "@wp-playground/website", "version": "0.0.1", "type": "module", "private": true