diff --git a/dist/setup/index.js b/dist/setup/index.js index 28d5dc8f..0a0c0bad 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -71768,15 +71768,25 @@ function translateArchToDistUrl(arch) { } } function parseNodeVersionFile(contents) { - var _a; + var _a, _b; + let nodeVersion; const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - const nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version; - if (nodeVersion) { - return nodeVersion; + nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version; + 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; + 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(); + } } - // In the case of an unknown format, - // return as is and evaluate the version separately. - return contents.trim(); + return nodeVersion; } exports.parseNodeVersionFile = parseNodeVersionFile; function isLatestSyntax(versionSpec) { diff --git a/src/installer.ts b/src/installer.ts index 8fa44b01..193ff16a 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -495,24 +495,26 @@ function translateArchToDistUrl(arch: string): string { } export function parseNodeVersionFile(contents: string): string { - // In the case of an unknown format, - // return as is and evaluate the version separately. - let nodeVersion = contents.trim(); + let nodeVersion: string | undefined; - try { - // Assume all content parseable as JSON is a valid, - // NPM `package.json` file - const packageJson = JSON.parse(contents); - nodeVersion = packageJson.engines.node || nodeVersion; + const found = contents.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); + nodeVersion = found?.groups?.version; - if (!nodeVersion) throw - } catch (err) { - // If not, assume it is a node version file - const found = nodeVersion.match(/^(?:nodejs\s+)?v?(?[^\s]+)$/m); - nodeVersion = found?.groups?.version || nodeVersion; + if (!nodeVersion) { + try { + // Try parsing the file as an NPM `package.json` + // file. + nodeVersion = JSON.parse(contents).engines?.node; + + 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(); + } } - return nodeVersion; + return nodeVersion as string; } function isLatestSyntax(versionSpec): boolean {