Adding Node.js version file support (#338)

This commit is contained in:
Hargun Kaur 2021-11-29 14:48:31 +05:30 committed by GitHub
parent 360ab8b75b
commit d08cf22211
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 221 additions and 23 deletions

View file

@ -1,5 +1,6 @@
import * as core from '@actions/core';
import * as installer from './installer';
import fs from 'fs';
import * as auth from './authutil';
import * as path from 'path';
import {restoreCache} from './cache-restore';
@ -12,10 +13,7 @@ export async function run() {
// Version is optional. If supplied, install / use from the tool cache
// If not supplied then task is still used to setup proxy, auth, etc...
//
let version = core.getInput('node-version');
if (!version) {
version = core.getInput('version');
}
let version = resolveVersionInput();
let arch = core.getInput('architecture');
const cache = core.getInput('cache');
@ -63,8 +61,8 @@ export async function run() {
core.info(
`##[add-matcher]${path.join(matchersPath, 'eslint-compact.json')}`
);
} catch (error) {
core.setFailed(error.message);
} catch (err) {
core.setFailed(err.message);
}
}
@ -74,3 +72,36 @@ function isGhes(): boolean {
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
function resolveVersionInput(): string {
let version = core.getInput('node-version') || core.getInput('version');
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'
);
}
if (version) {
return version;
}
if (versionFileInput) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFileInput
);
if (!fs.existsSync(versionFilePath)) {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`
);
}
version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);
core.info(`Resolved ${versionFileInput} as ${version}`);
}
return version;
}