merge changes from mmarchini/setup-node

This commit is contained in:
Anthony Whalley 2021-11-14 22:19:14 +00:00
parent 360ab8b75b
commit feb239db59
3 changed files with 5157 additions and 26 deletions

5116
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -14,6 +14,7 @@ import fs = require('fs');
// //
export interface INodeVersion { export interface INodeVersion {
version: string; version: string;
date: string;
files: string[]; files: string[];
} }
@ -33,7 +34,8 @@ export async function getNode(
stable: boolean, stable: boolean,
checkLatest: boolean, checkLatest: boolean,
auth: string | undefined, auth: string | undefined,
arch: string = os.arch() arch: string = os.arch(),
mirror: string
) { ) {
// Store manifest data to avoid multiple calls // Store manifest data to avoid multiple calls
let manifest: INodeRelease[] | undefined; let manifest: INodeRelease[] | undefined;
@ -119,7 +121,7 @@ export async function getNode(
// Download from nodejs.org // Download from nodejs.org
// //
if (!downloadPath) { if (!downloadPath) {
info = await getInfoFromDist(versionSpec, arch); info = await getInfoFromDist(versionSpec, arch, mirror);
if (!info) { if (!info) {
throw new Error( throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.` `Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
@ -135,7 +137,8 @@ export async function getNode(
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) { if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await acquireNodeFromFallbackLocation( return await acquireNodeFromFallbackLocation(
info.resolvedVersion, info.resolvedVersion,
info.arch info.arch,
mirror
); );
} }
@ -265,14 +268,15 @@ async function getInfoFromManifest(
async function getInfoFromDist( async function getInfoFromDist(
versionSpec: string, versionSpec: string,
arch: string = os.arch() arch: string = os.arch(),
mirror: string
): Promise<INodeVersionInfo | null> { ): Promise<INodeVersionInfo | null> {
let osPlat: string = os.platform(); let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(arch); let osArch: string = translateArchToDistUrl(arch);
let version: string; let version: string;
version = await queryDistForMatch(versionSpec, arch); version = await queryDistForMatch(versionSpec, arch, mirror);
if (!version) { if (!version) {
return null; return null;
} }
@ -287,7 +291,7 @@ async function getInfoFromDist(
: `node-v${version}-${osPlat}-${osArch}`; : `node-v${version}-${osPlat}-${osArch}`;
let urlFileName: string = let urlFileName: string =
osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`; osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
let url = `https://nodejs.org/dist/v${version}/${urlFileName}`; let url = `${mirror}/v${version}/${urlFileName}`;
return <INodeVersionInfo>{ return <INodeVersionInfo>{
downloadUrl: url, downloadUrl: url,
@ -320,17 +324,29 @@ async function resolveVersionFromManifest(
} }
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there // TODO - should we just export this from @actions/tool-cache? Lifted directly from there
function evaluateVersions(versions: string[], versionSpec: string): string { function evaluateVersions(versions: INodeVersion[], versionSpec: string): string {
let version = ''; let version = '';
core.debug(`evaluating ${versions.length} versions`); core.debug(`evaluating ${versions.length} versions`);
versions = versions.sort((a, b) => { versions = versions.sort((a, b) => {
if (semver.gt(a, b)) { const versionA = semver.coerce(a.version);
const versionB = semver.coerce(b.version);
// If versions are equal, compare date instead
if (versionA === versionB || versionA === null || versionB === null) {
if (new Date(a.date) > new Date(b.date)) {
return 1;
}
return -1;
}
if (semver.gt(versionA, versionB)) {
return 1; return 1;
} }
return -1; return -1;
}); });
for (let i = versions.length - 1; i >= 0; i--) { for (let i = versions.length - 1; i >= 0; i--) {
const potential: string = versions[i]; const potential: string = versions[i].version;
const semverPotential = semver.coerce(potential);
if (semverPotential === null)
continue;
const satisfied: boolean = semver.satisfies(potential, versionSpec); const satisfied: boolean = semver.satisfies(potential, versionSpec);
if (satisfied) { if (satisfied) {
version = potential; version = potential;
@ -347,9 +363,10 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
return version; return version;
} }
async function queryDistForMatch( export async function queryDistForMatch(
versionSpec: string, versionSpec: string,
arch: string = os.arch() arch: string = os.arch(),
mirror: string
): Promise<string> { ): Promise<string> {
let osPlat: string = os.platform(); let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(arch); let osArch: string = translateArchToDistUrl(arch);
@ -370,13 +387,13 @@ async function queryDistForMatch(
throw new Error(`Unexpected OS '${osPlat}'`); throw new Error(`Unexpected OS '${osPlat}'`);
} }
let versions: string[] = []; let versions: INodeVersion[] = [];
let nodeVersions = await module.exports.getVersionsFromDist(); let nodeVersions = await module.exports.getVersionsFromDist(mirror);
nodeVersions.forEach((nodeVersion: INodeVersion) => { nodeVersions.forEach((nodeVersion: INodeVersion) => {
// ensure this version supports your os and platform // ensure this version supports your os and platform
if (nodeVersion.files.indexOf(dataFileName) >= 0) { if (nodeVersion.files.indexOf(dataFileName) >= 0) {
versions.push(nodeVersion.version); versions.push(nodeVersion);
} }
}); });
@ -385,8 +402,8 @@ async function queryDistForMatch(
return version; return version;
} }
export async function getVersionsFromDist(): Promise<INodeVersion[]> { export async function getVersionsFromDist(mirror: string): Promise<INodeVersion[]> {
let dataUrl = 'https://nodejs.org/dist/index.json'; let dataUrl = `${mirror}/index.json`;
let httpClient = new hc.HttpClient('setup-node', [], { let httpClient = new hc.HttpClient('setup-node', [], {
allowRetries: true, allowRetries: true,
maxRetries: 3 maxRetries: 3
@ -409,7 +426,8 @@ export async function getVersionsFromDist(): Promise<INodeVersion[]> {
// and lib file in a folder, not zipped. // and lib file in a folder, not zipped.
async function acquireNodeFromFallbackLocation( async function acquireNodeFromFallbackLocation(
version: string, version: string,
arch: string = os.arch() arch: string = os.arch(),
mirror: string
): Promise<string> { ): Promise<string> {
let osPlat: string = os.platform(); let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(arch); let osArch: string = translateArchToDistUrl(arch);
@ -424,8 +442,8 @@ async function acquireNodeFromFallbackLocation(
let exeUrl: string; let exeUrl: string;
let libUrl: string; let libUrl: string;
try { try {
exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`; exeUrl = `${mirror}/v${version}/win-${osArch}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`; libUrl = `${mirror}/v${version}/win-${osArch}/node.lib`;
core.info(`Downloading only node binary from ${exeUrl}`); core.info(`Downloading only node binary from ${exeUrl}`);
@ -435,8 +453,8 @@ async function acquireNodeFromFallbackLocation(
await io.cp(libPath, path.join(tempDir, 'node.lib')); await io.cp(libPath, path.join(tempDir, 'node.lib'));
} catch (err) { } catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) { if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
exeUrl = `https://nodejs.org/dist/v${version}/node.exe`; exeUrl = `${mirror}/v${version}/node.exe`;
libUrl = `https://nodejs.org/dist/v${version}/node.lib`; libUrl = `${mirror}/v${version}/node.lib`;
const exePath = await tc.downloadTool(exeUrl); const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe')); await io.cp(exePath, path.join(tempDir, 'node.exe'));

View file

@ -31,14 +31,17 @@ export async function run() {
if (!arch) { if (!arch) {
arch = os.arch(); arch = os.arch();
} }
let mirror = core.getInput('mirror');
if (!mirror) {
mirror = core.getInput('node-mirror') || 'https://nodejs.org/dist';
}
if (version) { if (version) {
let token = core.getInput('token'); let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`; let auth = !token || isGhes() ? undefined : `token ${token}`;
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
const checkLatest = const checkLatest =
(core.getInput('check-latest') || 'false').toUpperCase() === 'TRUE'; (core.getInput('check-latest') || 'false').toUpperCase() === 'TRUE';
await installer.getNode(version, stable, checkLatest, auth, arch); await installer.getNode(version, stable, checkLatest, auth, arch, mirror);
} }
const registryUrl: string = core.getInput('registry-url'); const registryUrl: string = core.getInput('registry-url');