diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 1af0c125..9422a111 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -92,7 +92,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - node-version-file: [.nvmrc, .tool-versions] + node-version-file: [.nvmrc, .tool-versions, package.json] steps: - uses: actions/checkout@v3 - name: Setup node from node version file diff --git a/__tests__/data/package.json b/__tests__/data/package.json index 34063c3a..915f287a 100644 --- a/__tests__/data/package.json +++ b/__tests__/data/package.json @@ -1,6 +1,6 @@ { "engines": { - "node": ">=14.0.0" + "node": "^14.0.0" }, "volta": { "node": "14.0.0" diff --git a/dist/setup/index.js b/dist/setup/index.js index 84095537..efd8162a 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71770,14 +71770,19 @@ function translateArchToDistUrl(arch) { function parseNodeVersionFile(contents) { var _a, _b, _c; let nodeVersion; - const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version; + // Try parsing the file as an NPM `package.json` file. + try { + 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 (_d) { + core.warning('Node version file is not JSON file'); + } if (!nodeVersion) { try { - // Try parsing the file as an NPM `package.json` file. - nodeVersion = (_b = JSON.parse(contents).volta) === null || _b === void 0 ? void 0 : _b.node; - if (!nodeVersion) - nodeVersion = (_c = JSON.parse(contents).engines) === null || _c === void 0 ? void 0 : _c.node; + 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; if (!nodeVersion) throw new Error(); } diff --git a/src/installer.ts b/src/installer.ts index 6414bfa9..1e5e5623 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -497,14 +497,19 @@ function translateArchToDistUrl(arch: string): string { export function parseNodeVersionFile(contents: string): string { let nodeVersion: string | undefined; - const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = found?.groups?.version; + // Try parsing the file as an NPM `package.json` file. + try { + 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 { - // Try parsing the file as an NPM `package.json` file. - nodeVersion = JSON.parse(contents).volta?.node; - if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node; + 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,