|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# `npm version 1.2.3` does the following: |
| 4 | +# 1. Update `/package.json` and `/package-lock.json` with the new version |
| 5 | +# 2. `git add package.json package-lock.json` |
| 6 | +# 3. `git commit -m 1.2.3` |
| 7 | +# 4. `git --no-replace-objects tag -m 1.2.3 v1.2.3` |
| 8 | + |
| 9 | +# `npm --workspaces version 1.2.3` changes step 1 to update the version of each workspace but NOT the root. |
| 10 | +# The other steps are executed as-is: step 2 will say there's nothing to commit and the process will stop. |
| 11 | +# The version numbers in `/packages/*/package.json` will be updated but not committed. |
| 12 | + |
| 13 | +# `npm --workspaces --include-workspace-root version 1.2.3` changes step 1 to update the version of each workspace AND |
| 14 | +# the root. However, the remaining steps are still unaltered. You'll end up with a commit that only updates the |
| 15 | +# workspace root, and the version numbers in `/packages/*/package.json` will be updated but not committed. |
| 16 | + |
| 17 | +# Fortunately, npm runs the `version` script as part of this process. Unfortunately, the script runs after |
| 18 | +# `/package.json` and `/package-lock.json` are updated, but before `/packages/*/package.json` are updated. That |
| 19 | +# means the `--workspaces` flag isn't helping, and we need to update workspace versions ourselves. We can do that with |
| 20 | +# `npm --workspaces version --no-git-tag-version ${npm_package_version}` and then stage the files. |
| 21 | + |
| 22 | +# (Props to this post: <https://blog.chlod.net/technical/easy-npm-version-synchronization-for-monorepos/>) |
| 23 | + |
| 24 | +# BUT WAIT! |
| 25 | + |
| 26 | +# What about `--no-git-tag-version`? The normal behavior for `npm version` in that case is to adjust version numbers |
| 27 | +# but not stage or commit anything. If this script stages `/packages/*/package.json` in that case, that's no good. |
| 28 | + |
| 29 | +# The only way I've found to detect `--no-git-tag-version` is to check for the existence of an environment variable |
| 30 | +# called `npm_config_git_tag_version`. Unfortunately, it's considered "true" when the value is an empty string, so we |
| 31 | +# need to check for that as well. Big thanks to this StackOverflow answer: <https://serverfault.com/a/382740> |
| 32 | +# TL;DR: `[ -z "${npm_config_git_tag_version+set}" ]` is test we want. |
| 33 | + |
| 34 | +# So, the plan is: |
| 35 | +# 1. The user should run `npm version 1.2.3` |
| 36 | +# 2. npm will update `/package.json` and `/package-lock.json` for the workspace root |
| 37 | +# 3. npm will run this script (it should be set as the `version` script in `/package.json`) |
| 38 | +# 4. This script will stage `/packages/*/package.json` if `npm_config_git_tag_version` exists |
| 39 | +# 5. npm will choose whether to commit / tag afterward on its own |
| 40 | + |
| 41 | +me="$(basename "$0")" |
| 42 | + |
| 43 | +if [ -z "${npm_package_version}" ]; then |
| 44 | + echo "This script should be run by npm as part of the versioning process." |
| 45 | + echo "To test it, run: npm version --no-git-tag-version 1.2.3" |
| 46 | + echo "If you really must run it outside of npm, try: npm_package_version=1.2.3 npm_config_git_tag_version='' ${me}" |
| 47 | + echo "Be aware that running it directly will not update the version in /package.json" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | +update_dependency_in_workspace () { |
| 52 | + workspace="$1" |
| 53 | + dependency="$2" |
| 54 | + |
| 55 | + jq_filter=" |
| 56 | + if .dependencies.\"$dependency\" then |
| 57 | + .dependencies.\"$dependency\" = \"$npm_package_version\" |
| 58 | + else |
| 59 | + . |
| 60 | + end | |
| 61 | + if .devDependencies.\"$dependency\" then |
| 62 | + .devDependencies.\"$dependency\" = \"$npm_package_version\" |
| 63 | + else |
| 64 | + . |
| 65 | + end | |
| 66 | + if .optionalDependencies.\"$dependency\" then |
| 67 | + .optionalDependencies.\"$dependency\" = \"$npm_package_version\" |
| 68 | + else |
| 69 | + . |
| 70 | + end | |
| 71 | + if .peerDependencies.\"$dependency\" then |
| 72 | + .peerDependencies.\"$dependency\" = \"$npm_package_version\" |
| 73 | + else |
| 74 | + . |
| 75 | + end |
| 76 | + " |
| 77 | + |
| 78 | + jq "$jq_filter" "$workspace/package.json" > "$workspace/package.json.tmp" |
| 79 | + mv "$workspace/package.json.tmp" "$workspace/package.json" |
| 80 | +} |
| 81 | + |
| 82 | +echo "${me}: Setting workspace versions..." >&2 |
| 83 | +npm --workspaces version --no-git-tag-version "${npm_package_version}" |
| 84 | + |
| 85 | +echo "${me}: Reading workspaces..." >&2 |
| 86 | +readarray -t workspace_locations < <( npm query .workspace | jq -r '.[].location' ) |
| 87 | +readarray -t workspace_names < <( npm query .workspace | jq -r '.[].name' ) |
| 88 | + |
| 89 | +echo "${me}: Updating internal dependency versions..." >&2 |
| 90 | +for workspace in "." "${workspace_locations[@]}"; do |
| 91 | + for dependency in "${workspace_names[@]}"; do |
| 92 | + update_dependency_in_workspace "$workspace" "$dependency" |
| 93 | + done |
| 94 | +done |
| 95 | + |
| 96 | +echo "${me}: Asking npm to clean up the lock file..." >&2 |
| 97 | +npm install --offline --no-audit --no-fund --ignore-scripts --package-lock-only |
| 98 | +# Sometimes it makes further changes the second time |
| 99 | +npm install --offline --no-audit --no-fund --ignore-scripts --package-lock-only |
| 100 | + |
| 101 | +if [ -z "${npm_config_git_tag_version+set}" ]; then |
| 102 | + echo "${me}: Staging workspace package.json files..." >&2 |
| 103 | + git add package.json package-lock.json |
| 104 | + for workspace in "${workspace_locations[@]}"; do |
| 105 | + git add "$workspace/package.json" |
| 106 | + done |
| 107 | +fi |
| 108 | + |
| 109 | +echo "${me}: Done!" >&2 |
0 commit comments