Skip to content

Commit 9f3a02b

Browse files
Merge pull request #532 from jef/main
feat: add volta as node-version-file
2 parents 30f0e7d + 4cffe5c commit 9f3a02b

File tree

8 files changed

+64
-33
lines changed

8 files changed

+64
-33
lines changed

Diff for: .github/workflows/versions.yml

+18
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ jobs:
102102
- name: Verify node
103103
run: __tests__/verify-node.sh 14
104104

105+
version-file-volta:
106+
runs-on: ${{ matrix.os }}
107+
strategy:
108+
fail-fast: false
109+
matrix:
110+
os: [ ubuntu-latest, windows-latest, macos-latest ]
111+
steps:
112+
- uses: actions/checkout@v3
113+
- name: Remove engines from package.json
114+
shell: bash
115+
run: cat <<< "$(jq 'del(.engines)' ./__tests__/data/package.json)" > ./__tests__/data/package.json
116+
- name: Setup node from node version file
117+
uses: ./
118+
with:
119+
node-version-file: '__tests__/data/package.json'
120+
- name: Verify node
121+
run: __tests__/verify-node.sh 14
122+
105123
node-dist:
106124
runs-on: ${{ matrix.os }}
107125
strategy:

Diff for: __tests__/data/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"engines": {
33
"node": "^14.0.0"
4+
},
5+
"volta": {
6+
"node": "14.0.0"
47
}
58
}

Diff for: __tests__/installer.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ describe('setup-node', () => {
579579
existsSpy.mockImplementationOnce(
580580
input => input === path.join(__dirname, 'data', versionFile)
581581
);
582+
582583
// Act
583584
await main.run();
584585

Diff for: dist/setup/index.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -73530,29 +73530,25 @@ function translateArchToDistUrl(arch) {
7353073530
}
7353173531
}
7353273532
function parseNodeVersionFile(contents) {
73533-
var _a, _b;
73533+
var _a, _b, _c;
7353473534
let nodeVersion;
73535-
// Try parsing the file as an NPM `package.json`
73536-
// file.
73535+
// Try parsing the file as an NPM `package.json` file.
7353773536
try {
73538-
nodeVersion = (_a = JSON.parse(contents).engines) === null || _a === void 0 ? void 0 : _a.node;
73537+
nodeVersion = (_a = JSON.parse(contents).volta) === null || _a === void 0 ? void 0 : _a.node;
73538+
if (!nodeVersion)
73539+
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
7353973540
}
73540-
catch (_c) {
73541+
catch (_d) {
7354173542
core.warning('Node version file is not JSON file');
7354273543
}
7354373544
if (!nodeVersion) {
73544-
try {
73545-
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
73546-
nodeVersion = (_b = found === null || found === void 0 ? void 0 : found.groups) === null || _b === void 0 ? void 0 : _b.version;
73547-
if (!nodeVersion)
73548-
throw new Error();
73549-
}
73550-
catch (err) {
73551-
// In the case of an unknown format,
73552-
// return as is and evaluate the version separately.
73553-
nodeVersion = contents.trim();
73554-
}
73545+
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
73546+
nodeVersion = (_c = found === null || found === void 0 ? void 0 : found.groups) === null || _c === void 0 ? void 0 : _c.version;
7355573547
}
73548+
// In the case of an unknown format,
73549+
// return as is and evaluate the version separately.
73550+
if (!nodeVersion)
73551+
nodeVersion = contents.trim();
7355673552
return nodeVersion;
7355773553
}
7355873554
exports.parseNodeVersionFile = parseNodeVersionFile;

Diff for: docs/advanced-usage.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ steps:
4848
- uses: actions/checkout@v3
4949
- uses: actions/setup-node@v3
5050
with:
51-
node-version: '14'
51+
node-version: '16'
5252
check-latest: true
5353
- run: npm ci
5454
- run: npm test
5555
```
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`, `.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.
60-
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax)
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`, `.tool-versions`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.
60+
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax).
61+
6162
> The action will search for the node version file relative to the repository root.
6263

6364
```yaml
@@ -70,6 +71,19 @@ steps:
7071
- run: npm test
7172
```
7273

74+
When using the `package.json` input, the action will look for `volta.node` first. If `volta.node` isn't defined, then it will look for `engines.node`.
75+
76+
```json
77+
{
78+
"engines": {
79+
"node": ">=16.0.0"
80+
},
81+
"volta": {
82+
"node": "16.0.0"
83+
}
84+
}
85+
```
86+
7387
## Architecture
7488

7589
You can use any of the [supported operating systems](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners), and the compatible `architecture` can be selected using `architecture`. Values are `x86`, `x64`, `arm64`, `armv6l`, `armv7l`, `ppc64le`, `s390x` (not all of the architectures are available on all platforms).

Diff for: src/installer.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -497,27 +497,23 @@ function translateArchToDistUrl(arch: string): string {
497497
export function parseNodeVersionFile(contents: string): string {
498498
let nodeVersion: string | undefined;
499499

500-
// Try parsing the file as an NPM `package.json`
501-
// file.
500+
// Try parsing the file as an NPM `package.json` file.
502501
try {
503-
nodeVersion = JSON.parse(contents).engines?.node;
502+
nodeVersion = JSON.parse(contents).volta?.node;
503+
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
504504
} catch {
505505
core.warning('Node version file is not JSON file');
506506
}
507507

508508
if (!nodeVersion) {
509-
try {
510-
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
511-
nodeVersion = found?.groups?.version;
512-
513-
if (!nodeVersion) throw new Error();
514-
} catch (err) {
515-
// In the case of an unknown format,
516-
// return as is and evaluate the version separately.
517-
nodeVersion = contents.trim();
518-
}
509+
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
510+
nodeVersion = found?.groups?.version;
519511
}
520512

513+
// In the case of an unknown format,
514+
// return as is and evaluate the version separately.
515+
if (!nodeVersion) nodeVersion = contents.trim();
516+
521517
return nodeVersion as string;
522518
}
523519

Diff for: src/main.ts

+3
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@ function resolveVersionInput(): string {
9595
process.env.GITHUB_WORKSPACE!,
9696
versionFileInput
9797
);
98+
9899
if (!fs.existsSync(versionFilePath)) {
99100
throw new Error(
100101
`The specified node version file at: ${versionFilePath} does not exist`
101102
);
102103
}
104+
103105
version = installer.parseNodeVersionFile(
104106
fs.readFileSync(versionFilePath, 'utf8')
105107
);
108+
106109
core.info(`Resolved ${versionFileInput} as ${version}`);
107110
}
108111

Diff for: tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
77
"sourceMap": true,
88
"strict": true, /* Enable all strict type-checking options. */
9-
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
9+
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
1010
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
1111
},
1212
"exclude": ["__tests__", "lib", "node_modules"]

0 commit comments

Comments
 (0)