diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 9422a111f..3ef717c18 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -102,6 +102,24 @@ jobs: - name: Verify node run: __tests__/verify-node.sh 14 + version-file-volta: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest, windows-latest, macos-latest ] + steps: + - uses: actions/checkout@v3 + - name: Remove engines from package.json + shell: bash + run: cat <<< "$(jq 'del(.engines)' ./__tests__/data/package.json)" > ./__tests__/data/package.json + - name: Setup node from node version file + uses: ./ + with: + node-version-file: '__tests__/data/package.json' + - name: Verify node + run: __tests__/verify-node.sh 14 + node-dist: runs-on: ${{ matrix.os }} strategy: diff --git a/__tests__/data/package.json b/__tests__/data/package.json index b201009d6..915f287a3 100644 --- a/__tests__/data/package.json +++ b/__tests__/data/package.json @@ -1,5 +1,8 @@ { "engines": { "node": "^14.0.0" + }, + "volta": { + "node": "14.0.0" } } diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index c1e16b5fc..5cf55f779 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -579,6 +579,7 @@ describe('setup-node', () => { existsSpy.mockImplementationOnce( input => input === path.join(__dirname, 'data', versionFile) ); + // Act await main.run(); diff --git a/dist/setup/index.js b/dist/setup/index.js index 899b0e2e6..5b744d723 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -73530,29 +73530,25 @@ function translateArchToDistUrl(arch) { } } function parseNodeVersionFile(contents) { - var _a, _b; + var _a, _b, _c; let nodeVersion; - // Try parsing the file as an NPM `package.json` - // file. + // Try parsing the file as an NPM `package.json` file. try { - nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node; + nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node; + if (!nodeVersion) + nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node; } - catch (_c) { + catch (_d) { core.warning('Node version file is not JSON file'); } if (!nodeVersion) { - try { - const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = (_b = found === null || found === void 0 ? void 0 : found.groups) === null || _b === void 0 ? void 0 : _b.version; - if (!nodeVersion) - throw new Error(); - } - catch (err) { - // In the case of an unknown format, - // return as is and evaluate the version separately. - nodeVersion = contents.trim(); - } + const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + nodeVersion = (_c = found === null || found === void 0 ? void 0 : found.groups) === null || _c === void 0 ? void 0 : _c.version; } + // In the case of an unknown format, + // return as is and evaluate the version separately. + if (!nodeVersion) + nodeVersion = contents.trim(); return nodeVersion; } exports.parseNodeVersionFile = parseNodeVersionFile; diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 8af1b2422..c0f649a94 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -48,7 +48,7 @@ steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: - node-version: '14' + node-version: '16' check-latest: true - run: npm ci - run: npm test @@ -56,8 +56,9 @@ steps: ## Node version file -The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version` or `.tool-versions`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. -See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax) +The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version`, `.tool-versions`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. +See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax). + > The action will search for the node version file relative to the repository root. ```yaml @@ -70,6 +71,19 @@ steps: - run: npm test ``` +When using the `package.json` input, the action will look for `volta.node` first. If `volta.node` isn't defined, then it will look for `engines.node`. + +```json +{ + "engines": { + "node": ">=16.0.0" + }, + "volta": { + "node": "16.0.0" + } +} +``` + ## Architecture You can use any of the [supported operating systems](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners), and the compatible `architecture` can be selected using `architecture`. Values are `x86`, `x64`, `arm64`, `armv6l`, `armv7l`, `ppc64le`, `s390x` (not all of the architectures are available on all platforms). diff --git a/src/installer.ts b/src/installer.ts index c74fbc97c..bd9c8423d 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -497,27 +497,23 @@ function translateArchToDistUrl(arch: string): string { export function parseNodeVersionFile(contents: string): string { let nodeVersion: string | undefined; - // Try parsing the file as an NPM `package.json` - // file. + // Try parsing the file as an NPM `package.json` file. try { - nodeVersion = JSON.parse(contents).engines?.node; + nodeVersion = JSON.parse(contents).volta?.node; + if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; } catch { core.warning('Node version file is not JSON file'); } if (!nodeVersion) { - try { - const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = found?.groups?.version; - - if (!nodeVersion) throw new Error(); - } catch (err) { - // In the case of an unknown format, - // return as is and evaluate the version separately. - nodeVersion = contents.trim(); - } + const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + nodeVersion = found?.groups?.version; } + // In the case of an unknown format, + // return as is and evaluate the version separately. + if (!nodeVersion) nodeVersion = contents.trim(); + return nodeVersion as string; } diff --git a/src/main.ts b/src/main.ts index ac7e51f5c..d43614e6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -95,14 +95,17 @@ function resolveVersionInput(): string { process.env.GITHUB_WORKSPACE!, versionFileInput ); + if (!fs.existsSync(versionFilePath)) { throw new Error( `The specified node version file at: ${versionFilePath} does not exist` ); } + version = installer.parseNodeVersionFile( fs.readFileSync(versionFilePath, 'utf8') ); + core.info(`Resolved ${versionFileInput} as ${version}`); } diff --git a/tsconfig.json b/tsconfig.json index 8405b0a1f..84dd72010 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,7 @@ "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "sourceMap": true, "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ + "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ }, "exclude": ["__tests__", "lib", "node_modules"]