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 💅
This commit is contained in:
parent
2fddd8803e
commit
2a814b57e1
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.0.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -591,6 +591,33 @@ describe('setup-node', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('reads package.json as node-version-file if provided', async () => {
|
||||||
|
// Arrange
|
||||||
|
const versionSpec = fs.readFileSync(
|
||||||
|
path.join(__dirname, 'data/package.json'),
|
||||||
|
'utf-8'
|
||||||
|
);
|
||||||
|
const versionFile = 'package.json';
|
||||||
|
const expectedVersionSpec = '14';
|
||||||
|
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
|
||||||
|
inputs['node-version-file'] = versionFile;
|
||||||
|
|
||||||
|
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
|
||||||
|
existsSpy.mockImplementationOnce(
|
||||||
|
input => input === path.join(__dirname, 'data', versionFile)
|
||||||
|
);
|
||||||
|
// Act
|
||||||
|
await main.run();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(existsSpy).toHaveBeenCalledTimes(1);
|
||||||
|
expect(existsSpy).toHaveReturnedWith(true);
|
||||||
|
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
|
||||||
|
expect(logSpy).toHaveBeenCalledWith(
|
||||||
|
`Resolved ${versionFile} as ${expectedVersionSpec}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('both node-version-file and node-version are provided', async () => {
|
it('both node-version-file and node-version are provided', async () => {
|
||||||
inputs['node-version'] = '12';
|
inputs['node-version'] = '12';
|
||||||
const versionSpec = 'v14';
|
const versionSpec = 'v14';
|
||||||
|
|
|
@ -71768,15 +71768,25 @@ function translateArchToDistUrl(arch) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function parseNodeVersionFile(contents) {
|
function parseNodeVersionFile(contents) {
|
||||||
var _a;
|
var _a, _b;
|
||||||
|
let nodeVersion;
|
||||||
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
|
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
|
||||||
const nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
|
nodeVersion = (_a = found === null || found === void 0 ? void 0 : found.groups) === null || _a === void 0 ? void 0 : _a.version;
|
||||||
if (nodeVersion) {
|
if (!nodeVersion) {
|
||||||
return 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;
|
||||||
|
if (!nodeVersion)
|
||||||
|
throw new Error();
|
||||||
}
|
}
|
||||||
|
catch (err) {
|
||||||
// In the case of an unknown format,
|
// In the case of an unknown format,
|
||||||
// return as is and evaluate the version separately.
|
// return as is and evaluate the version separately.
|
||||||
return contents.trim();
|
nodeVersion = contents.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nodeVersion;
|
||||||
}
|
}
|
||||||
exports.parseNodeVersionFile = parseNodeVersionFile;
|
exports.parseNodeVersionFile = parseNodeVersionFile;
|
||||||
function isLatestSyntax(versionSpec) {
|
function isLatestSyntax(versionSpec) {
|
||||||
|
|
|
@ -495,16 +495,26 @@ function translateArchToDistUrl(arch: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseNodeVersionFile(contents: string): string {
|
export function parseNodeVersionFile(contents: string): string {
|
||||||
|
let nodeVersion: string | undefined;
|
||||||
|
|
||||||
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
|
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
|
||||||
const nodeVersion = found?.groups?.version;
|
nodeVersion = found?.groups?.version;
|
||||||
|
|
||||||
if (nodeVersion) {
|
if (!nodeVersion) {
|
||||||
return nodeVersion;
|
try {
|
||||||
}
|
// Try parsing the file as an NPM `package.json`
|
||||||
|
// file.
|
||||||
|
nodeVersion = JSON.parse(contents).engines?.node;
|
||||||
|
|
||||||
|
if (!nodeVersion) throw new Error();
|
||||||
|
} catch (err) {
|
||||||
// In the case of an unknown format,
|
// In the case of an unknown format,
|
||||||
// return as is and evaluate the version separately.
|
// return as is and evaluate the version separately.
|
||||||
return contents.trim();
|
nodeVersion = contents.trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodeVersion as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isLatestSyntax(versionSpec): boolean {
|
function isLatestSyntax(versionSpec): boolean {
|
||||||
|
|
Loading…
Reference in New Issue