Skip to content

Commit 55288a9

Browse files
committed
build: don't confuse npm install with unpublished version numbers
Even with `--offline` and installing a dependency that's contained in an internal worksace, `npm --workspace a install b` will put the latest published version of `b` (that it knows about) into the lock file rather than the current monorepo version of `b`. The only way I've found to get the version updated correctly in both `package.json` and `package-lock.json` is the way that @MiroslavDionisiev did it in the previous versioning script: just manually hammer the version number, then get `npm i --package-lock-only` to make the lock file consistent afterward.
1 parent 29345ed commit 55288a9

File tree

3 files changed

+38
-85
lines changed

3 files changed

+38
-85
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
GIT_TAG="${{github.event.release.tag_name}}"
3030
NEW_VERSION="${GIT_TAG/v/}"
3131
32-
bash ./scripts/update-dependencies-with-tag-versions.sh "$NEW_VERSION"
32+
npm version "$NEW_VERSION" --no-git-tag-version
3333
git add package* && git commit -m "Release $NEW_VERSION"
3434
3535
- uses: ./.github/actions/install-dependencies

scripts/npm-version.sh

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,35 @@ if [ -z "${npm_package_version}" ]; then
4848
exit 1
4949
fi
5050

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 () {
51+
update_dependency_in_workspace () {
7752
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"
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"
8880
}
8981

9082
echo "${me}: Setting workspace versions..." >&2
@@ -95,11 +87,17 @@ readarray -t workspace_locations < <( npm query .workspace | jq -r '.[].location
9587
readarray -t workspace_names < <( npm query .workspace | jq -r '.[].name' )
9688

9789
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"
90+
for workspace in "." "${workspace_locations[@]}"; do
91+
for dependency in "${workspace_names[@]}"; do
92+
update_dependency_in_workspace "$workspace" "$dependency"
93+
done
10194
done
10295

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+
103101
if [ -z "${npm_config_git_tag_version+set}" ]; then
104102
echo "${me}: Staging workspace package.json files..." >&2
105103
git add package.json package-lock.json

scripts/update-dependencies-with-tag-versions.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)