Skip to content

Commit

Permalink
Use NPM for publishing packages instead of Lerna (#1581)
Browse files Browse the repository at this point in the history
## 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:

#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.
  • Loading branch information
adamziel authored Jul 8, 2024
1 parent 2b3336b commit 83c86cd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-npm-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] 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 }}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 25 additions & 3 deletions packages/docs/site/docs/11-architecture/01-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <major|minor|patch> --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
```
49 changes: 49 additions & 0 deletions packages/meta/bin/publish-packages.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/playground/website/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "@wp-playground/website",
"version": "0.0.1",
"type": "module",
"private": true
Expand Down

0 comments on commit 83c86cd

Please sign in to comment.