From 389036835e6197253dbe84cdac1af7c32e74582d Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 11:17:53 +0200 Subject: [PATCH 1/6] Fix node version file parsing --- src/installer.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/installer.ts b/src/installer.ts index 193ff16a7..a9afb2ff9 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -497,14 +497,14 @@ 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. + nodeVersion = JSON.parse(contents).engines?.node; if (!nodeVersion) { try { - // Try parsing the file as an NPM `package.json` - // file. - 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) { From 0db4699b29675e7bc6be5d655d4b8aa7decc7bc7 Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 11:24:08 +0200 Subject: [PATCH 2/6] Build index.js --- dist/setup/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 0a0c0bade..d7fd9f84c 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71770,13 +71770,13 @@ function translateArchToDistUrl(arch) { function parseNodeVersionFile(contents) { var _a, _b; 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. + nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node; if (!nodeVersion) { try { - // Try parsing the file as an NPM `package.json` - // file. - nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node; + 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(); } From e22d92112e4396ad95d74ff483273903d6159100 Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 11:30:15 +0200 Subject: [PATCH 3/6] Non-json file error handling --- dist/setup/index.js | 7 ++++++- src/installer.ts | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index d7fd9f84c..a5ae4a27b 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71772,7 +71772,12 @@ function parseNodeVersionFile(contents) { let nodeVersion; // Try parsing the file as an NPM `package.json` // file. - nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node; + try { + nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node; + } + catch (_c) { + core.warning("Node version file is not JSON file"); + } if (!nodeVersion) { try { const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); diff --git a/src/installer.ts b/src/installer.ts index a9afb2ff9..c724c6f60 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -499,7 +499,11 @@ export function parseNodeVersionFile(contents: string): string { // Try parsing the file as an NPM `package.json` // file. - nodeVersion = JSON.parse(contents).engines?.node; + try { + nodeVersion = JSON.parse(contents).engines?.node; + } catch { + core.warning("Node version file is not JSON file") + } if (!nodeVersion) { try { From 72615645bbaa5ce1ab111373b7a78db46fde2f6c Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 11:32:42 +0200 Subject: [PATCH 4/6] Format code --- dist/setup/index.js | 2 +- src/installer.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index a5ae4a27b..0407c1ce2 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71776,7 +71776,7 @@ function parseNodeVersionFile(contents) { nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node; } catch (_c) { - core.warning("Node version file is not JSON file"); + core.warning('Node version file is not JSON file'); } if (!nodeVersion) { try { diff --git a/src/installer.ts b/src/installer.ts index c724c6f60..c74fbc97c 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -502,7 +502,7 @@ export function parseNodeVersionFile(contents: string): string { try { nodeVersion = JSON.parse(contents).engines?.node; } catch { - core.warning("Node version file is not JSON file") + core.warning('Node version file is not JSON file'); } if (!nodeVersion) { From 14a17a7dd133ad2141324454eb0f6c7b6c2e4ad2 Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 12:23:07 +0200 Subject: [PATCH 5/6] Add package.json to e2e tests --- .github/workflows/versions.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 1af0c125f..9422a111f 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 From 18c26e10b22cd59d84b984da71cb6432a7a95cf6 Mon Sep 17 00:00:00 2001 From: Vladimir Safonkin Date: Wed, 3 Aug 2022 12:30:28 +0200 Subject: [PATCH 6/6] Minor fix --- __tests__/data/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/data/package.json b/__tests__/data/package.json index e537e2005..b201009d6 100644 --- a/__tests__/data/package.json +++ b/__tests__/data/package.json @@ -1,5 +1,5 @@ { "engines": { - "node": ">=14.0.0" + "node": "^14.0.0" } }