mirror of
https://code.forgejo.org/actions/setup-node.git
synced 2025-06-08 13:11:11 +00:00
fixed merge conflicts
This commit is contained in:
commit
bca281829f
88 changed files with 138029 additions and 274 deletions
156
src/installer.ts
156
src/installer.ts
|
@ -11,24 +11,43 @@ import {INodeVersion, getVersionsFromDist} from './node-version';
|
|||
interface INodeVersionInfo {
|
||||
downloadUrl: string;
|
||||
resolvedVersion: string;
|
||||
arch: string;
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
interface INodeRelease extends tc.IToolRelease {
|
||||
lts?: string;
|
||||
}
|
||||
|
||||
export async function getNode(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
checkLatest: boolean,
|
||||
auth: string | undefined
|
||||
auth: string | undefined,
|
||||
arch: string = os.arch()
|
||||
) {
|
||||
// Store manifest data to avoid multiple calls
|
||||
let manifest: INodeRelease[] | undefined;
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = translateArchToDistUrl(os.arch());
|
||||
let osArch: string = translateArchToDistUrl(arch);
|
||||
|
||||
if (isLtsAlias(versionSpec)) {
|
||||
core.info('Attempt to resolve LTS alias from manifest...');
|
||||
|
||||
// No try-catch since it's not possible to resolve LTS alias without manifest
|
||||
manifest = await getManifest(auth);
|
||||
|
||||
versionSpec = resolveLtsAliasFromManifest(versionSpec, stable, manifest);
|
||||
}
|
||||
|
||||
if (checkLatest) {
|
||||
core.info('Attempt to resolve the latest version from manifest...');
|
||||
const resolvedVersion = await resolveVersionFromManifest(
|
||||
versionSpec,
|
||||
stable,
|
||||
auth
|
||||
auth,
|
||||
osArch,
|
||||
manifest
|
||||
);
|
||||
if (resolvedVersion) {
|
||||
versionSpec = resolvedVersion;
|
||||
|
@ -40,7 +59,7 @@ export async function getNode(
|
|||
|
||||
// check cache
|
||||
let toolPath: string;
|
||||
toolPath = tc.find('node', versionSpec);
|
||||
toolPath = tc.find('node', versionSpec, osArch);
|
||||
|
||||
// If not found in cache, download
|
||||
if (toolPath) {
|
||||
|
@ -54,9 +73,17 @@ export async function getNode(
|
|||
// Try download from internal distribution (popular versions only)
|
||||
//
|
||||
try {
|
||||
info = await getInfoFromManifest(versionSpec, stable, auth);
|
||||
info = await getInfoFromManifest(
|
||||
versionSpec,
|
||||
stable,
|
||||
auth,
|
||||
osArch,
|
||||
manifest
|
||||
);
|
||||
if (info) {
|
||||
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
|
||||
core.info(
|
||||
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
|
||||
);
|
||||
downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
|
||||
} else {
|
||||
core.info(
|
||||
|
@ -83,19 +110,24 @@ export async function getNode(
|
|||
// Download from nodejs.org
|
||||
//
|
||||
if (!downloadPath) {
|
||||
info = await getInfoFromDist(versionSpec);
|
||||
info = await getInfoFromDist(versionSpec, arch);
|
||||
if (!info) {
|
||||
throw new Error(
|
||||
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
|
||||
);
|
||||
}
|
||||
|
||||
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
|
||||
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);
|
||||
return await acquireNodeFromFallbackLocation(
|
||||
info.resolvedVersion,
|
||||
info.arch
|
||||
);
|
||||
}
|
||||
|
||||
throw err;
|
||||
|
@ -109,7 +141,7 @@ export async function getNode(
|
|||
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');
|
||||
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'));
|
||||
|
@ -128,7 +160,12 @@ export async function getNode(
|
|||
// Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded
|
||||
//
|
||||
core.info('Adding to the cache ...');
|
||||
toolPath = await tc.cacheDir(extPath, 'node', info.resolvedVersion);
|
||||
toolPath = await tc.cacheDir(
|
||||
extPath,
|
||||
'node',
|
||||
info.resolvedVersion,
|
||||
info.arch
|
||||
);
|
||||
core.info('Done');
|
||||
}
|
||||
|
||||
|
@ -146,23 +183,70 @@ export async function getNode(
|
|||
core.addPath(toolPath);
|
||||
}
|
||||
|
||||
function isLtsAlias(versionSpec: string): boolean {
|
||||
return versionSpec.startsWith('lts/');
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
function resolveLtsAliasFromManifest(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
manifest: INodeRelease[]
|
||||
): string {
|
||||
const alias = versionSpec.split('lts/')[1]?.toLowerCase();
|
||||
|
||||
if (!alias) {
|
||||
throw new Error(
|
||||
`Unable to parse LTS alias for Node version '${versionSpec}'`
|
||||
);
|
||||
}
|
||||
|
||||
core.debug(`LTS alias '${alias}' for Node version '${versionSpec}'`);
|
||||
|
||||
// Supported formats are `lts/<alias>` and `lts/*`. Where asterisk means highest possible LTS.
|
||||
const release =
|
||||
alias === '*'
|
||||
? manifest.find(x => !!x.lts && x.stable === stable)
|
||||
: manifest.find(
|
||||
x => x.lts?.toLowerCase() === alias && x.stable === stable
|
||||
);
|
||||
|
||||
if (!release) {
|
||||
throw new Error(
|
||||
`Unable to find LTS release '${alias}' for Node version '${versionSpec}'.`
|
||||
);
|
||||
}
|
||||
|
||||
core.debug(
|
||||
`Found LTS release '${release.version}' for Node version '${versionSpec}'`
|
||||
);
|
||||
|
||||
return release.version.split('.')[0];
|
||||
}
|
||||
|
||||
async function getInfoFromManifest(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
auth: string | undefined
|
||||
auth: string | undefined,
|
||||
osArch: string = translateArchToDistUrl(os.arch()),
|
||||
manifest: tc.IToolRelease[] | undefined
|
||||
): Promise<INodeVersionInfo | null> {
|
||||
let info: INodeVersionInfo | null = null;
|
||||
const releases = await tc.getManifestFromRepo(
|
||||
'actions',
|
||||
'node-versions',
|
||||
auth,
|
||||
'main'
|
||||
);
|
||||
const rel = await tc.findFromManifest(versionSpec, stable, releases);
|
||||
if (!manifest) {
|
||||
core.debug('No manifest cached');
|
||||
manifest = await getManifest(auth);
|
||||
}
|
||||
|
||||
const rel = await tc.findFromManifest(versionSpec, stable, manifest, osArch);
|
||||
|
||||
if (rel && rel.files.length > 0) {
|
||||
info = <INodeVersionInfo>{};
|
||||
info.resolvedVersion = rel.version;
|
||||
info.arch = rel.files[0].arch;
|
||||
info.downloadUrl = rel.files[0].download_url;
|
||||
info.fileName = rel.files[0].filename;
|
||||
}
|
||||
|
@ -171,14 +255,15 @@ async function getInfoFromManifest(
|
|||
}
|
||||
|
||||
async function getInfoFromDist(
|
||||
versionSpec: string
|
||||
versionSpec: string,
|
||||
arch: string = os.arch()
|
||||
): Promise<INodeVersionInfo | null> {
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = translateArchToDistUrl(os.arch());
|
||||
let osArch: string = translateArchToDistUrl(arch);
|
||||
|
||||
let version: string;
|
||||
|
||||
version = await queryDistForMatch(versionSpec);
|
||||
version = await queryDistForMatch(versionSpec, arch);
|
||||
if (!version) {
|
||||
return null;
|
||||
}
|
||||
|
@ -198,6 +283,7 @@ async function getInfoFromDist(
|
|||
return <INodeVersionInfo>{
|
||||
downloadUrl: url,
|
||||
resolvedVersion: version,
|
||||
arch: arch,
|
||||
fileName: fileName
|
||||
};
|
||||
}
|
||||
|
@ -205,10 +291,18 @@ async function getInfoFromDist(
|
|||
async function resolveVersionFromManifest(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
auth: string | undefined
|
||||
auth: string | undefined,
|
||||
osArch: string = translateArchToDistUrl(os.arch()),
|
||||
manifest: tc.IToolRelease[] | undefined
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
const info = await getInfoFromManifest(versionSpec, stable, auth);
|
||||
const info = await getInfoFromManifest(
|
||||
versionSpec,
|
||||
stable,
|
||||
auth,
|
||||
osArch,
|
||||
manifest
|
||||
);
|
||||
return info?.resolvedVersion;
|
||||
} catch (err) {
|
||||
core.info('Unable to resolve version from manifest...');
|
||||
|
@ -244,9 +338,12 @@ function evaluateVersions(versions: string[], versionSpec: string): string {
|
|||
return version;
|
||||
}
|
||||
|
||||
async function queryDistForMatch(versionSpec: string): Promise<string> {
|
||||
async function queryDistForMatch(
|
||||
versionSpec: string,
|
||||
arch: string = os.arch()
|
||||
): Promise<string> {
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = translateArchToDistUrl(os.arch());
|
||||
let osArch: string = translateArchToDistUrl(arch);
|
||||
|
||||
// node offers a json list of versions
|
||||
let dataFileName: string;
|
||||
|
@ -292,10 +389,11 @@ async function queryDistForMatch(versionSpec: string): Promise<string> {
|
|||
// 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
|
||||
version: string,
|
||||
arch: string = os.arch()
|
||||
): Promise<string> {
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = translateArchToDistUrl(os.arch());
|
||||
let osArch: string = translateArchToDistUrl(arch);
|
||||
|
||||
// Create temporary folder to download in to
|
||||
const tempDownloadFolder: string =
|
||||
|
@ -329,7 +427,7 @@ async function acquireNodeFromFallbackLocation(
|
|||
throw err;
|
||||
}
|
||||
}
|
||||
let toolPath = await tc.cacheDir(tempDir, 'node', version);
|
||||
let toolPath = await tc.cacheDir(tempDir, 'node', version, arch);
|
||||
core.addPath(toolPath);
|
||||
return toolPath;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue