mirror of
https://github.com/actions/setup-node.git
synced 2025-06-29 22:23:47 +00:00
Add support for node version codename
Refactored for optimization and maintainability
This commit is contained in:
parent
7a3ce83626
commit
62a25ae2c2
10 changed files with 507 additions and 401 deletions
|
@ -7,11 +7,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
const fs = __importStar(require("fs"));
|
||||
const os = __importStar(require("os"));
|
||||
const path = __importStar(require("path"));
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const github = __importStar(require("@actions/github"));
|
||||
function configAuthentication(registryUrl, alwaysAuth) {
|
||||
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
|
||||
if (!registryUrl.endsWith('/')) {
|
||||
|
|
230
lib/installer.js
230
lib/installer.js
|
@ -15,170 +15,141 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
// Load tempDirectory before it gets wiped by tool-cache
|
||||
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const io = __importStar(require("@actions/io"));
|
||||
const tc = __importStar(require("@actions/tool-cache"));
|
||||
const restm = __importStar(require("typed-rest-client/RestClient"));
|
||||
const os = __importStar(require("os"));
|
||||
const path = __importStar(require("path"));
|
||||
const semver = __importStar(require("semver"));
|
||||
const restm = __importStar(require("typed-rest-client/RestClient"));
|
||||
let osPlat = os.platform();
|
||||
let osArch = os.arch();
|
||||
if (!tempDirectory) {
|
||||
let baseLocation;
|
||||
if (process.platform === 'win32') {
|
||||
// On windows use the USERPROFILE env variable
|
||||
baseLocation = process.env['USERPROFILE'] || 'C:\\';
|
||||
}
|
||||
else {
|
||||
if (process.platform === 'darwin') {
|
||||
baseLocation = '/Users';
|
||||
}
|
||||
else {
|
||||
baseLocation = '/home';
|
||||
}
|
||||
}
|
||||
tempDirectory = path.join(baseLocation, 'actions', 'temp');
|
||||
}
|
||||
const IS_WINDOWS = osPlat === 'win32';
|
||||
function getNode(versionSpec) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// check cache
|
||||
let toolPath;
|
||||
toolPath = tc.find('node', versionSpec);
|
||||
// If not found in cache, download
|
||||
if (!toolPath) {
|
||||
let version;
|
||||
const c = semver.clean(versionSpec) || '';
|
||||
// If explicit version
|
||||
if (semver.valid(c) != null) {
|
||||
// version to download
|
||||
version = versionSpec;
|
||||
}
|
||||
else {
|
||||
// query nodejs.org for a matching version
|
||||
version = yield queryLatestMatch(versionSpec);
|
||||
if (!version) {
|
||||
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
|
||||
}
|
||||
// check cache
|
||||
toolPath = tc.find('node', version);
|
||||
}
|
||||
if (!toolPath) {
|
||||
// download, extract, cache
|
||||
toolPath = yield acquireNode(version);
|
||||
}
|
||||
versionSpec = versionSpec.trim();
|
||||
// resolve node codenames
|
||||
let version = yield resolve(versionSpec);
|
||||
if (!version) {
|
||||
throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`);
|
||||
}
|
||||
// check cache
|
||||
let toolPath = tc.find('node', version, osArch);
|
||||
// Not found in cache -> download
|
||||
if (!toolPath) {
|
||||
// download, extract, cache
|
||||
toolPath = yield acquireNode(version);
|
||||
}
|
||||
//
|
||||
// a tool installer initimately knows details about the layout of that tool
|
||||
// for example, node binary is in the bin folder after the extract on Mac/Linux.
|
||||
// layouts could change by version, by platform etc... but that's the tool installers job
|
||||
//
|
||||
if (osPlat != 'win32') {
|
||||
if (!IS_WINDOWS) {
|
||||
toolPath = path.join(toolPath, 'bin');
|
||||
}
|
||||
//
|
||||
// prepend the tools path. instructs the agent to prepend for future tasks
|
||||
core.addPath(toolPath);
|
||||
});
|
||||
}
|
||||
exports.getNode = getNode;
|
||||
function queryLatestMatch(versionSpec) {
|
||||
function resolve(versionSpec) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
let version = semver.clean(versionSpec) || '';
|
||||
return semver.valid(version) || tc.find('node', versionSpec, osArch)
|
||||
? version || versionSpec
|
||||
: queryNodeVersions(versionSpec);
|
||||
});
|
||||
}
|
||||
function queryNodeVersions(versionSpec) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
core.debug(`querying Node.js for ${versionSpec}`);
|
||||
// node offers a json list of versions
|
||||
let dataUrl = 'https://nodejs.org/dist/index.json';
|
||||
let rest = new restm.RestClient('setup-node');
|
||||
let nodeVersions = (yield rest.get(dataUrl)).result || [];
|
||||
let dataFileName;
|
||||
switch (osPlat) {
|
||||
case 'linux':
|
||||
dataFileName = 'linux-' + osArch;
|
||||
dataFileName = `linux-${osArch}`;
|
||||
break;
|
||||
case 'darwin':
|
||||
dataFileName = 'osx-' + osArch + '-tar';
|
||||
dataFileName = `osx-${osArch}-tar`;
|
||||
break;
|
||||
case 'win32':
|
||||
dataFileName = 'win-' + osArch + '-exe';
|
||||
dataFileName = `win-${osArch}-7z`;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unexpected OS '${osPlat}'`);
|
||||
}
|
||||
let versions = [];
|
||||
let dataUrl = 'https://nodejs.org/dist/index.json';
|
||||
let rest = new restm.RestClient('setup-node');
|
||||
let nodeVersions = (yield rest.get(dataUrl)).result || [];
|
||||
nodeVersions.forEach((nodeVersion) => {
|
||||
// ensure this version supports your os and platform
|
||||
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
|
||||
versions.push(nodeVersion.version);
|
||||
}
|
||||
});
|
||||
// ensure this version supports your os and platform
|
||||
nodeVersions = nodeVersions.filter((nodeVersion) => nodeVersion.files.indexOf(dataFileName) > -1);
|
||||
// sort node versions by descending version
|
||||
nodeVersions = nodeVersions.sort((a, b) => semver.gt(b.version, a.version) ? 1 : -1);
|
||||
const isLatestSpec = /^latest|current$/i.test(versionSpec);
|
||||
const isLTSSpec = /^lts$/i.test(versionSpec);
|
||||
const isLTSCodenameSpec = !isLatestSpec && !isLTSSpec && /^[a-zA-Z]+$/.test(versionSpec);
|
||||
const findNodeVersion = (predicator) => {
|
||||
nodeVersions = nodeVersions.filter(predicator);
|
||||
return nodeVersions.length
|
||||
? semver.clean(nodeVersions[0].version) || ''
|
||||
: '';
|
||||
};
|
||||
// resolve latest or current node version
|
||||
if (isLatestSpec) {
|
||||
return findNodeVersion((nodeVersion) => typeof nodeVersion.lts !== 'string');
|
||||
}
|
||||
// resolve lts node version
|
||||
if (isLTSSpec) {
|
||||
return findNodeVersion((nodeVersion) => typeof nodeVersion.lts === 'string');
|
||||
}
|
||||
// resolve node version codename
|
||||
if (isLTSCodenameSpec) {
|
||||
return findNodeVersion((nodeVersion) => typeof nodeVersion.lts === 'string' &&
|
||||
nodeVersion.lts.toLowerCase() === versionSpec.toLowerCase());
|
||||
}
|
||||
// get the latest version that matches the version spec
|
||||
let version = evaluateVersions(versions, versionSpec);
|
||||
return version;
|
||||
return evaluateVersions(nodeVersions, versionSpec);
|
||||
});
|
||||
}
|
||||
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
|
||||
function evaluateVersions(versions, versionSpec) {
|
||||
let version = '';
|
||||
core.debug(`evaluating ${versions.length} versions`);
|
||||
versions = versions.sort((a, b) => {
|
||||
if (semver.gt(a, b)) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
for (let i = versions.length - 1; i >= 0; i--) {
|
||||
const potential = versions[i];
|
||||
const satisfied = semver.satisfies(potential, versionSpec);
|
||||
if (satisfied) {
|
||||
version = potential;
|
||||
break;
|
||||
}
|
||||
}
|
||||
function evaluateVersions(nodeVersions, versionSpec) {
|
||||
core.debug(`evaluating ${nodeVersions.length} versions`);
|
||||
const versions = nodeVersions.map((nodeVersion) => nodeVersion.version);
|
||||
const version = versions.find((potential) => semver.satisfies(potential, versionSpec)) || '';
|
||||
if (version) {
|
||||
core.debug(`matched: ${version}`);
|
||||
}
|
||||
else {
|
||||
core.debug('match not found');
|
||||
}
|
||||
return version;
|
||||
return semver.clean(version) || '';
|
||||
}
|
||||
function acquireNode(version) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
//
|
||||
// Download - a tool installer intimately knows how to get the tool (and construct urls)
|
||||
//
|
||||
version = semver.clean(version) || '';
|
||||
let fileName = osPlat == 'win32'
|
||||
? 'node-v' + version + '-win-' + os.arch()
|
||||
: 'node-v' + version + '-' + osPlat + '-' + os.arch();
|
||||
let urlFileName = osPlat == 'win32' ? fileName + '.7z' : fileName + '.tar.gz';
|
||||
let downloadUrl = 'https://nodejs.org/dist/v' + version + '/' + urlFileName;
|
||||
const fileName = `node-v${version}-${IS_WINDOWS ? 'win' : osPlat}-${osArch}`;
|
||||
const urlFileName = `${fileName}.${IS_WINDOWS ? '7z' : 'tar.gz'}`;
|
||||
const downloadUrl = `https://nodejs.org/dist/v${version}/${urlFileName}`;
|
||||
let downloadPath;
|
||||
try {
|
||||
downloadPath = yield tc.downloadTool(downloadUrl);
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
|
||||
return yield acquireNodeFromFallbackLocation(version);
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode === 404) {
|
||||
if (IS_WINDOWS) {
|
||||
return acquireNodeFromFallbackLocation(version);
|
||||
}
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
//
|
||||
// Extract
|
||||
//
|
||||
let extPath;
|
||||
if (osPlat == 'win32') {
|
||||
let _7zPath = path.join(__dirname, '..', 'externals', '7zr.exe');
|
||||
extPath = yield tc.extract7z(downloadPath, undefined, _7zPath);
|
||||
}
|
||||
else {
|
||||
extPath = yield tc.extractTar(downloadPath);
|
||||
}
|
||||
//
|
||||
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
|
||||
//
|
||||
let toolRoot = path.join(extPath, fileName);
|
||||
return yield tc.cacheDir(toolRoot, 'node', version);
|
||||
const _7zPath = path.join(__dirname, '..', 'externals', '7zr.exe');
|
||||
const extPath = IS_WINDOWS
|
||||
? yield tc.extract7z(downloadPath, undefined, _7zPath)
|
||||
: yield tc.extractTar(downloadPath);
|
||||
// Install into the local tool cache
|
||||
// node extracts with a root folder that matches the fileName downloaded
|
||||
const toolRoot = path.join(extPath, fileName);
|
||||
return tc.cacheDir(toolRoot, 'node', version, osArch);
|
||||
});
|
||||
}
|
||||
// For non LTS versions of Node, the files we need (for Windows) are sometimes located
|
||||
|
@ -196,32 +167,39 @@ function acquireNode(version) {
|
|||
function acquireNodeFromFallbackLocation(version) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Create temporary folder to download in to
|
||||
let tempDownloadFolder = 'temp_' + Math.floor(Math.random() * 2000000000);
|
||||
let tempDir = path.join(tempDirectory, tempDownloadFolder);
|
||||
yield io.mkdirP(tempDir);
|
||||
let exeUrl;
|
||||
let libUrl;
|
||||
const tempDownloadFolder = `temp_${Math.floor(Math.random() * 2000000000)}`;
|
||||
const tempDir = path.join(getTempDirectory(), tempDownloadFolder);
|
||||
const baseUrl = `https://nodejs.org/dist/v${version}/`;
|
||||
const tryDownload = (url) => __awaiter(this, void 0, void 0, function* () {
|
||||
const exeFileName = 'node.exe';
|
||||
const libFileName = 'node.lib';
|
||||
const exePath = yield tc.downloadTool(`${url}${exeFileName}`);
|
||||
yield io.cp(exePath, path.join(tempDir, exeFileName));
|
||||
const libPath = yield tc.downloadTool(`${url}${libFileName}`);
|
||||
yield io.cp(libPath, path.join(tempDir, libFileName));
|
||||
});
|
||||
try {
|
||||
exeUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.exe`;
|
||||
libUrl = `https://nodejs.org/dist/v${version}/win-${os.arch()}/node.lib`;
|
||||
const exePath = yield tc.downloadTool(exeUrl);
|
||||
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
|
||||
const libPath = yield tc.downloadTool(libUrl);
|
||||
yield io.cp(libPath, path.join(tempDir, 'node.lib'));
|
||||
yield io.mkdirP(tempDir);
|
||||
yield tryDownload(`${baseUrl}win-${osArch}/`);
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
|
||||
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`;
|
||||
libUrl = `https://nodejs.org/dist/v${version}/node.lib`;
|
||||
const exePath = yield tc.downloadTool(exeUrl);
|
||||
yield io.cp(exePath, path.join(tempDir, 'node.exe'));
|
||||
const libPath = yield tc.downloadTool(libUrl);
|
||||
yield io.cp(libPath, path.join(tempDir, 'node.lib'));
|
||||
if (err instanceof tc.HTTPError && err.httpStatusCode === 404) {
|
||||
yield tryDownload(baseUrl);
|
||||
}
|
||||
else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return yield tc.cacheDir(tempDir, 'node', version);
|
||||
return tc.cacheDir(tempDir, 'node', version, osArch);
|
||||
});
|
||||
}
|
||||
function getTempDirectory() {
|
||||
const baseLocation =
|
||||
// On windows use the USERPROFILE env variable
|
||||
process.platform === 'win32'
|
||||
? process.env['USERPROFILE'] || 'C:\\'
|
||||
: process.platform === 'darwin'
|
||||
? '/Users'
|
||||
: '/home';
|
||||
return (process.env['RUNNER_TEMP'] || path.join(baseLocation, 'actions', 'temp'));
|
||||
}
|
||||
|
|
|
@ -16,20 +16,16 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
const installer = __importStar(require("./installer"));
|
||||
const auth = __importStar(require("./authutil"));
|
||||
const path = __importStar(require("path"));
|
||||
const auth = __importStar(require("./authutil"));
|
||||
const installer = __importStar(require("./installer"));
|
||||
function run() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
//
|
||||
// Version is optional. If supplied, install / use from the tool cache
|
||||
// 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('version');
|
||||
if (!version) {
|
||||
version = core.getInput('node-version');
|
||||
}
|
||||
const version = core.getInput('version') || core.getInput('node-version');
|
||||
// allow user to not specify a node version
|
||||
if (version) {
|
||||
// TODO: installer doesn't support proxy
|
||||
yield installer.getNode(version);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue