Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add volta as node-version-file #532

Merged
merged 21 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move volta logic
  • Loading branch information
jef committed Jun 29, 2022
commit dbfbe9b6dab362ff4d3dacc32758182a21c603bf
24 changes: 20 additions & 4 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -572,6 +572,7 @@ describe('setup-node', () => {
existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile)
);

// Act
await main.run();

Expand All @@ -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}`
);
Expand Down
18 changes: 9 additions & 9 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
}
Expand All @@ -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'));
}
version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;
Expand Down
8 changes: 7 additions & 1 deletion src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 4 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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')
);
}
version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);

core.info(`Resolved ${versionFileInput} as ${version}`);
}
Expand Down