Skip to content

Commit 37e9e0e

Browse files
committed
fix: use npx semver to better determine latest npm
1 parent ec65582 commit 37e9e0e

File tree

10 files changed

+1115
-1626
lines changed

10 files changed

+1115
-1626
lines changed

Diff for: .github/workflows/audit.yml

+22-37
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,33 @@ jobs:
3030
node-version: 20.x
3131
check-latest: contains('20.x', '.x')
3232

33-
# node 10/12/14 ship with npm@6, which is known to fail when updating itself in windows
34-
- name: Update Windows npm
35-
if: |
36-
matrix.platform.os == 'windows-latest' && (
37-
startsWith(steps.node.outputs.node-version, 'v10.') || startsWith(steps.node.outputs.node-version, 'v12.') || startsWith(steps.node.outputs.node-version, 'v14.')
38-
)
39-
run: |
40-
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
41-
tar xf npm-7.5.4.tgz
42-
cd package
43-
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
44-
cd ..
45-
rmdir /s /q package
46-
47-
# Start on Node 10 because we dont test on anything lower
48-
- name: Install npm@7 on Node 10
33+
- name: Install Latest npm
4934
shell: bash
50-
if: startsWith(steps.node.outputs.node-version, 'v10.')
51-
id: npm-7
35+
env:
36+
NODE_VERSION: ${{ steps.node.outputs.node-version }}
5237
run: |
53-
npm i --prefer-online --no-fund --no-audit -g npm@7
54-
echo "updated=true" >> "$GITHUB_OUTPUT"
38+
MATCH=""
39+
SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")
5540
56-
- name: Install npm@8 on Node 12
57-
shell: bash
58-
if: startsWith(steps.node.outputs.node-version, 'v12.')
59-
id: npm-8
60-
run: |
61-
npm i --prefer-online --no-fund --no-audit -g npm@8
62-
echo "updated=true" >> "$GITHUB_OUTPUT"
41+
echo "node@$NODE_VERSION"
6342
64-
- name: Install npm@9 on Node 14/16/18.0
65-
shell: bash
66-
if: startsWith(steps.node.outputs.node-version, 'v14.') || startsWith(steps.node.outputs.node-version, 'v16.') || startsWith(steps.node.outputs.node-version, 'v18.0.')
67-
id: npm-9
68-
run: |
69-
npm i --prefer-online --no-fund --no-audit -g npm@9
70-
echo "updated=true" >> "$GITHUB_OUTPUT"
43+
for SPEC in ${SPECS[@]}; do
44+
ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
45+
echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"
46+
47+
if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
48+
MATCH=$SPEC
49+
echo "Found compatible version: npm@$MATCH"
50+
break
51+
fi
52+
done
53+
54+
if [ -z $MATCH ]; then
55+
echo "Could not find a compatible version of npm for node@$NODE_VERSION"
56+
exit 1
57+
fi
7158
72-
- name: Install npm@latest on Node
73-
if: ${{ !(steps.npm-7.outputs.updated || steps.npm-8.outputs.updated || steps.npm-9.outputs.updated) }}
74-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
59+
npm i --prefer-online --no-fund --no-audit -g npm@$MATCH
7560
7661
- name: npm Version
7762
run: npm -v

Diff for: .github/workflows/ci-release.yml

+47-61
Original file line numberDiff line numberDiff line change
@@ -85,48 +85,33 @@ jobs:
8585
node-version: 20.x
8686
check-latest: contains('20.x', '.x')
8787

