|
38 | 38 | # 4. This script will stage `/packages/*/package.json` if `npm_config_git_tag_version` exists
|
39 | 39 | # 5. npm will choose whether to commit / tag afterward on its own
|
40 | 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_workspace_dependency_kind () { |
| 52 | + kind_arg="$1" |
| 53 | + kind_property="$2" |
| 54 | + |
| 55 | + workspace_args=() |
| 56 | + if [ "$3" != "." ]; then |
| 57 | + workspace_args+=("--workspace" "$3") |
| 58 | + fi |
| 59 | + |
| 60 | + dependencies=() |
| 61 | + for dependency in "${workspace_names[@]}"; do |
| 62 | + if jq -e ".${kind_property}.\"$dependency\"" "$workspace/package.json" > /dev/null; then |
| 63 | + dependencies+=("${dependency}@${npm_package_version}") |
| 64 | + fi |
| 65 | + done |
| 66 | + if [ "${#dependencies[@]}" -gt 0 ]; then |
| 67 | + echo "Updating $kind_property in $workspace: ${dependencies[*]}" >&2 |
| 68 | + set -x |
| 69 | + npm "${workspace_args[@]}" install --offline --no-audit --no-fund "$kind_arg" --save-exact "${dependencies[@]}" |
| 70 | + { set +x; } 2>/dev/null |
| 71 | + else |
| 72 | + echo "No $kind_property to update in $workspace" >&2 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +update_workspace_dependency_versions () { |
| 77 | + workspace="$1" |
| 78 | + update_workspace_dependency_kind --save-prod dependencies "$workspace" |
| 79 | + update_workspace_dependency_kind --save-dev devDependencies "$workspace" |
| 80 | + update_workspace_dependency_kind --save-optional optionalDependencies "$workspace" |
| 81 | + update_workspace_dependency_kind --save-peer peerDependencies "$workspace" |
| 82 | + |
| 83 | + # Do it all twice: sometimes npm doesn't actually update the dependency versions the first time |
| 84 | + update_workspace_dependency_kind --save-prod dependencies "$workspace" |
| 85 | + update_workspace_dependency_kind --save-dev devDependencies "$workspace" |
| 86 | + update_workspace_dependency_kind --save-optional optionalDependencies "$workspace" |
| 87 | + update_workspace_dependency_kind --save-peer peerDependencies "$workspace" |
| 88 | +} |
| 89 | + |
| 90 | +echo "${me}: Setting workspace versions..." >&2 |
41 | 91 | npm --workspaces version --no-git-tag-version "${npm_package_version}"
|
42 | 92 |
|
| 93 | +echo "${me}: Reading workspaces..." >&2 |
| 94 | +readarray -t workspace_locations < <( npm query .workspace | jq -r '.[].location' ) |
| 95 | +readarray -t workspace_names < <( npm query .workspace | jq -r '.[].name' ) |
| 96 | + |
| 97 | +echo "${me}: Updating internal dependency versions..." >&2 |
| 98 | +update_workspace_dependency_versions "." # workspace root |
| 99 | +for workspace in "${workspace_locations[@]}"; do |
| 100 | + update_workspace_dependency_versions "$workspace" |
| 101 | +done |
| 102 | + |
43 | 103 | if [ -z "${npm_config_git_tag_version+set}" ]; then
|
44 |
| - npm query .workspace | jq -r '.[].location' | while read WS; do |
45 |
| - git add "$WS/package.json" |
| 104 | + echo "${me}: Staging workspace package.json files..." >&2 |
| 105 | + git add package.json package-lock.json |
| 106 | + for workspace in "${workspace_locations[@]}"; do |
| 107 | + git add "$workspace/package.json" |
46 | 108 | done
|
47 | 109 | fi
|
| 110 | + |
| 111 | +echo "${me}: Done!" >&2 |
0 commit comments