|
| 1 | +name: get-node-version |
| 2 | +description: extracts the node version from the package.json engines |
| 3 | +author: datavisyn |
| 4 | + |
| 5 | +outputs: |
| 6 | + node_version: |
| 7 | + description: "extracted node version" |
| 8 | + value: ${{ steps.get-node-version.outputs.result }} |
| 9 | + |
| 10 | +runs: |
| 11 | + using: "composite" |
| 12 | + steps: |
| 13 | + - name: Setup sandboxed dependencies |
| 14 | + shell: bash |
| 15 | + run: | |
| 16 | + npm init -y --prefix ${{ github.action_path }} >/dev/null 2>&1 |
| 17 | + npm install semver --prefix ${{ github.action_path }} >/dev/null 2>&1 |
| 18 | +
|
| 19 | + - name: Find version script |
| 20 | + id: get-node-version |
| 21 | + uses: actions/github-script@v7 |
| 22 | + with: |
| 23 | + script: | |
| 24 | + const path = require('path'); |
| 25 | + const fs = require('fs'); |
| 26 | +
|
| 27 | + // Load the sandboxed semver dependency from the action's own node_modules |
| 28 | + const semver = require(path.join(process.env.GITHUB_ACTION_PATH, 'node_modules/semver')); |
| 29 | + |
| 30 | + // ✅ Define your extendable array of versions to check |
| 31 | + const versionsToCheck = ['24.10', '20.9', '18.17.0']; |
| 32 | +
|
| 33 | + const packageJsonPath = path.join(process.env.GITHUB_WORKSPACE, 'package.json'); |
| 34 | + if (!fs.existsSync(packageJsonPath)) { |
| 35 | + core.setFailed(`Error: package.json not found at ${packageJsonPath}`); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 40 | + const requiredRange = packageJson.engines?.node; |
| 41 | +
|
| 42 | + if (!requiredRange) { |
| 43 | + core.info('No node engine requirement in package.json. Returning empty.'); |
| 44 | + return ''; |
| 45 | + } |
| 46 | +
|
| 47 | + core.info(`Required Node.js range: ${requiredRange}`); |
| 48 | +
|
| 49 | + for (const version of versionsToCheck) { |
| 50 | + if (semver.satisfies(version, requiredRange)) { |
| 51 | + core.info(`\n✅ Match found! Returning version ${version}.`); |
| 52 | + // Simply return the matching version string to set the 'result' output |
| 53 | + return version; |
| 54 | + } |
| 55 | + } |
| 56 | +
|
| 57 | + core.setFailed(`\n❌ No compatible Node.js version found in the list for range "${requiredRange}".`); |
0 commit comments