2020-05-19 13:25:54 +00:00
|
|
|
import os = require('os');
|
2020-01-27 15:37:12 +00:00
|
|
|
import * as assert from 'assert';
|
2019-08-04 01:49:54 +00:00
|
|
|
import * as core from '@actions/core';
|
2020-01-27 15:37:12 +00:00
|
|
|
import * as hc from '@actions/http-client';
|
2019-08-04 01:49:54 +00:00
|
|
|
import * as io from '@actions/io';
|
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as semver from 'semver';
|
2020-05-19 13:25:54 +00:00
|
|
|
import fs = require('fs');
|
2019-08-04 01:49:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Node versions interface
|
|
|
|
// see https://nodejs.org/dist/index.json
|
|
|
|
//
|
2020-05-19 13:25:54 +00:00
|
|
|
export interface INodeVersion {
|
2019-08-04 01:49:54 +00:00
|
|
|
version: string;
|
|
|
|
files: string[];
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
interface INodeVersionInfo {
|
|
|
|
downloadUrl: string;
|
|
|
|
resolvedVersion: string;
|
2020-09-03 12:29:40 +00:00
|
|
|
arch: string;
|
2020-05-19 13:25:54 +00:00
|
|
|
fileName: string;
|
|
|
|
}
|
|
|
|
|
2021-06-21 09:37:50 +00:00
|
|
|
interface INodeRelease extends tc.IToolRelease {
|
|
|
|
lts?: string;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
export async function getNode(
|
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean,
|
2020-06-29 18:56:37 +00:00
|
|
|
checkLatest: boolean,
|
2020-09-03 12:19:43 +00:00
|
|
|
auth: string | undefined,
|
|
|
|
arch: string = os.arch()
|
2020-05-19 13:25:54 +00:00
|
|
|
) {
|
2021-06-22 13:45:25 +00:00
|
|
|
// Store manifest data to avoid multiple calls
|
|
|
|
let manifest: INodeRelease[] | undefined;
|
2020-05-19 13:25:54 +00:00
|
|
|
let osPlat: string = os.platform();
|
2020-09-03 12:19:43 +00:00
|
|
|
let osArch: string = translateArchToDistUrl(arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
|
2021-06-22 11:10:16 +00:00
|
|
|
if (isLtsAlias(versionSpec)) {
|
2021-06-22 13:45:25 +00:00
|
|
|
core.info('Attempt to resolve LTS alias from manifest...');
|
2021-06-22 15:52:06 +00:00
|
|
|
|
2021-06-22 13:45:25 +00:00
|
|
|
// No try-catch since it's not possible to resolve LTS alias without manifest
|
2021-06-22 15:52:06 +00:00
|
|
|
manifest = await getManifest(auth);
|
|
|
|
|
2021-06-22 13:45:25 +00:00
|
|
|
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
|
2021-06-17 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 18:56:37 +00:00
|
|
|
if (checkLatest) {
|
|
|
|
core.info('Attempt to resolve the latest version from manifest...');
|
|
|
|
const resolvedVersion = await resolveVersionFromManifest(
|
|
|
|
versionSpec,
|
|
|
|
stable,
|
2020-09-03 12:35:58 +00:00
|
|
|
auth,
|
2021-06-22 13:45:25 +00:00
|
|
|
osArch,
|
|
|
|
manifest
|
2020-06-29 18:56:37 +00:00
|
|
|
);
|
|
|
|
if (resolvedVersion) {
|
|
|
|
versionSpec = resolvedVersion;
|
|
|
|
core.info(`Resolved as '${versionSpec}'`);
|
|
|
|
} else {
|
|
|
|
core.info(`Failed to resolve version ${versionSpec} from manifest`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 01:49:54 +00:00
|
|
|
// check cache
|
|
|
|
let toolPath: string;
|
2020-09-03 12:20:04 +00:00
|
|
|
toolPath = tc.find('node', versionSpec, osArch);
|
2019-08-04 01:49:54 +00:00
|
|
|
|
|
|
|
// If not found in cache, download
|
2021-08-04 14:00:35 +00:00
|
|
|
if (toolPath) {
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(`Found in cache @ ${toolPath}`);
|
2020-05-19 13:25:54 +00:00
|
|
|
} else {
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(`Attempting to download ${versionSpec}...`);
|
2020-05-19 13:25:54 +00:00
|
|
|
let downloadPath = '';
|
|
|
|
let info: INodeVersionInfo | null = null;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Try download from internal distribution (popular versions only)
|
|
|
|
//
|
|
|
|
try {
|
2021-06-22 14:11:44 +00:00
|
|
|
info = await getInfoFromManifest(
|
|
|
|
versionSpec,
|
|
|
|
stable,
|
|
|
|
auth,
|
|
|
|
osArch,
|
|
|
|
manifest
|
|
|
|
);
|
2020-05-19 13:25:54 +00:00
|
|
|
if (info) {
|
2020-09-03 12:50:29 +00:00
|
|
|
core.info(
|
|
|
|
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
|
|
|
|
);
|
2020-05-19 13:25:54 +00:00
|
|
|
downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
|
|
|
|
} else {
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(
|
2020-05-19 13:25:54 +00:00
|
|
|
'Not found in manifest. Falling back to download directly from Node'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Rate limit?
|
|
|
|
if (
|
|
|
|
err instanceof tc.HTTPError &&
|
|
|
|
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
|
|
|
|
) {
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(
|
2020-05-19 13:25:54 +00:00
|
|
|
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
|
|
|
|
);
|
|
|
|
} else {
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(err.message);
|
2020-05-19 13:25:54 +00:00
|
|
|
}
|
|
|
|
core.debug(err.stack);
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info('Falling back to download directly from Node');
|
2020-05-19 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Download from nodejs.org
|
|
|
|
//
|
|
|
|
if (!downloadPath) {
|
2020-09-03 12:54:48 +00:00
|
|
|
info = await getInfoFromDist(versionSpec, arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
if (!info) {
|
2019-08-04 01:49:54 +00:00
|
|
|
throw new Error(
|
|
|
|
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:50:29 +00:00
|
|
|
core.info(
|
|
|
|
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
|
|
|
|
);
|
2020-05-19 13:25:54 +00:00
|
|
|
try {
|
|
|
|
downloadPath = await tc.downloadTool(info.downloadUrl);
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
|
2020-09-03 12:24:52 +00:00
|
|
|
return await acquireNodeFromFallbackLocation(
|
|
|
|
info.resolvedVersion,
|
|
|
|
info.arch
|
|
|
|
);
|
2020-05-19 13:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
//
|
|
|
|
// Extract
|
|
|
|
//
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info('Extracting ...');
|
2020-05-19 13:25:54 +00:00
|
|
|
let extPath: string;
|
|
|
|
info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here
|
|
|
|
if (osPlat == 'win32') {
|
2021-06-16 06:52:44 +00:00
|
|
|
let _7zPath = path.join(__dirname, '../..', 'externals', '7zr.exe');
|
2020-05-19 13:25:54 +00:00
|
|
|
extPath = await tc.extract7z(downloadPath, undefined, _7zPath);
|
|
|
|
// 7z extracts to folder matching file name
|
|
|
|
let nestedPath = path.join(extPath, path.basename(info.fileName, '.7z'));
|
|
|
|
if (fs.existsSync(nestedPath)) {
|
|
|
|
extPath = nestedPath;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
extPath = await tc.extractTar(downloadPath, undefined, [
|
|
|
|
'xz',
|
|
|
|
'--strip',
|
|
|
|
'1'
|
|
|
|
]);
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
2020-05-19 13:25:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
|
|
|
|
//
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info('Adding to the cache ...');
|
2020-09-03 12:58:24 +00:00
|
|
|
toolPath = await tc.cacheDir(
|
|
|
|
extPath,
|
|
|
|
'node',
|
|
|
|
info.resolvedVersion,
|
|
|
|
info.arch
|
|
|
|
);
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info('Done');
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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') {
|
|
|
|
toolPath = path.join(toolPath, 'bin');
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// prepend the tools path. instructs the agent to prepend for future tasks
|
|
|
|
core.addPath(toolPath);
|
|
|
|
}
|
|
|
|
|
2021-06-22 11:10:16 +00:00
|
|
|
function isLtsAlias(versionSpec: string): boolean {
|
2021-06-30 07:34:42 +00:00
|
|
|
return versionSpec.startsWith('lts/');
|
2021-06-17 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 15:52:06 +00:00
|
|
|
function getManifest(auth: string | undefined): Promise<tc.IToolRelease[]> {
|
|
|
|
core.debug('Getting manifest from actions/node-versions@main');
|
|
|
|
return tc.getManifestFromRepo('actions', 'node-versions', auth, 'main');
|
|
|
|
}
|
|
|
|
|
2021-06-22 11:10:16 +00:00
|
|
|
function resolveLtsAliasFromManifest(
|
2021-06-17 15:51:02 +00:00
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean,
|
2021-06-22 11:10:16 +00:00
|
|
|
manifest: INodeRelease[]
|
2021-06-17 15:51:02 +00:00
|
|
|
): string {
|
|
|
|
const alias = versionSpec.split('lts/')[1]?.toLowerCase();
|
|
|
|
|
|
|
|
if (!alias) {
|
2021-06-22 14:11:44 +00:00
|
|
|
throw new Error(
|
2021-06-30 07:34:42 +00:00
|
|
|
`Unable to parse LTS alias for Node version '${versionSpec}'`
|
2021-06-22 14:11:44 +00:00
|
|
|
);
|
2021-06-17 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
|
|
|
|
|
2021-06-21 09:37:06 +00:00
|
|
|
// Supported formats are `lts/<alias>` and `lts/*`. Where asterisk means highest possible LTS.
|
2021-06-22 14:11:44 +00:00
|
|
|
const release =
|
|
|
|
alias === '*'
|
|
|
|
? manifest.find(x => !!x.lts && x.stable === stable)
|
|
|
|
: manifest.find(
|
|
|
|
x => x.lts?.toLowerCase() === alias && x.stable === stable
|
|
|
|
);
|
2021-06-17 15:51:02 +00:00
|
|
|
|
|
|
|
if (!release) {
|
2021-06-22 14:11:44 +00:00
|
|
|
throw new Error(
|
|
|
|
`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`
|
|
|
|
);
|
2021-06-17 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 14:11:44 +00:00
|
|
|
core.debug(
|
|
|
|
`Found LTS release '${release.version}' for Node version '${versionSpec}'`
|
|
|
|
);
|
2021-06-17 15:51:02 +00:00
|
|
|
|
|
|
|
return release.version.split('.')[0];
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
async function getInfoFromManifest(
|
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean,
|
2020-09-03 12:34:03 +00:00
|
|
|
auth: string | undefined,
|
2021-06-22 13:45:25 +00:00
|
|
|
osArch: string = translateArchToDistUrl(os.arch()),
|
|
|
|
manifest: tc.IToolRelease[] | undefined
|
2020-05-19 13:25:54 +00:00
|
|
|
): Promise<INodeVersionInfo | null> {
|
|
|
|
let info: INodeVersionInfo | null = null;
|
2021-06-22 13:45:25 +00:00
|
|
|
if (!manifest) {
|
2021-06-22 15:52:06 +00:00
|
|
|
core.debug('No manifest cached');
|
|
|
|
manifest = await getManifest(auth);
|
2021-06-17 15:51:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 13:45:25 +00:00
|
|
|
const rel = await tc.findFromManifest(versionSpec, stable, manifest, osArch);
|
2020-05-19 13:25:54 +00:00
|
|
|
|
|
|
|
if (rel && rel.files.length > 0) {
|
|
|
|
info = <INodeVersionInfo>{};
|
|
|
|
info.resolvedVersion = rel.version;
|
2020-09-03 12:34:03 +00:00
|
|
|
info.arch = rel.files[0].arch;
|
2020-05-19 13:25:54 +00:00
|
|
|
info.downloadUrl = rel.files[0].download_url;
|
|
|
|
info.fileName = rel.files[0].filename;
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
return info;
|
|
|
|
}
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
async function getInfoFromDist(
|
2020-09-03 12:54:48 +00:00
|
|
|
versionSpec: string,
|
|
|
|
arch: string = os.arch()
|
2020-05-19 13:25:54 +00:00
|
|
|
): Promise<INodeVersionInfo | null> {
|
|
|
|
let osPlat: string = os.platform();
|
2020-09-03 12:54:48 +00:00
|
|
|
let osArch: string = translateArchToDistUrl(arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
|
|
|
|
let version: string;
|
|
|
|
|
2020-09-03 13:49:37 +00:00
|
|
|
version = await queryDistForMatch(versionSpec, arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
if (!version) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Download - a tool installer intimately knows how to get the tool (and construct urls)
|
|
|
|
//
|
|
|
|
version = semver.clean(version) || '';
|
|
|
|
let fileName: string =
|
|
|
|
osPlat == 'win32'
|
|
|
|
? `node-v${version}-win-${osArch}`
|
|
|
|
: `node-v${version}-${osPlat}-${osArch}`;
|
|
|
|
let urlFileName: string =
|
|
|
|
osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
|
|
|
|
let url = `https://nodejs.org/dist/v${version}/${urlFileName}`;
|
|
|
|
|
|
|
|
return <INodeVersionInfo>{
|
|
|
|
downloadUrl: url,
|
|
|
|
resolvedVersion: version,
|
2020-09-05 11:57:59 +00:00
|
|
|
arch: arch,
|
2020-05-19 13:25:54 +00:00
|
|
|
fileName: fileName
|
|
|
|
};
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 18:56:37 +00:00
|
|
|
async function resolveVersionFromManifest(
|
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean,
|
2020-09-03 12:35:58 +00:00
|
|
|
auth: string | undefined,
|
2021-06-22 13:45:25 +00:00
|
|
|
osArch: string = translateArchToDistUrl(os.arch()),
|
|
|
|
manifest: tc.IToolRelease[] | undefined
|
2020-06-29 18:56:37 +00:00
|
|
|
): Promise<string | undefined> {
|
|
|
|
try {
|
2021-06-22 14:11:44 +00:00
|
|
|
const info = await getInfoFromManifest(
|
|
|
|
versionSpec,
|
|
|
|
stable,
|
|
|
|
auth,
|
|
|
|
osArch,
|
|
|
|
manifest
|
|
|
|
);
|
2020-06-29 18:56:37 +00:00
|
|
|
return info?.resolvedVersion;
|
|
|
|
} catch (err) {
|
|
|
|
core.info('Unable to resolve version from manifest...');
|
|
|
|
core.debug(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 01:49:54 +00:00
|
|
|
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
|
|
|
|
function evaluateVersions(versions: string[], versionSpec: string): string {
|
|
|
|
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: string = versions[i];
|
|
|
|
const satisfied: boolean = semver.satisfies(potential, versionSpec);
|
|
|
|
if (satisfied) {
|
|
|
|
version = potential;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version) {
|
|
|
|
core.debug(`matched: ${version}`);
|
|
|
|
} else {
|
|
|
|
core.debug('match not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:49:37 +00:00
|
|
|
async function queryDistForMatch(
|
|
|
|
versionSpec: string,
|
|
|
|
arch: string = os.arch()
|
|
|
|
): Promise<string> {
|
2020-05-19 13:25:54 +00:00
|
|
|
let osPlat: string = os.platform();
|
2020-09-03 13:49:37 +00:00
|
|
|
let osArch: string = translateArchToDistUrl(arch);
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
// node offers a json list of versions
|
|
|
|
let dataFileName: string;
|
|
|
|
switch (osPlat) {
|
|
|
|
case 'linux':
|
|
|
|
dataFileName = `linux-${osArch}`;
|
|
|
|
break;
|
|
|
|
case 'darwin':
|
|
|
|
dataFileName = `osx-${osArch}-tar`;
|
|
|
|
break;
|
|
|
|
case 'win32':
|
|
|
|
dataFileName = `win-${osArch}-exe`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unexpected OS '${osPlat}'`);
|
|
|
|
}
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
let versions: string[] = [];
|
2021-11-29 09:18:31 +00:00
|
|
|
let nodeVersions = await getVersionsFromDist();
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
nodeVersions.forEach((nodeVersion: INodeVersion) => {
|
|
|
|
// ensure this version supports your os and platform
|
|
|
|
if (nodeVersion.files.indexOf(dataFileName) >= 0) {
|
|
|
|
versions.push(nodeVersion.version);
|
|
|
|
}
|
|
|
|
});
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
// get the latest version that matches the version spec
|
|
|
|
let version: string = evaluateVersions(versions, versionSpec);
|
|
|
|
return version;
|
|
|
|
}
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-05-19 13:25:54 +00:00
|
|
|
export async function getVersionsFromDist(): Promise<INodeVersion[]> {
|
|
|
|
let dataUrl = 'https://nodejs.org/dist/index.json';
|
|
|
|
let httpClient = new hc.HttpClient('setup-node', [], {
|
|
|
|
allowRetries: true,
|
|
|
|
maxRetries: 3
|
|
|
|
});
|
|
|
|
let response = await httpClient.getJson<INodeVersion[]>(dataUrl);
|
|
|
|
return response.result || [];
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non LTS versions of Node, the files we need (for Windows) are sometimes located
|
|
|
|
// in a different folder than they normally are for other versions.
|
|
|
|
// Normally the format is similar to: https://nodejs.org/dist/v5.10.1/node-v5.10.1-win-x64.7z
|
|
|
|
// In this case, there will be two files located at:
|
|
|
|
// /dist/v5.10.1/win-x64/node.exe
|
|
|
|
// /dist/v5.10.1/win-x64/node.lib
|
|
|
|
// If this is not the structure, there may also be two files located at:
|
|
|
|
// /dist/v0.12.18/node.exe
|
|
|
|
// /dist/v0.12.18/node.lib
|
|
|
|
// This method attempts to download and cache the resources from these alternative locations.
|
|
|
|
// Note also that the files are normally zipped but in this case they are just an exe
|
|
|
|
// and lib file in a folder, not zipped.
|
|
|
|
async function acquireNodeFromFallbackLocation(
|
2020-09-03 12:24:52 +00:00
|
|
|
version: string,
|
|
|
|
arch: string = os.arch()
|
2019-08-04 01:49:54 +00:00
|
|
|
): Promise<string> {
|
2020-05-19 13:25:54 +00:00
|
|
|
let osPlat: string = os.platform();
|
2020-09-03 12:24:52 +00:00
|
|
|
let osArch: string = translateArchToDistUrl(arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
|
2019-08-04 01:49:54 +00:00
|
|
|
// Create temporary folder to download in to
|
2020-03-10 15:51:57 +00:00
|
|
|
const tempDownloadFolder: string =
|
2019-08-04 01:49:54 +00:00
|
|
|
'temp_' + Math.floor(Math.random() * 2000000000);
|
2020-03-10 15:51:57 +00:00
|
|
|
const tempDirectory = process.env['RUNNER_TEMP'] || '';
|
|
|
|
assert.ok(tempDirectory, 'Expected RUNNER_TEMP to be defined');
|
|
|
|
const tempDir: string = path.join(tempDirectory, tempDownloadFolder);
|
2019-08-04 01:49:54 +00:00
|
|
|
await io.mkdirP(tempDir);
|
|
|
|
let exeUrl: string;
|
|
|
|
let libUrl: string;
|
|
|
|
try {
|
2019-11-20 17:21:56 +00:00
|
|
|
exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`;
|
|
|
|
libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`;
|
2019-08-04 01:49:54 +00:00
|
|
|
|
2020-06-29 18:56:37 +00:00
|
|
|
core.info(`Downloading only node binary from ${exeUrl}`);
|
2020-05-19 13:25:54 +00:00
|
|
|
|
2019-08-04 01:49:54 +00:00
|
|
|
const exePath = await tc.downloadTool(exeUrl);
|
2019-08-06 02:23:46 +00:00
|
|
|
await io.cp(exePath, path.join(tempDir, 'node.exe'));
|
2019-08-04 01:49:54 +00:00
|
|
|
const libPath = await tc.downloadTool(libUrl);
|
2019-08-06 02:23:46 +00:00
|
|
|
await io.cp(libPath, path.join(tempDir, 'node.lib'));
|
2019-08-04 01:49:54 +00:00
|
|
|
} 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 = await tc.downloadTool(exeUrl);
|
2019-08-06 02:23:46 +00:00
|
|
|
await io.cp(exePath, path.join(tempDir, 'node.exe'));
|
2019-08-04 01:49:54 +00:00
|
|
|
const libPath = await tc.downloadTool(libUrl);
|
2019-08-06 02:23:46 +00:00
|
|
|
await io.cp(libPath, path.join(tempDir, 'node.lib'));
|
2019-08-04 01:49:54 +00:00
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2020-09-03 12:58:24 +00:00
|
|
|
let toolPath = await tc.cacheDir(tempDir, 'node', version, arch);
|
2020-05-19 13:25:54 +00:00
|
|
|
core.addPath(toolPath);
|
|
|
|
return toolPath;
|
2019-08-04 01:49:54 +00:00
|
|
|
}
|
2019-11-20 17:21:56 +00:00
|
|
|
|
|
|
|
// os.arch does not always match the relative download url, e.g.
|
|
|
|
// os.arch == 'arm' != node-v12.13.1-linux-armv7l.tar.gz
|
2019-11-20 20:30:44 +00:00
|
|
|
// All other currently supported architectures match, e.g.:
|
|
|
|
// os.arch = arm64 => https://nodejs.org/dist/v{VERSION}/node-v{VERSION}-{OS}-arm64.tar.gz
|
|
|
|
// os.arch = x64 => https://nodejs.org/dist/v{VERSION}/node-v{VERSION}-{OS}-x64.tar.gz
|
2019-11-20 17:21:56 +00:00
|
|
|
function translateArchToDistUrl(arch: string): string {
|
|
|
|
switch (arch) {
|
|
|
|
case 'arm':
|
|
|
|
return 'armv7l';
|
|
|
|
default:
|
|
|
|
return arch;
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 09:18:31 +00:00
|
|
|
|
|
|
|
export function parseNodeVersionFile(contents: string): string {
|
|
|
|
let nodeVersion = contents.trim();
|
|
|
|
|
|
|
|
if (/^v\d/.test(nodeVersion)) {
|
|
|
|
nodeVersion = nodeVersion.substring(1);
|
|
|
|
}
|
|
|
|
return nodeVersion;
|
|
|
|
}
|