mirror of
https://github.com/actions/setup-node.git
synced 2025-09-11 19:26:18 +00:00
Merge branch 'actions:main' into main
This commit is contained in:
commit
7b37530a14
64 changed files with 35500 additions and 9375 deletions
|
@ -3,6 +3,7 @@ import * as core from '@actions/core';
|
|||
import * as glob from '@actions/glob';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
||||
import {State} from './constants';
|
||||
import {
|
||||
|
@ -21,6 +22,7 @@ export const restoreCache = async (
|
|||
throw new Error(`Caching for '${packageManager}' is not supported`);
|
||||
}
|
||||
const platform = process.env.RUNNER_OS;
|
||||
const arch = os.arch();
|
||||
|
||||
const cachePaths = await getCacheDirectories(
|
||||
packageManagerInfo,
|
||||
|
@ -38,7 +40,7 @@ export const restoreCache = async (
|
|||
);
|
||||
}
|
||||
|
||||
const keyPrefix = `node-cache-${platform}-${packageManager}`;
|
||||
const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`;
|
||||
const primaryKey = `${keyPrefix}-${fileHash}`;
|
||||
core.debug(`primary key is ${primaryKey}`);
|
||||
|
||||
|
|
|
@ -13,10 +13,20 @@ process.on('uncaughtException', e => {
|
|||
core.info(`${warningPrefix}${e.message}`);
|
||||
});
|
||||
|
||||
export async function run() {
|
||||
// Added early exit to resolve issue with slow post action step:
|
||||
export async function run(earlyExit?: boolean) {
|
||||
try {
|
||||
const cacheLock = core.getState(State.CachePackageManager);
|
||||
await cachePackages(cacheLock);
|
||||
|
||||
if (cacheLock) {
|
||||
await cachePackages(cacheLock);
|
||||
|
||||
if (earlyExit) {
|
||||
process.exit(0);
|
||||
}
|
||||
} else {
|
||||
core.debug(`Caching for '${cacheLock}' is not supported`);
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed((error as Error).message);
|
||||
}
|
||||
|
@ -59,4 +69,4 @@ const cachePackages = async (packageManager: string) => {
|
|||
core.info(`Cache saved with the key: ${primaryKey}`);
|
||||
};
|
||||
|
||||
run();
|
||||
run(true);
|
||||
|
|
|
@ -295,7 +295,13 @@ export function isGhes(): boolean {
|
|||
const ghUrl = new URL(
|
||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||
);
|
||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
||||
|
||||
const hostname = ghUrl.hostname.trimEnd().toUpperCase();
|
||||
const isGitHubHost = hostname === 'GITHUB.COM';
|
||||
const isGitHubEnterpriseCloudHost = hostname.endsWith('.GHE.COM');
|
||||
const isLocalHost = hostname.endsWith('.LOCALHOST');
|
||||
|
||||
return !isGitHubHost && !isGitHubEnterpriseCloudHost && !isLocalHost;
|
||||
}
|
||||
|
||||
export function isCacheFeatureAvailable(): boolean {
|
||||
|
|
|
@ -112,7 +112,11 @@ export default abstract class BaseDistribution {
|
|||
? `node-v${version}-win-${osArch}`
|
||||
: `node-v${version}-${this.osPlat}-${osArch}`;
|
||||
const urlFileName: string =
|
||||
this.osPlat == 'win32' ? `${fileName}.7z` : `${fileName}.tar.gz`;
|
||||
this.osPlat == 'win32'
|
||||
? this.nodeInfo.arch === 'arm64'
|
||||
? `${fileName}.zip`
|
||||
: `${fileName}.7z`
|
||||
: `${fileName}.tar.gz`;
|
||||
const initialUrl = this.getDistributionUrl();
|
||||
const url = `${initialUrl}/v${version}/${urlFileName}`;
|
||||
|
||||
|
@ -146,7 +150,7 @@ export default abstract class BaseDistribution {
|
|||
throw err;
|
||||
}
|
||||
|
||||
const toolPath = await this.extractArchive(downloadPath, info);
|
||||
const toolPath = await this.extractArchive(downloadPath, info, true);
|
||||
core.info('Done');
|
||||
|
||||
return toolPath;
|
||||
|
@ -206,7 +210,8 @@ export default abstract class BaseDistribution {
|
|||
|
||||
protected async extractArchive(
|
||||
downloadPath: string,
|
||||
info: INodeVersionInfo | null
|
||||
info: INodeVersionInfo | null,
|
||||
isOfficialArchive?: boolean
|
||||
) {
|
||||
//
|
||||
// Extract
|
||||
|
@ -215,12 +220,24 @@ export default abstract class BaseDistribution {
|
|||
let extPath: string;
|
||||
info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here
|
||||
if (this.osPlat == 'win32') {
|
||||
const _7zPath = path.join(__dirname, '../..', 'externals', '7zr.exe');
|
||||
extPath = await tc.extract7z(downloadPath, undefined, _7zPath);
|
||||
const extension = this.nodeInfo.arch === 'arm64' ? '.zip' : '.7z';
|
||||
// Rename archive to add extension because after downloading
|
||||
// archive does not contain extension type and it leads to some issues
|
||||
// on Windows runners without PowerShell Core.
|
||||
//
|
||||
// For default PowerShell Windows it should contain extension type to unpack it.
|
||||
if (extension === '.zip' && isOfficialArchive) {
|
||||
const renamedArchive = `${downloadPath}.zip`;
|
||||
fs.renameSync(downloadPath, renamedArchive);
|
||||
extPath = await tc.extractZip(renamedArchive);
|
||||
} else {
|
||||
const _7zPath = path.join(__dirname, '../..', 'externals', '7zr.exe');
|
||||
extPath = await tc.extract7z(downloadPath, undefined, _7zPath);
|
||||
}
|
||||
// 7z extracts to folder matching file name
|
||||
const nestedPath = path.join(
|
||||
extPath,
|
||||
path.basename(info.fileName, '.7z')
|
||||
path.basename(info.fileName, extension)
|
||||
);
|
||||
if (fs.existsSync(nestedPath)) {
|
||||
extPath = nestedPath;
|
||||
|
@ -260,7 +277,11 @@ export default abstract class BaseDistribution {
|
|||
dataFileName = `osx-${osArch}-tar`;
|
||||
break;
|
||||
case 'win32':
|
||||
dataFileName = `win-${osArch}-exe`;
|
||||
if (this.nodeInfo.arch === 'arm64') {
|
||||
dataFileName = `win-${osArch}-zip`;
|
||||
} else {
|
||||
dataFileName = `win-${osArch}-exe`;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unexpected OS '${this.osPlat}'`);
|
||||
|
|
|
@ -88,7 +88,11 @@ export default class OfficialBuilds extends BaseDistribution {
|
|||
);
|
||||
|
||||
if (downloadPath) {
|
||||
toolPath = await this.extractArchive(downloadPath, versionInfo);
|
||||
toolPath = await this.extractArchive(
|
||||
downloadPath,
|
||||
versionInfo,
|
||||
false
|
||||
);
|
||||
}
|
||||
} else {
|
||||
core.info(
|
||||
|
|
15
src/main.ts
15
src/main.ts
|
@ -1,6 +1,5 @@
|
|||
import * as core from '@actions/core';
|
||||
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
||||
import * as auth from './authutil';
|
||||
|
@ -8,7 +7,7 @@ import * as path from 'path';
|
|||
import {restoreCache} from './cache-restore';
|
||||
import {isCacheFeatureAvailable} from './cache-utils';
|
||||
import {getNodejsDistribution} from './distributions/installer-factory';
|
||||
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
|
||||
import {getNodeVersionFromFile, printEnvDetailsAndSetOutput} from './util';
|
||||
import {State} from './constants';
|
||||
|
||||
export async function run() {
|
||||
|
@ -99,14 +98,16 @@ function resolveVersionInput(): string {
|
|||
versionFileInput
|
||||
);
|
||||
|
||||
if (!fs.existsSync(versionFilePath)) {
|
||||
throw new Error(
|
||||
`The specified node version file at: ${versionFilePath} does not exist`
|
||||
const parsedVersion = getNodeVersionFromFile(versionFilePath);
|
||||
|
||||
if (parsedVersion) {
|
||||
version = parsedVersion;
|
||||
} else {
|
||||
core.warning(
|
||||
`Could not determine node version from ${versionFilePath}. Falling back`
|
||||
);
|
||||
}
|
||||
|
||||
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
|
||||
|
||||
core.info(`Resolved ${versionFileInput} as ${version}`);
|
||||
}
|
||||
|
||||
|
|
68
src/util.ts
68
src/util.ts
|
@ -1,34 +1,70 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
import * as io from '@actions/io';
|
||||
|
||||
export function parseNodeVersionFile(contents: string): string {
|
||||
let nodeVersion: string | undefined;
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
export function getNodeVersionFromFile(versionFilePath: string): string | null {
|
||||
if (!fs.existsSync(versionFilePath)) {
|
||||
throw new Error(
|
||||
`The specified node version file at: ${versionFilePath} does not exist`
|
||||
);
|
||||
}
|
||||
|
||||
const contents = fs.readFileSync(versionFilePath, 'utf8');
|
||||
|
||||
// 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;
|
||||
const manifest = JSON.parse(contents);
|
||||
|
||||
// Presume package.json file.
|
||||
if (typeof manifest === 'object' && !!manifest) {
|
||||
// Support Volta.
|
||||
// See https://docs.volta.sh/guide/understanding#managing-your-project
|
||||
if (manifest.volta?.node) {
|
||||
return manifest.volta.node;
|
||||
}
|
||||
|
||||
if (manifest.engines?.node) {
|
||||
return manifest.engines.node;
|
||||
}
|
||||
|
||||
// Support Volta workspaces.
|
||||
// See https://docs.volta.sh/advanced/workspaces
|
||||
if (manifest.volta?.extends) {
|
||||
const extendedFilePath = path.resolve(
|
||||
path.dirname(versionFilePath),
|
||||
manifest.volta.extends
|
||||
);
|
||||
core.info('Resolving node version from ' + extendedFilePath);
|
||||
return getNodeVersionFromFile(extendedFilePath);
|
||||
}
|
||||
|
||||
// If contents are an object, we parsed JSON
|
||||
// this can happen if node-version-file is a package.json
|
||||
// yet contains no volta.node or engines.node
|
||||
//
|
||||
// If node-version file is _not_ JSON, control flow
|
||||
// will not have reached these lines.
|
||||
//
|
||||
// And because we've reached here, we know the contents
|
||||
// *are* JSON, so no further string parsing makes sense.
|
||||
return null;
|
||||
}
|
||||
} catch {
|
||||
core.info('Node version file is not JSON file');
|
||||
}
|
||||
|
||||
if (!nodeVersion) {
|
||||
const found = contents.match(/^(?:node(js)?\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;
|
||||
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
|
||||
return found?.groups?.version ?? contents.trim();
|
||||
}
|
||||
|
||||
export async function printEnvDetailsAndSetOutput() {
|
||||
core.startGroup('Environment details');
|
||||
|
||||
const promises = ['node', 'npm', 'yarn'].map(async tool => {
|
||||
const output = await getToolVersion(tool, ['--version']);
|
||||
const pathTool = await io.which(tool, false);
|
||||
const output = pathTool ? await getToolVersion(tool, ['--version']) : '';
|
||||
|
||||
return {tool, output};
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue