refactor: move volta logic

This commit is contained in:
Jef LeCompte 2022-06-29 15:37:53 -07:00
parent 9aa86428fe
commit dbfbe9b6da
No known key found for this signature in database
GPG Key ID: 7F5BA322B5DC170C
4 changed files with 40 additions and 24 deletions

View File

@ -560,7 +560,7 @@ describe('setup-node', () => {
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
});
it('reads node-version-file if provided', async () => {
it('reads node-version-file if provided (.nvmrc)', async () => {
// Arrange
const versionSpec = 'v14';
const versionFile = '.nvmrc';
@ -572,6 +572,7 @@ describe('setup-node', () => {
existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile)
);
// Act
await main.run();
@ -584,22 +585,37 @@ describe('setup-node', () => {
);
});
it('reads node-version-file if provided with volta', async () => {
it('reads node-version-file if provided (package.json, volta)', async () => {
// Arrange
const expectedVersionSpec = '16.15.1';
const versionSpec = `{
"name": "test",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "echo test"
},
"volta": {
"node": "16.15.1"
}
}
`;
const versionFile = 'package.json';
const expectedVersionSpec = '16.15.1';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
inputs['node-version-file'] = 'volta';
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}`
);

16
dist/setup/index.js vendored
View File

@ -71768,7 +71768,13 @@ function translateArchToDistUrl(arch) {
}
}
function parseNodeVersionFile(contents) {
let nodeVersion = contents.trim();
let nodeVersion;
if (contents.includes('volta')) {
nodeVersion = JSON.parse(contents).volta.node;
}
else {
nodeVersion = contents.trim();
}
if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1);
}
@ -71862,8 +71868,7 @@ function run() {
exports.run = run;
function resolveVersionInput() {
let version = core.getInput('node-version');
const nodeVersionFile = core.getInput('node-version-file');
const versionFileInput = nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile;
const versionFileInput = core.getInput('node-version-file');
if (version && versionFileInput) {
core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used');
}
@ -71875,12 +71880,7 @@ function resolveVersionInput() {
if (!fs_1.default.existsSync(versionFilePath)) {
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
}
if (nodeVersionFile === 'volta') {
version = JSON.parse(fs_1.default.readFileSync(versionFilePath, 'utf8')).volta.node;
}
else {
version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
}
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;

View File

@ -495,7 +495,13 @@ function translateArchToDistUrl(arch: string): string {
}
export function parseNodeVersionFile(contents: string): string {
let nodeVersion = contents.trim();
let nodeVersion;
if (contents.includes('volta')) {
nodeVersion = JSON.parse(contents).volta.node;
} else {
nodeVersion = contents.trim();
}
if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1);

View File

@ -65,9 +65,7 @@ export async function run() {
function resolveVersionInput(): string {
let version = core.getInput('node-version');
const nodeVersionFile = core.getInput('node-version-file');
const versionFileInput =
nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile;
const versionFileInput = core.getInput('node-version-file');
if (version && versionFileInput) {
core.warning(
@ -91,13 +89,9 @@ function resolveVersionInput(): string {
);
}
if (nodeVersionFile === 'volta') {
version = JSON.parse(fs.readFileSync(versionFilePath, 'utf8')).volta.node;
} else {
version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);
}
core.info(`Resolved ${versionFileInput} as ${version}`);
}