Skip to content

Commit 2a814b5

Browse files
authored
Respect package.json's engines.node field when used as a node-version-file (#485)
* Allow reading 'package.json' as node-version-file * Run 'npm run build' * Read package.json contents directly during tests - this eliminates OS-specific line-ending issues * Run project Prettier 💅
1 parent 2fddd88 commit 2a814b5

File tree

4 files changed

+65
-13
lines changed

4 files changed

+65
-13
lines changed

__tests__/data/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"engines": {
3+
"node": ">=14.0.0"
4+
}
5+
}

__tests__/installer.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,33 @@ describe('setup-node', () => {
591591
);
592592
});
593593

594+
it('reads package.json as node-version-file if provided', async () => {
595+
// Arrange
596+
const versionSpec = fs.readFileSync(
597+
path.join(__dirname, 'data/package.json'),
598+
'utf-8'
599+
);
600+
const versionFile = 'package.json';
601+
const expectedVersionSpec = '14';
602+
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
603+
inputs['node-version-file'] = versionFile;
604+
605+
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
606+
existsSpy.mockImplementationOnce(
607+
input => input === path.join(__dirname, 'data', versionFile)
608+
);
609+
// Act
610+
await main.run();
611+
612+
// Assert
613+
expect(existsSpy).toHaveBeenCalledTimes(1);
614+
expect(existsSpy).toHaveReturnedWith(true);
615+
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
616+
expect(logSpy).toHaveBeenCalledWith(
617+
`Resolved ${versionFile} as ${expectedVersionSpec}`
618+
);
619+
});
620+
594621
it('both node-version-file and node-version are provided', async () => {
595622
inputs['node-version'] = '12';
596623
const versionSpec = 'v14';

dist/setup/index.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71768,15 +71768,25 @@ function translateArchToDistUrl(arch) {
7176871768
}
7176971769
}
7177071770
function parseNodeVersionFile(contents) {
71771-
var _a;
71771+
var _a, _b;
71772+
let nodeVersion;
7177271773
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;
71774+
nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
71775+
if (!nodeVersion) {
71776+
try {
71777+
// Try parsing the file as an NPM `package.json`
71778+
// file.
71779+
nodeVersion = (_b = JSON.parse(contents).engines) === null || _b === void 0 ? void 0 : _b.node;
71780+
if (!nodeVersion)
71781+
throw new Error();
71782+
}
71783+
catch (err) {
71784+
// In the case of an unknown format,
71785+
// return as is and evaluate the version separately.
71786+
nodeVersion = contents.trim();
71787+
}
7177671788
}
71777-
// In the case of an unknown format,
71778-
// return as is and evaluate the version separately.
71779-
return contents.trim();
71789+
return nodeVersion;
7178071790
}
7178171791
exports.parseNodeVersionFile = parseNodeVersionFile;
7178271792
function isLatestSyntax(versionSpec) {

src/installer.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,26 @@ function translateArchToDistUrl(arch: string): string {
495495
}
496496

497497
export function parseNodeVersionFile(contents: string): string {
498+
let nodeVersion: string | undefined;
499+
498500
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
499-
const nodeVersion = found?.groups?.version;
501+
nodeVersion = found?.groups?.version;
502+
503+
if (!nodeVersion) {
504+
try {
505+
// Try parsing the file as an NPM `package.json`
506+
// file.
507+
nodeVersion = JSON.parse(contents).engines?.node;
500508

501-
if (nodeVersion) {
502-
return nodeVersion;
509+
if (!nodeVersion) throw new Error();
510+
} catch (err) {
511+
// In the case of an unknown format,
512+
// return as is and evaluate the version separately.
513+
nodeVersion = contents.trim();
514+
}
503515
}
504516

505-
// In the case of an unknown format,
506-
// return as is and evaluate the version separately.
507-
return contents.trim();
517+
return nodeVersion as string;
508518
}
509519

510520
function isLatestSyntax(versionSpec): boolean {

0 commit comments

Comments
 (0)