88-
# node 10/12/14 ship with npm@6, which is known to fail when updating itself in windows
89-
- name: Update Windows npm
90-
if: |
91-
matrix.platform.os == 'windows-latest' && (
92-
startsWith(steps.node.outputs.node-version, 'v10.') || startsWith(steps.node.outputs.node-version, 'v12.') || startsWith(steps.node.outputs.node-version, 'v14.')
93-
)
94-
run: |
95-
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
96-
tar xf npm-7.5.4.tgz
97-
cd package
98-
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
99-
cd ..
100-
rmdir /s /q package
101-
102-
# Start on Node 10 because we dont test on anything lower
103-
- name: Install npm@7 on Node 10
88+
- name: Install Latest npm
10489
shell: bash
105-
if: startsWith(steps.node.outputs.node-version, 'v10.')
106-
id: npm-7
90+
env:
91+
NODE_VERSION: ${{ steps.node.outputs.node-version }}
10792
run: |
108-
npm i --prefer-online --no-fund --no-audit -g npm@7
109-
echo "updated=true" >> "$GITHUB_OUTPUT"
93+
MATCH=""
94+
SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")
11095
111-
- name: Install npm@8 on Node 12
112-
shell: bash
113-
if: startsWith(steps.node.outputs.node-version, 'v12.')
114-
id: npm-8
115-
run: |
116-
npm i --prefer-online --no-fund --no-audit -g npm@8
117-
echo "updated=true" >> "$GITHUB_OUTPUT"
96+
echo "node@$NODE_VERSION"
11897
119-
- name: Install npm@9 on Node 14/16/18.0
120-
shell: bash
121-
if: startsWith(steps.node.outputs.node-version, 'v14.') || startsWith(steps.node.outputs.node-version, 'v16.') || startsWith(steps.node.outputs.node-version, 'v18.0.')
122-
id: npm-9
123-
run: |
124-
npm i --prefer-online --no-fund --no-audit -g npm@9
125-
echo "updated=true" >> "$GITHUB_OUTPUT"
98+
for SPEC in ${SPECS[@]}; do
99+
ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
100+
echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"
126101
127-
- name: Install npm@latest on Node
128-
if: ${{ !(steps.npm-7.outputs.updated || steps.npm-8.outputs.updated || steps.npm-9.outputs.updated) }}
129-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
102+
if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
103+
MATCH=$SPEC
104+
echo "Found compatible version: npm@$MATCH"
105+
break
106+
fi
107+
done
108+
109+
if [ -z $MATCH ]; then
110+
echo "Could not find a compatible version of npm for node@$NODE_VERSION"
111+
exit 1
112+
fi
113+
114+
npm i --prefer-online --no-fund --no-audit -g npm@$MATCH
130115
131116
- name: npm Version
132117
run: npm -v
@@ -232,7 +217,9 @@ jobs:
232217
- name: Update Windows npm
233218
if: |
234219
matrix.platform.os == 'windows-latest' && (
235-
startsWith(steps.node.outputs.node-version, 'v10.') || startsWith(steps.node.outputs.node-version, 'v12.') || startsWith(steps.node.outputs.node-version, 'v14.')
220+
startsWith(steps.node.outputs.node-version, 'v10.') ||
221+
startsWith(steps.node.outputs.node-version, 'v12.') ||
222+
startsWith(steps.node.outputs.node-version, 'v14.')
236223
)
237224
run: |
238225
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
@@ -242,34 +229,33 @@ jobs:
242229
cd ..
243230
rmdir /s /q package
244231
245-
# Start on Node 10 because we dont test on anything lower
246-
- name: Install npm@7 on Node 10
232+
- name: Install Latest npm
247233
shell: bash
248-
if: startsWith(steps.node.outputs.node-version, 'v10.')
249-
id: npm-7
234+
env:
235+
NODE_VERSION: ${{ steps.node.outputs.node-version }}
250236
run: |
251-
npm i --prefer-online --no-fund --no-audit -g npm@7
252-
echo "updated=true" >> "$GITHUB_OUTPUT"
237+
MATCH=""
238+
SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")
253239
254-
- name: Install npm@8 on Node 12
255-
shell: bash
256-
if: startsWith(steps.node.outputs.node-version, 'v12.')
257-
id: npm-8
258-
run: |
259-
npm i --prefer-online --no-fund --no-audit -g npm@8
260-
echo "updated=true" >> "$GITHUB_OUTPUT"
240+
echo "node@$NODE_VERSION"
261241
262-
- name: Install npm@9 on Node 14/16/18.0
263-
shell: bash
264-
if: startsWith(steps.node.outputs.node-version, 'v14.') || startsWith(steps.node.outputs.node-version, 'v16.') || startsWith(steps.node.outputs.node-version, 'v18.0.')
265-
id: npm-9
266-
run: |
267-
npm i --prefer-online --no-fund --no-audit -g npm@9
268-
echo "updated=true" >> "$GITHUB_OUTPUT"
242+
for SPEC in ${SPECS[@]}; do
243+
ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
244+
echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"
245+
246+
if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
247+
MATCH=$SPEC
248+
echo "Found compatible version: npm@$MATCH"
249+
break
250+
fi
251+
done
252+
253+
if [ -z $MATCH ]; then
254+
echo "Could not find a compatible version of npm for node@$NODE_VERSION"
255+
exit 1
256+
fi
269257
270-
- name: Install npm@latest on Node
271-
if: ${{ !(steps.npm-7.outputs.updated || steps.npm-8.outputs.updated || steps.npm-9.outputs.updated) }}
272-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
258+
npm i --prefer-online --no-fund --no-audit -g npm@$MATCH
273259
274260
- name: npm Version
275261
run: npm -v

Diff for: .github/workflows/ci-test-workspace.yml

+47-61
Original file line numberDiff line numberDiff line change
@@ -38,48 +38,33 @@ jobs:
3838
node-version: 20.x
3939
check-latest: contains('20.x', '.x')
4040

