Skip to content

Commit 5b949b5

Browse files
Merge pull request #373 from ganta/add-support-for-asdf-format-as-node-version-file
Add support for asdf format as Node.js version file
2 parents 09ba51f + dbb64ac commit 5b949b5

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

Diff for: .github/workflows/versions.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ jobs:
9292
fail-fast: false
9393
matrix:
9494
os: [ubuntu-latest, windows-latest, macos-latest]
95+
node-version-file: [.nvmrc, .tool-versions]
9596
steps:
9697
- uses: actions/checkout@v3
9798
- name: Setup node from node version file
9899
uses: ./
99100
with:
100-
node-version-file: '__tests__/data/.nvmrc'
101+
node-version-file: '__tests__/data/${{ matrix.node-version-file }}'
101102
- name: Verify node
102103
run: __tests__/verify-node.sh 14
103104

Diff for: __tests__/data/.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 14.0.0

Diff for: __tests__/installer.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import fs from 'fs';
88
import cp from 'child_process';
99
import osm = require('os');
1010
import path from 'path';
11+
import each from 'jest-each';
1112
import * as main from '../src/main';
1213
import * as auth from '../src/authutil';
1314

@@ -904,3 +905,23 @@ describe('setup-node', () => {
904905
);
905906
});
906907
});
908+
909+
describe('helper methods', () => {
910+
describe('parseNodeVersionFile', () => {
911+
each`
912+
contents | expected
913+
${'12'} | ${'12'}
914+
${'12.3'} | ${'12.3'}
915+
${'12.3.4'} | ${'12.3.4'}
916+
${'v12.3.4'} | ${'12.3.4'}
917+
${'lts/erbium'} | ${'lts/erbium'}
918+
${'lts/*'} | ${'lts/*'}
919+
${'nodejs 12.3.4'} | ${'12.3.4'}
920+
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
921+
${''} | ${''}
922+
${'unknown format'} | ${'unknown format'}
923+
`.it('parses "$contents"', ({contents, expected}) => {
924+
expect(im.parseNodeVersionFile(contents)).toBe(expected);
925+
});
926+
});
927+
});

Diff for: action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ inputs:
88
node-version:
99
description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0.'
1010
node-version-file:
11-
description: 'File containing the version Spec of the version to use. Examples: .nvmrc, .node-version.'
11+
description: 'File containing the version Spec of the version to use. Examples: .nvmrc, .node-version, .tool-versions.'
1212
architecture:
1313
description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.'
1414
check-latest:

Diff for: dist/setup/index.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -71768,11 +71768,15 @@ function translateArchToDistUrl(arch) {
7176871768
}
7176971769
}
7177071770
function parseNodeVersionFile(contents) {
71771-
let nodeVersion = contents.trim();
71772-
if (/^v\d/.test(nodeVersion)) {
71773-
nodeVersion = nodeVersion.substring(1);
71774-
}
71775-
return nodeVersion;
71771+
var _a;
71772+
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
71773+
const nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
71774+
if (nodeVersion) {
71775+
return nodeVersion;
71776+
}
71777+
// In the case of an unknown format,
71778+
// return as is and evaluate the version separately.
71779+
return contents.trim();
7177671780
}
7177771781
exports.parseNodeVersionFile = parseNodeVersionFile;
7177871782
function isLatestSyntax(versionSpec) {

Diff for: docs/advanced-usage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ steps:
5656
5757
## Node version file
5858
59-
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` or `.node-version`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.
59+
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.
6060
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax)
6161
> The action will search for the node version file relative to the repository root.
6262

Diff for: src/installer.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,16 @@ function translateArchToDistUrl(arch: string): string {
495495
}
496496

497497
export function parseNodeVersionFile(contents: string): string {
498-
let nodeVersion = contents.trim();
498+
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
499+
const nodeVersion = found?.groups?.version;
499500

500-
if (/^v\d/.test(nodeVersion)) {
501-
nodeVersion = nodeVersion.substring(1);
501+
if (nodeVersion) {
502+
return nodeVersion;
502503
}
503-
return nodeVersion;
504+
505+
// In the case of an unknown format,
506+
// return as is and evaluate the version separately.
507+
return contents.trim();
504508
}
505509

506510
function isLatestSyntax(versionSpec): boolean {

0 commit comments

Comments
 (0)