setup-node/src/installer.ts

686 lines
21 KiB
TypeScript
Raw Normal View History

import os from 'os';
2020-01-27 10:37:12 -05:00
import * as assert from 'assert';
2019-08-03 21:49:54 -04:00
import * as core from '@actions/core';
2020-01-27 10:37:12 -05:00
import * as hc from '@actions/http-client';
2019-08-03 21:49:54 -04:00
import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as path from 'path';
import * as semver from 'semver';
import fs from 'fs';
2019-08-03 21:49:54 -04:00
//
// Node versions interface
// see https://nodejs.org/dist/index.json
// for nightly https://nodejs.org/download/nightly/index.json
// for rc https://nodejs.org/download/rc/index.json
2019-08-03 21:49:54 -04:00
//
export interface INodeVersion {
2019-08-03 21:49:54 -04:00
version: string;
files: string[];
}
interface INodeVersionInfo {
downloadUrl: string;
resolvedVersion: string;
2020-09-03 07:29:40 -05:00
arch: string;
fileName: string;
}
2021-06-21 11:37:50 +02:00
interface INodeRelease extends tc.IToolRelease {
lts?: string;
}
2022-11-24 05:16:09 +01:00
export enum Distributions {
2022-11-24 18:52:31 +01:00
DEFAULT = 'default',
CANARY = 'v8-canary',
NIGHTLY = 'nightly',
RC = 'rc'
2022-11-24 05:16:09 +01:00
}
2022-11-24 18:52:31 +01:00
export const distributionOf = (versionSpec: string): Distributions => {
if (versionSpec.includes('-v8-canary')) return Distributions.CANARY;
if (versionSpec.includes('nightly')) return Distributions.NIGHTLY;
if (semver.prerelease(versionSpec)) return Distributions.RC;
return Distributions.DEFAULT;
};
2022-11-24 05:16:09 +01:00
interface VersionMatcher {
2022-11-24 05:29:48 +01:00
(potential: string): boolean;
2022-11-24 05:16:09 +01:00
// memoize the factory for testing and debug purposes
2022-11-24 05:29:48 +01:00
factory:
| ((ver: string, suffix: string) => VersionMatcher)
| ((semverRanger: string) => VersionMatcher)
| (() => VersionMatcher);
2022-11-24 05:16:09 +01:00
}
export const semverVersionMatcherFactory = (range: string): VersionMatcher => {
2022-11-24 05:29:48 +01:00
const matcher = (potential: string): boolean =>
semver.satisfies(potential, range);
matcher.factory = semverVersionMatcherFactory;
return matcher;
};
export const canaryRangeVersionMatcherFactory = (
version: string
): VersionMatcher => {
const range = semver.validRange(`^${version}`);
2022-11-24 05:16:09 +01:00
const matcher = (potential: string): boolean =>
semver.satisfies(potential.replace('-v8-canary', '+v8-canary.'), range);
2022-11-24 05:29:48 +01:00
matcher.factory = canaryRangeVersionMatcherFactory;
return matcher;
};
export const canaryExactVersionMatcherFactory = (
version: string,
timestamp: string
): VersionMatcher => {
const range = `${version}-${timestamp}`;
2022-11-24 05:16:09 +01:00
const matcher = (potential: string): boolean =>
semver.satisfies(potential, range);
2022-11-24 05:29:48 +01:00
matcher.factory = canaryExactVersionMatcherFactory;
return matcher;
};
export const nightlyRangeVersionMatcherFactory = (
version: string
): VersionMatcher => {
2022-11-24 12:15:42 +01:00
const range = semver.validRange(`^${version}`);
// TODO: this makes v20.1.1-nightly to do not match v20.1.1-nightly20221103f7e2421e91
// const range = `${semver.validRange(`^${version}-0`)}-0`;
2022-11-24 05:16:09 +01:00
const matcher = (potential: string): boolean =>
distributionOf(potential) === Distributions.NIGHTLY &&
2022-11-24 12:15:42 +01:00
// TODO: dmitry's variant was potential.replace('-nightly', '-nightly.') that made
// all unit tests to fail
semver.satisfies(
potential.replace('-nightly', '+nightly.'),
range /*, {
// TODO: what is for?
2022-11-24 05:29:48 +01:00
includePrerelease: true
2022-11-24 12:15:42 +01:00
}*/
);
2022-11-24 05:29:48 +01:00
matcher.factory = nightlyRangeVersionMatcherFactory;
return matcher;
};
export const nightlyExactVersionMatcherFactory = (
version: string,
2022-11-24 12:15:42 +01:00
timestamp: string
2022-11-24 05:29:48 +01:00
): VersionMatcher => {
2022-11-24 12:15:42 +01:00
const range = `${version}-${timestamp.replace('nightly', 'nightly.')}`;
2022-11-24 05:16:09 +01:00
const matcher = (potential: string): boolean =>
distributionOf(potential) === Distributions.NIGHTLY &&
2022-11-24 12:15:42 +01:00
semver.satisfies(
potential.replace('-nightly', '-nightly.'),
range /*, {
// TODO: what is for?
2022-11-24 05:29:48 +01:00
includePrerelease: true
2022-11-24 12:15:42 +01:00
}*/
);
2022-11-24 05:29:48 +01:00
matcher.factory = nightlyExactVersionMatcherFactory;
return matcher;
};
2022-11-24 05:16:09 +01:00
const alwaysFalseVersionMatcherFactory = (): VersionMatcher => {
const matcher = () => false;
2022-11-24 05:29:48 +01:00
matcher.factory = alwaysFalseVersionMatcherFactory;
return matcher;
};
2022-11-24 05:16:09 +01:00
2022-11-24 05:29:48 +01:00
const alwaysFalseVersionMatcher = alwaysFalseVersionMatcherFactory();
2022-11-24 05:16:09 +01:00
// [raw, prerelease]
2022-11-24 05:29:48 +01:00
export const splitVersionSpec = (versionSpec: string): string[] =>
versionSpec.split(/-(.*)/s);
2022-11-24 05:16:09 +01:00
export function versionMatcherFactory(versionSpec: string): VersionMatcher {
2022-11-24 05:29:48 +01:00
const [raw, prerelease] = splitVersionSpec(versionSpec);
2022-11-24 05:16:09 +01:00
const validVersion = semver.valid(raw) ? raw : semver.coerce(raw)?.version;
if (validVersion) {
switch (distributionOf(versionSpec)) {
case Distributions.CANARY:
2022-11-24 05:29:48 +01:00
return prerelease === 'v8-canary' // this means versionSpec does not have timestamp
2022-11-24 05:16:09 +01:00
? canaryRangeVersionMatcherFactory(validVersion)
2022-11-24 05:29:48 +01:00
: canaryExactVersionMatcherFactory(validVersion, prerelease);
2022-11-24 05:16:09 +01:00
case Distributions.NIGHTLY:
2022-11-24 05:29:48 +01:00
return prerelease === 'nightly' // this means versionSpec does not have prerelease tag
? nightlyRangeVersionMatcherFactory(validVersion)
: nightlyExactVersionMatcherFactory(validVersion, prerelease);
2022-11-24 05:16:09 +01:00
case Distributions.RC:
case Distributions.DEFAULT:
2022-11-24 05:29:48 +01:00
return semverVersionMatcherFactory(versionSpec);
2022-11-24 05:16:09 +01:00
}
} else {
// TODO: i prefer to have implicit exception for the malformed input
2022-11-24 05:29:48 +01:00
throw Error(`Invalid version input "${versionSpec}"`);
2022-11-24 05:16:09 +01:00
// TODO: but it is possible to silently fail
// return alwaysFalseVersionMatcher
}
}
2022-11-09 20:39:42 +01:00
export async function getNode(
versionSpec: string,
stable: boolean,
checkLatest: boolean,
auth: string | undefined,
arch: string = os.arch()
) {
// Store manifest data to avoid multiple calls
let manifest: INodeRelease[] | undefined;
2022-05-20 16:32:52 +02:00
let nodeVersions: INodeVersion[] | undefined;
2022-11-24 05:16:09 +01:00
const osPlat: string = os.platform();
const osArch: string = translateArchToDistUrl(arch);
2022-11-24 05:29:48 +01:00
const distribution = distributionOf(versionSpec);
2021-06-22 13:10:16 +02:00
if (isLtsAlias(versionSpec)) {
core.info('Attempt to resolve LTS alias from manifest...');
2021-06-22 17:52:06 +02:00
// No try-catch since it's not possible to resolve LTS alias without manifest
2021-06-22 17:52:06 +02:00
manifest = await getManifest(auth);
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
2021-06-17 17:51:02 +02:00
}
2022-11-24 18:52:31 +01:00
if (isLatestSyntax(versionSpec)) {
nodeVersions = await getVersionsFromDist(versionSpec);
2022-05-20 16:32:52 +02:00
versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions);
2022-11-24 18:52:31 +01:00
core.info(`getting latest node version ${versionSpec}...`);
2022-05-20 13:45:28 +02:00
}
2022-11-24 18:52:31 +01:00
if (
(distribution === Distributions.NIGHTLY ||
distribution === Distributions.CANARY) &&
checkLatest
) {
nodeVersions = await getVersionsFromDist(versionSpec);
versionSpec = await queryDistForMatch(versionSpec, arch, nodeVersions);
}
2022-11-24 18:52:31 +01:00
if (
checkLatest &&
distribution !== Distributions.NIGHTLY &&
distribution !== Distributions.CANARY
) {
core.info('Attempt to resolve the latest version from manifest...');
const resolvedVersion = await resolveVersionFromManifest(
versionSpec,
stable,
auth,
osArch,
manifest
);
if (resolvedVersion) {
versionSpec = resolvedVersion;
core.info(`Resolved as '${versionSpec}'`);
} else {
core.info(`Failed to resolve version ${versionSpec} from manifest`);
}
}
2019-08-03 21:49:54 -04:00
// check cache
let toolPath: string;
2022-11-24 05:16:09 +01:00
if (distribution === Distributions.DEFAULT) {
toolPath = tc.find('node', versionSpec, osArch);
2022-11-24 05:16:09 +01:00
} else {
2022-11-24 05:29:48 +01:00
const localVersionPaths = tc.findAllVersions('node', osArch);
2022-11-24 05:16:09 +01:00
const localVersion = evaluateVersions(localVersionPaths, versionSpec);
toolPath = localVersion && tc.find('node', localVersion, osArch);
}
2019-08-03 21:49:54 -04:00
// If not found in cache, download
if (toolPath) {
core.info(`Found in cache @ ${toolPath}`);
} else {
core.info(`Attempting to download ${versionSpec}...`);
let downloadPath = '';
let info: INodeVersionInfo | null = null;
//
// Try download from internal distribution (popular versions only)
//
try {
2021-06-22 16:11:44 +02:00
info = await getInfoFromManifest(
versionSpec,
stable,
auth,
osArch,
manifest
);
if (info) {
2020-09-03 07:50:29 -05:00
core.info(
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
);
downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
} else {
core.info(
'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)
) {
core.info(
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
);
} else {
core.info(err.message);
}
core.debug(err.stack);
core.info('Falling back to download directly from Node');
}
//
// Download from nodejs.org
//
if (!downloadPath) {
2022-05-20 16:32:52 +02:00
info = await getInfoFromDist(versionSpec, arch, nodeVersions);
if (!info) {
2019-08-03 21:49:54 -04:00
throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
);
}
2020-09-03 07:50:29 -05:00
core.info(
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
);
try {
downloadPath = await tc.downloadTool(info.downloadUrl);
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await acquireNodeFromFallbackLocation(
info.resolvedVersion,
info.arch
);
}
throw err;
}
2019-08-03 21:49:54 -04:00
}
//
// Extract
//
core.info('Extracting ...');
let extPath: string;
info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here
if (osPlat == 'win32') {
let _7zPath = path.join(__dirname, '../..', 'externals', '7zr.exe');
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-03 21:49:54 -04:00
}
//
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
//
core.info('Adding to the cache ...');
2020-09-03 07:58:24 -05:00
toolPath = await tc.cacheDir(
extPath,
'node',
info.resolvedVersion,
info.arch
);
core.info('Done');
2019-08-03 21:49:54 -04: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);
}
2022-11-09 20:41:33 +01:00
export function isLtsAlias(versionSpec: string): boolean {
2021-06-30 09:34:42 +02:00
return versionSpec.startsWith('lts/');
2021-06-17 17:51:02 +02:00
}
2021-06-22 17:52:06 +02: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');
}
2022-11-09 20:41:33 +01:00
export function resolveLtsAliasFromManifest(
2021-06-17 17:51:02 +02:00
versionSpec: string,
stable: boolean,
2021-06-22 13:10:16 +02:00
manifest: INodeRelease[]
2021-06-17 17:51:02 +02:00
): string {
const alias = versionSpec.split('lts/')[1]?.toLowerCase();
if (!alias) {
2021-06-22 16:11:44 +02:00
throw new Error(
2021-06-30 09:34:42 +02:00
`Unable to parse LTS alias for Node version '${versionSpec}'`
2021-06-22 16:11:44 +02:00
);
2021-06-17 17:51:02 +02:00
}
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
2022-06-02 03:30:33 -07:00
// Supported formats are `lts/<alias>`, `lts/*`, and `lts/-n`. Where asterisk means highest possible LTS and -n means the nth-highest.
const n = Number(alias);
const aliases = Object.fromEntries(
manifest
.filter(x => x.lts && x.stable === stable)
.map(x => [x.lts!.toLowerCase(), x])
.reverse()
);
const numbered = Object.values(aliases);
2021-06-22 16:11:44 +02:00
const release =
alias === '*'
2022-06-02 03:30:33 -07:00
? numbered[numbered.length - 1]
: n < 0
2022-11-24 05:29:48 +01:00
? numbered[numbered.length - 1 + n]
: aliases[alias];
2021-06-17 17:51:02 +02:00
if (!release) {
2021-06-22 16:11:44 +02:00
throw new Error(
`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`
);
2021-06-17 17:51:02 +02:00
}
2021-06-22 16:11:44 +02:00
core.debug(
`Found LTS release '${release.version}' for Node version '${versionSpec}'`
);
2021-06-17 17:51:02 +02:00
return release.version.split('.')[0];
}
async function getInfoFromManifest(
versionSpec: string,
stable: boolean,
2020-09-03 07:34:03 -05:00
auth: string | undefined,
osArch: string = translateArchToDistUrl(os.arch()),
manifest: tc.IToolRelease[] | undefined
): Promise<INodeVersionInfo | null> {
let info: INodeVersionInfo | null = null;
if (!manifest) {
2021-06-22 17:52:06 +02:00
core.debug('No manifest cached');
manifest = await getManifest(auth);
2021-06-17 17:51:02 +02:00
}
const rel = await tc.findFromManifest(versionSpec, stable, manifest, osArch);
if (rel && rel.files.length > 0) {
info = <INodeVersionInfo>{};
info.resolvedVersion = rel.version;
2020-09-03 07:34:03 -05:00
info.arch = rel.files[0].arch;
info.downloadUrl = rel.files[0].download_url;
info.fileName = rel.files[0].filename;
2019-08-03 21:49:54 -04:00
}
return info;
}
2019-08-03 21:49:54 -04:00
async function getInfoFromDist(
2020-09-03 07:54:48 -05:00
versionSpec: string,
2022-05-19 18:21:03 +02:00
arch: string = os.arch(),
2022-05-20 16:32:52 +02:00
nodeVersions?: INodeVersion[]
): Promise<INodeVersionInfo | null> {
let osPlat: string = os.platform();
2020-09-03 07:54:48 -05:00
let osArch: string = translateArchToDistUrl(arch);
2022-05-20 13:45:28 +02:00
let version: string = await queryDistForMatch(
versionSpec,
arch,
2022-05-20 16:32:52 +02:00
nodeVersions
2022-05-20 13:45:28 +02: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`;
const initialUrl = getNodejsDistUrl(versionSpec);
const url = `${initialUrl}/v${version}/${urlFileName}`;
return <INodeVersionInfo>{
downloadUrl: url,
resolvedVersion: version,
arch: arch,
fileName: fileName
};
2019-08-03 21:49:54 -04:00
}
async function resolveVersionFromManifest(
versionSpec: string,
stable: boolean,
auth: string | undefined,
osArch: string = translateArchToDistUrl(os.arch()),
manifest: tc.IToolRelease[] | undefined
): Promise<string | undefined> {
try {
2021-06-22 16:11:44 +02:00
const info = await getInfoFromManifest(
versionSpec,
stable,
auth,
osArch,
manifest
);
return info?.resolvedVersion;
} catch (err) {
core.info('Unable to resolve version from manifest...');
core.debug(err.message);
}
}
2019-08-03 21:49:54 -04:00
// TODO - should we just export this from @actions/tool-cache? Lifted directly from there
2022-11-24 05:16:09 +01:00
// - the answer from dsame@github.com - we have customized matcher and can not
// export `evaluateVersions` from tc. But it would be possible to modify tc to accept
// the matcher as an optional parameter to `evaluateVersions`
2022-11-24 05:29:48 +01:00
export function evaluateVersions(
versions: string[],
versionSpec: string
): string {
2019-08-03 21:49:54 -04:00
core.debug(`evaluating ${versions.length} versions`);
2022-11-24 05:29:48 +01:00
const matcher = versionMatcherFactory(versionSpec);
const version = versions.sort(semver.rcompare).find(matcher) || '';
2019-08-03 21:49:54 -04:00
if (version) {
core.debug(`matched: ${version}`);
} else {
core.debug('match not found');
}
return version;
}
export function getNodejsDistUrl(version: string) {
2022-11-24 05:16:09 +01:00
switch (distributionOf(version)) {
case Distributions.CANARY:
return 'https://nodejs.org/download/v8-canary';
case Distributions.NIGHTLY:
return 'https://nodejs.org/download/nightly';
case Distributions.RC:
return 'https://nodejs.org/download/rc';
case Distributions.DEFAULT:
return 'https://nodejs.org/dist';
}
}
2022-11-09 20:39:42 +01:00
export async function queryDistForMatch(
2020-09-03 08:49:37 -05:00
versionSpec: string,
2022-05-20 13:45:28 +02:00
arch: string = os.arch(),
2022-05-20 16:32:52 +02:00
nodeVersions?: INodeVersion[]
2020-09-03 08:49:37 -05:00
): Promise<string> {
let osPlat: string = os.platform();
2020-09-03 08:49:37 -05:00
let osArch: string = translateArchToDistUrl(arch);
2019-08-03 21:49:54 -04: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-03 21:49:54 -04:00
2022-05-20 16:32:52 +02:00
if (!nodeVersions) {
2022-05-20 13:45:28 +02:00
core.debug('No dist manifest cached');
nodeVersions = await getVersionsFromDist(versionSpec);
2022-05-20 13:45:28 +02:00
}
2022-05-19 17:33:13 +02:00
if (isLatestSyntax(versionSpec)) {
2022-05-19 17:02:08 +02:00
core.info(`getting latest node version...`);
2022-05-20 16:32:52 +02:00
return nodeVersions[0].version;
2022-05-19 17:02:08 +02:00
}
2022-11-24 18:52:31 +01:00
const versions: string[] = [];
2022-05-20 16:32:52 +02: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-03 21:49:54 -04:00
// get the latest version that matches the version spec
2022-11-24 18:52:31 +01:00
const version = evaluateVersions(versions, versionSpec);
return version;
}
2019-08-03 21:49:54 -04:00
export async function getVersionsFromDist(
versionSpec: string
): Promise<INodeVersion[]> {
2022-11-09 20:39:42 +01:00
const distUrl = getNodejsDistUrl(versionSpec);
const dataUrl = `${distUrl}/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-03 21:49:54 -04: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(
version: string,
arch: string = os.arch()
2019-08-03 21:49:54 -04:00
): Promise<string> {
const initialUrl = getNodejsDistUrl(version);
let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(arch);
2019-08-03 21:49:54 -04:00
// Create temporary folder to download in to
const tempDownloadFolder: string =
2019-08-03 21:49:54 -04:00
'temp_' + Math.floor(Math.random() * 2000000000);
const tempDirectory = process.env['RUNNER_TEMP'] || '';
assert.ok(tempDirectory, 'Expected RUNNER_TEMP to be defined');
const tempDir: string = path.join(tempDirectory, tempDownloadFolder);
2019-08-03 21:49:54 -04:00
await io.mkdirP(tempDir);
let exeUrl: string;
let libUrl: string;
try {
exeUrl = `${initialUrl}/v${version}/win-${osArch}/node.exe`;
libUrl = `${initialUrl}/v${version}/win-${osArch}/node.lib`;
2019-08-03 21:49:54 -04:00
core.info(`Downloading only node binary from ${exeUrl}`);
2019-08-03 21:49:54 -04:00
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
2019-08-03 21:49:54 -04:00
const libPath = await tc.downloadTool(libUrl);
await io.cp(libPath, path.join(tempDir, 'node.lib'));
2019-08-03 21:49:54 -04:00
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
exeUrl = `${initialUrl}/v${version}/node.exe`;
libUrl = `${initialUrl}/v${version}/node.lib`;
2019-08-03 21:49:54 -04:00
const exePath = await tc.downloadTool(exeUrl);
await io.cp(exePath, path.join(tempDir, 'node.exe'));
2019-08-03 21:49:54 -04:00
const libPath = await tc.downloadTool(libUrl);
await io.cp(libPath, path.join(tempDir, 'node.lib'));
2019-08-03 21:49:54 -04:00
} else {
throw err;
}
}
2020-09-03 07:58:24 -05:00
let toolPath = await tc.cacheDir(tempDir, 'node', version, arch);
core.addPath(toolPath);
return toolPath;
2019-08-03 21:49:54 -04:00
}
2019-11-20 12:21:56 -05: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 15:30:44 -05: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 12:21:56 -05:00
function translateArchToDistUrl(arch: string): string {
switch (arch) {
case 'arm':
return 'armv7l';
default:
return arch;
}
}
export function parseNodeVersionFile(contents: string): string {
let nodeVersion: string | undefined;
// Try parsing the file as an NPM `package.json` file.
try {
nodeVersion = JSON.parse(contents).volta?.node;
if (!nodeVersion) nodeVersion = JSON.parse(contents).engines?.node;
} catch {
2022-09-26 13:58:23 +02:00
core.info('Node version file is not JSON file');
}
if (!nodeVersion) {
const found = contents.match(/^(?:nodejs\s+)?v?(?<version>[^\s]+)$/m);
nodeVersion = found?.groups?.version;
}
// In the case of an unknown format,
// return as is and evaluate the version separately.
if (!nodeVersion) nodeVersion = contents.trim();
return nodeVersion as string;
}
2022-05-19 17:33:13 +02:00
2022-11-09 20:41:33 +01:00
export function isLatestSyntax(versionSpec): boolean {
2022-05-19 17:33:13 +02:00
return ['current', 'latest', 'node'].includes(versionSpec);
}