|
| 1 | +# This file is automatically added by @npmcli/template-oss. Do not edit. |
| 2 | + |
| 3 | +name: 'Install Latest npm' |
| 4 | +description: 'Install the latest version of npm compatible with the Node version' |
| 5 | +inputs: |
| 6 | + node: |
| 7 | + description: 'Current Node version' |
| 8 | + required: true |
| 9 | +runs: |
| 10 | + using: "composite" |
| 11 | + steps: |
| 12 | + # node 10/12/14 ship with npm@6, which is known to fail when updating itself in windows |
| 13 | + - name: Update Windows npm |
| 14 | + if: | |
| 15 | + runner.os == 'Windows' && ( |
| 16 | + startsWith(inputs.node, 'v10.') || |
| 17 | + startsWith(inputs.node, 'v12.') || |
| 18 | + startsWith(inputs.node, 'v14.') |
| 19 | + ) |
| 20 | + shell: cmd |
| 21 | + run: | |
| 22 | + curl -sO https://registry.npmjs.org/npm/-/npm-7.5.4.tgz |
| 23 | + tar xf npm-7.5.4.tgz |
| 24 | + cd package |
| 25 | + node lib/npm.js install --no-fund --no-audit -g ..\npm-7.5.4.tgz |
| 26 | + cd .. |
| 27 | + rmdir /s /q package |
| 28 | + - name: Install Latest npm |
| 29 | + shell: bash |
| 30 | + env: |
| 31 | + NODE_VERSION: ${{ inputs.node }} |
| 32 | + working-directory: ${{ runner.temp }} |
| 33 | + run: | |
| 34 | + MATCH="" |
| 35 | + SPECS=("latest" "next-10" "next-9" "next-8" "next-7" "next-6") |
| 36 | +
|
| 37 | + echo "node@$NODE_VERSION" |
| 38 | +
|
| 39 | + for SPEC in ${SPECS[@]}; do |
| 40 | + ENGINES=$(npm view npm@$SPEC --json | jq -r '.engines.node') |
| 41 | + echo "Checking if node@$NODE_VERSION satisfies npm@$SPEC ($ENGINES)" |
| 42 | +
|
| 43 | + if npx semver -r "$ENGINES" "$NODE_VERSION" > /dev/null; then |
| 44 | + MATCH=$SPEC |
| 45 | + echo "Found compatible version: npm@$MATCH" |
| 46 | + break |
| 47 | + fi |
| 48 | + done |
| 49 | +
|
| 50 | + if [ -z $MATCH ]; then |
| 51 | + echo "Could not find a compatible version of npm for node@$NODE_VERSION" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +
|
| 55 | + npm i --prefer-online --no-fund --no-audit -g npm@$MATCH |
| 56 | + - name: npm Version |
| 57 | + shell: bash |
| 58 | + run: npm -v |
0 commit comments