Skip to content

Commit 774c1d6

Browse files
feat(node-version-file): support parsing devEngines field (#1283)
* feat(node-version-file): support parsing `devEngines` field Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> * test: adjust for array like `devEngines` Co-authored-by: Grigory <grigory.orlov.set@gmail.com> Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> * ci(versions.yml): update actions and reduce duplicated tests Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> * docs: consolidate advanced usage Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> * chore: compile assets Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> --------- Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de> Co-authored-by: Grigory <grigory.orlov.set@gmail.com>
1 parent efcb663 commit 774c1d6

File tree

7 files changed

+93
-21
lines changed

7 files changed

+93
-21
lines changed

.github/workflows/versions.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ jobs:
168168
- name: Verify node
169169
run: __tests__/verify-node.sh 24
170170

171+
version-file-dev-engines:
172+
runs-on: ${{ matrix.os }}
173+
strategy:
174+
fail-fast: false
175+
matrix:
176+
os: [ubuntu-latest, windows-latest, macos-latest]
177+
steps:
178+
- uses: actions/checkout@v6
179+
- name: Setup node from node version file
180+
uses: ./
181+
with:
182+
node-version-file: '__tests__/data/package-dev-engines.json'
183+
- name: Verify node
184+
run: __tests__/verify-node.sh 20
185+
171186
version-file-volta:
172187
runs-on: ${{ matrix.os }}
173188
strategy:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"engines": {
3+
"node": "^19"
4+
},
5+
"devEngines": {
6+
"runtime": {
7+
"name": "node",
8+
"version": "^20"
9+
}
10+
}
11+
}

__tests__/main.test.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,24 @@ describe('main tests', () => {
9494

9595
describe('getNodeVersionFromFile', () => {
9696
each`
97-
contents | expected
98-
${'12'} | ${'12'}
99-
${'12.3'} | ${'12.3'}
100-
${'12.3.4'} | ${'12.3.4'}
101-
${'v12.3.4'} | ${'12.3.4'}
102-
${'lts/erbium'} | ${'lts/erbium'}
103-
${'lts/*'} | ${'lts/*'}
104-
${'nodejs 12.3.4'} | ${'12.3.4'}
105-
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
106-
${''} | ${''}
107-
${'unknown format'} | ${'unknown format'}
108-
${' 14.1.0 '} | ${'14.1.0'}
109-
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'}| ${'>=14.0.0 <=17.0.0'}
110-
${'{"volta": {"extends": "./package.json"}}'}| ${'18.0.0'}
111-
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
112-
${'{}'} | ${null}
97+
contents | expected
98+
${'12'} | ${'12'}
99+
${'12.3'} | ${'12.3'}
100+
${'12.3.4'} | ${'12.3.4'}
101+
${'v12.3.4'} | ${'12.3.4'}
102+
${'lts/erbium'} | ${'lts/erbium'}
103+
${'lts/*'} | ${'lts/*'}
104+
${'nodejs 12.3.4'} | ${'12.3.4'}
105+
${'ruby 2.3.4\nnodejs 12.3.4\npython 3.4.5'} | ${'12.3.4'}
106+
${''} | ${''}
107+
${'unknown format'} | ${'unknown format'}
108+
${' 14.1.0 '} | ${'14.1.0'}
109+
${'{}'} | ${null}
110+
${'{"volta": {"node": ">=14.0.0 <=17.0.0"}}'} | ${'>=14.0.0 <=17.0.0'}
111+
${'{"volta": {"extends": "./package.json"}}'} | ${'18.0.0'}
112+
${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'}
113+
${'{"devEngines": {"runtime": {"name": "node", "version": "22.0.0"}}}'} | ${'22.0.0'}
114+
${'{"devEngines": {"runtime": [{"name": "bun"}, {"name": "node", "version": "22.0.0"}]}}'} | ${'22.0.0'}
113115
`.it('parses "$contents"', ({contents, expected}) => {
114116
const existsSpy = jest.spyOn(fs, 'existsSync');
115117
existsSpy.mockImplementation(() => true);

dist/cache-save/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71877,6 +71877,17 @@ function getNodeVersionFromFile(versionFilePath) {
7187771877
if (manifest.volta?.node) {
7187871878
return manifest.volta.node;
7187971879
}
71880+
// support devEngines from npm 11
71881+
if (manifest.devEngines?.runtime) {
71882+
// find an entry with name set to node and having set a version.
71883+
// the devEngines.runtime can either be an object or an array of objects
71884+
const nodeEntry = [manifest.devEngines.runtime]
71885+
.flat()
71886+
.find(({ name, version }) => name?.toLowerCase() === 'node' && version);
71887+
if (nodeEntry) {
71888+
return nodeEntry.version;
71889+
}
71890+
}
7188071891
if (manifest.engines?.node) {
7188171892
return manifest.engines.node;
7188271893
}

dist/setup/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82415,6 +82415,17 @@ function getNodeVersionFromFile(versionFilePath) {
8241582415
if (manifest.volta?.node) {
8241682416
return manifest.volta.node;
8241782417
}
82418+
// support devEngines from npm 11
82419+
if (manifest.devEngines?.runtime) {
82420+
// find an entry with name set to node and having set a version.
82421+
// the devEngines.runtime can either be an object or an array of objects
82422+
const nodeEntry = [manifest.devEngines.runtime]
82423+
.flat()
82424+
.find(({ name, version }) => name?.toLowerCase() === 'node' && version);
82425+
if (nodeEntry) {
82426+
return nodeEntry.version;
82427+
}
82428+
}
8241882429
if (manifest.engines?.node) {
8241982430
return manifest.engines.node;
8242082431
}

docs/advanced-usage.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,31 @@ steps:
9090
- run: npm test
9191
```
9292

93-
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`.
93+
When using the `package.json` input, the action will look in the following fields for a specified Node version:
94+
1. It checks `volta.node` first.
95+
2. Then it checks `devEngines.runtime` for an entry with `"name": "node"`.
96+
3. Then it will look for `engines.node`.
97+
4. Otherwise it tries to resolve the file defined by [`volta.extends`](https://docs.volta.sh/advanced/workspaces)
98+
and look for `volta.node`, `devEngines.runtime`, or `engines.node` recursively.
99+
94100

95101
```json
96102
{
97103
"engines": {
98-
"node": ">=16.0.0"
104+
"node": "^22 || ^24"
105+
},
106+
"devEngines": {
107+
"runtime": {
108+
"name": "node",
109+
"version": "^24.3"
110+
}
99111
},
100112
"volta": {
101-
"node": "16.0.0"
113+
"node": "24.11.1"
102114
}
103115
}
104116
```
105117

106-
Otherwise, when [`volta.extends`](https://docs.volta.sh/advanced/workspaces) is defined, then it will resolve the corresponding file and look for `volta.node` or `engines.node` recursively.
107-
108118
## Architecture
109119

110120
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).

src/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null {
2626
return manifest.volta.node;
2727
}
2828

29+
// support devEngines from npm 11
30+
if (manifest.devEngines?.runtime) {
31+
// find an entry with name set to node and having set a version.
32+
// the devEngines.runtime can either be an object or an array of objects
33+
const nodeEntry = [manifest.devEngines.runtime]
34+
.flat()
35+
.find(({name, version}) => name?.toLowerCase() === 'node' && version);
36+
if (nodeEntry) {
37+
return nodeEntry.version;
38+
}
39+
}
40+
2941
if (manifest.engines?.node) {
3042
return manifest.engines.node;
3143
}

0 commit comments

Comments
 (0)