41-
# node 10/12/14 ship with npm@6, which is known to fail when updating itself in windows
42-
- name: Update Windows npm
43-
if: |
44-
matrix.platform.os == 'windows-latest' && (
45-
startsWith(steps.node.outputs.node-version, 'v10.') || startsWith(steps.node.outputs.node-version, 'v12.') || startsWith(steps.node.outputs.node-version, 'v14.')
46-
)
47-
run: |
48-
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
49-
tar xf npm-7.5.4.tgz
50-
cd package
51-
node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz
52-
cd ..
53-
rmdir /s /q package
54-
55-
# Start on Node 10 because we dont test on anything lower
56-
- name: Install npm@7 on Node 10
41+
- name: Install Latest npm
5742
shell: bash
58-
if: startsWith(steps.node.outputs.node-version, 'v10.')
59-
id: npm-7
43+
env:
44+
NODE_VERSION: ${{ steps.node.outputs.node-version }}
6045
run: |
61-
npm i --prefer-online --no-fund --no-audit -g npm@7
62-
echo "updated=true" >> "$GITHUB_OUTPUT"
46+
MATCH=""
47+
SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")
6348
64-
- name: Install npm@8 on Node 12
65-
shell: bash
66-
if: startsWith(steps.node.outputs.node-version, 'v12.')
67-
id: npm-8
68-
run: |
69-
npm i --prefer-online --no-fund --no-audit -g npm@8
70-
echo "updated=true" >> "$GITHUB_OUTPUT"
49+
echo "node@$NODE_VERSION"
7150
72-
- name: Install npm@9 on Node 14/16/18.0
73-
shell: bash
74-
if: startsWith(steps.node.outputs.node-version, 'v14.') || startsWith(steps.node.outputs.node-version, 'v16.') || startsWith(steps.node.outputs.node-version, 'v18.0.')
75-
id: npm-9
76-
run: |
77-
npm i --prefer-online --no-fund --no-audit -g npm@9
78-
echo "updated=true" >> "$GITHUB_OUTPUT"
51+
for SPEC in ${SPECS[@]}; do
52+
ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
53+
echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"
54+
55+
if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
56+
MATCH=$SPEC
57+
echo "Found compatible version: npm@$MATCH"
58+
break
59+
fi
60+
done
61+
62+
if [ -z $MATCH ]; then
63+
echo "Could not find a compatible version of npm for node@$NODE_VERSION"
64+
exit 1
65+
fi
7966
80-
- name: Install npm@latest on Node
81-
if: ${{ !(steps.npm-7.outputs.updated || steps.npm-8.outputs.updated || steps.npm-9.outputs.updated) }}
82-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
67+
npm i --prefer-online --no-fund --no-audit -g npm@$MATCH
8368
8469
- name: npm Version
8570
run: npm -v
@@ -133,7 +118,9 @@ jobs:
133118
- name: Update Windows npm
134119
if: |
135120
matrix.platform.os == 'windows-latest' && (
136-
startsWith(steps.node.outputs.node-version, 'v10.') || startsWith(steps.node.outputs.node-version, 'v12.') || startsWith(steps.node.outputs.node-version, 'v14.')
121+
startsWith(steps.node.outputs.node-version, 'v10.') ||
122+
startsWith(steps.node.outputs.node-version, 'v12.') ||
123+
startsWith(steps.node.outputs.node-version, 'v14.')
137124
)
138125
run: |
139126
curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz
@@ -143,34 +130,33 @@ jobs:
143130
cd ..
144131
rmdir /s /q package
145132
146-
# Start on Node 10 because we dont test on anything lower
147-
- name: Install npm@7 on Node 10
133+
- name: Install Latest npm
148134
shell: bash
149-
if: startsWith(steps.node.outputs.node-version, 'v10.')
150-
id: npm-7
135+
env:
136+
NODE_VERSION: ${{ steps.node.outputs.node-version }}
151137
run: |
152-
npm i --prefer-online --no-fund --no-audit -g npm@7
153-
echo "updated=true" >> "$GITHUB_OUTPUT"
138+
MATCH=""
139+
SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6")
154140
155-
- name: Install npm@8 on Node 12
156-
shell: bash
157-
if: startsWith(steps.node.outputs.node-version, 'v12.')
158-
id: npm-8
159-
run: |
160-
npm i --prefer-online --no-fund --no-audit -g npm@8
161-
echo "updated=true" >> "$GITHUB_OUTPUT"
141+
echo "node@$NODE_VERSION"
162142
163-
- name: Install npm@9 on Node 14/16/18.0
164-
shell: bash
165-
if: startsWith(steps.node.outputs.node-version, 'v14.') || startsWith(steps.node.outputs.node-version, 'v16.') || startsWith(steps.node.outputs.node-version, 'v18.0.')
166-
id: npm-9
167-
run: |
168-
npm i --prefer-online --no-fund --no-audit -g npm@9
169-
echo "updated=true" >> "$GITHUB_OUTPUT"
143+
for SPEC in ${SPECS[@]}; do
144+
ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node')
145+
echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)"
146+
147+
if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then
148+
MATCH=$SPEC
149+
echo "Found compatible version: npm@$MATCH"
150+
break
151+
fi
152+
done
153+
154+
if [ -z $MATCH ]; then
155+
echo "Could not find a compatible version of npm for node@$NODE_VERSION"
156+
exit 1
157+
fi
170158
171-
- name: Install npm@latest on Node
172-
if: ${{ !(steps.npm-7.outputs.updated || steps.npm-8.outputs.updated || steps.npm-9.outputs.updated) }}
173-
run: npm i --prefer-online --no-fund --no-audit -g npm@latest
159+
npm i --prefer-online --no-fund --no-audit -g npm@$MATCH
174160
175161
- name: npm Version
176162
run: npm -v

0 commit comments

Comments
 (0)