Skip to content

Commit

Permalink
Fix node version file parsing (#553)
Browse files Browse the repository at this point in the history
* Fix node version file parsing

* Build index.js

* Non-json file error handling

* Format code

* Add package.json to e2e tests

* Minor fix
  • Loading branch information
Vladimir Safonkin authored Aug 4, 2022
1 parent 2a814b5 commit 348e008
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion __tests__/data/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"engines": {
"node": ">=14.0.0"
"node": "^14.0.0"
}
}
15 changes: 10 additions & 5 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71770,13 +71770,18 @@ function translateArchToDistUrl(arch) {
function parseNodeVersionFile(contents) {
var _a, _b;
let nodeVersion;
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\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).engines) === null || _a === void 0 ? void 0 : _a.node;
}
catch (_c) {
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).engines) === null || _b === void 0 ? void 0 : _b.node;
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\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();
}
Expand Down
14 changes: 9 additions & 5 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,18 @@ function translateArchToDistUrl(arch: string): string {
export function parseNodeVersionFile(contents: string): string {
let nodeVersion: string | undefined;

const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;
// Try parsing the file as an NPM `package.json`
// file.
try {
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).engines?.node;
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;

if (!nodeVersion) throw new Error();
} catch (err) {
Expand Down

0 comments on commit 348e008

Please sign in to comment.