Skip to content

Commit f89df17

Browse files
committed
Support .node-version and .nvmrc by default; closes actions#683
1 parent 3dbcda8 commit f89df17

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

__tests__/data/.node-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v16

__tests__/main.test.ts

+47-8
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,6 @@ describe('main tests', () => {
155155
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
156156
}, 10000);
157157

158-
it('not used if node-version-file not provided', async () => {
159-
// Act
160-
await main.run();
161-
162-
// Assert
163-
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
164-
});
165-
166158
it('reads node-version-file if provided', async () => {
167159
// Arrange
168160
const versionSpec = 'v14';
@@ -215,6 +207,53 @@ describe('main tests', () => {
215207
);
216208
}, 10000);
217209

210+
it('reads .node-version if node-version and node-version-file were not provided', async () => {
211+
// Arrange
212+
const versionSpec = 'v16';
213+
const versionFile = '.node-version';
214+
const expectedVersionSpec = '16';
215+
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
216+
217+
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
218+
existsSpy.mockImplementationOnce(
219+
input => input === path.join(__dirname, 'data', versionFile)
220+
);
221+
222+
// Act
223+
await main.run();
224+
225+
// Assert
226+
expect(existsSpy).toHaveBeenCalledTimes(1);
227+
expect(existsSpy).toHaveReturnedWith(true);
228+
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
229+
expect(infoSpy).toHaveBeenCalledWith(
230+
`Resolved ${versionFile} as ${expectedVersionSpec}`
231+
);
232+
}, 10000);
233+
234+
it('reads .nvmrc if node-version and node-version-file were not provided', async () => {
235+
// Arrange
236+
const versionSpec = 'v14';
237+
const versionFile = '.nvmrc';
238+
const expectedVersionSpec = '14';
239+
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
240+
241+
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
242+
existsSpy.mockImplementation(
243+
input => input === path.join(__dirname, 'data', versionFile)
244+
);
245+
246+
// Act
247+
await main.run();
248+
249+
// Assert
250+
expect(existsSpy).toHaveBeenCalledTimes(2);
251+
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
252+
expect(infoSpy).toHaveBeenCalledWith(
253+
`Resolved ${versionFile} as ${expectedVersionSpec}`
254+
);
255+
}, 10000);
256+
218257
it('both node-version-file and node-version are provided', async () => {
219258
inputs['node-version'] = '12';
220259
const versionSpec = 'v14';

dist/setup/index.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -74003,11 +74003,22 @@ function resolveVersionInput() {
7400374003
}
7400474004
if (versionFileInput) {
7400574005
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput);
74006-
if (!fs_1.default.existsSync(versionFilePath)) {
74006+
if (fs_1.default.existsSync(versionFilePath)) {
74007+
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
74008+
core.info(`Resolved ${versionFileInput} as ${version}`);
74009+
return version;
74010+
}
74011+
else {
7400774012
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
7400874013
}
74009-
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
74010-
core.info(`Resolved ${versionFileInput} as ${version}`);
74014+
}
74015+
for (const versionFile of ['.node-version', '.nvmrc']) {
74016+
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile);
74017+
if (fs_1.default.existsSync(versionFilePath)) {
74018+
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
74019+
core.info(`Resolved ${versionFile} as ${version}`);
74020+
return version;
74021+
}
7401174022
}
7401274023
return version;
7401374024
}

src/main.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,32 @@ function resolveVersionInput(): string {
9797
versionFileInput
9898
);
9999

100-
if (!fs.existsSync(versionFilePath)) {
100+
if (fs.existsSync(versionFilePath)) {
101+
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
102+
103+
core.info(`Resolved ${versionFileInput} as ${version}`);
104+
105+
return version;
106+
} else {
101107
throw new Error(
102108
`The specified node version file at: ${versionFilePath} does not exist`
103109
);
104110
}
111+
}
105112

106-
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
113+
for (const versionFile of ['.node-version', '.nvmrc']) {
114+
const versionFilePath = path.join(
115+
process.env.GITHUB_WORKSPACE!,
116+
versionFile
117+
);
118+
119+
if (fs.existsSync(versionFilePath)) {
120+
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
107121

108-
core.info(`Resolved ${versionFileInput} as ${version}`);
122+
core.info(`Resolved ${versionFile} as ${version}`);
123+
124+
return version;
125+
}
109126
}
110127

111128
return version;

0 commit comments

Comments
 (0)