mirror of
https://github.com/actions/setup-node.git
synced 2025-07-26 08:28:23 +02:00
restructure tests
This commit is contained in:
parent
822a587e93
commit
2723204c59
14 changed files with 1956 additions and 1104 deletions
|
@ -7,7 +7,7 @@ import semver from 'semver';
|
|||
import * as assert from 'assert';
|
||||
|
||||
import * as path from 'path';
|
||||
import * as os from 'os';
|
||||
import os from 'os';
|
||||
import fs from 'fs';
|
||||
|
||||
import {INodejs, INodeVersion, INodeVersionInfo} from './base-models';
|
||||
|
@ -27,6 +27,16 @@ export default abstract class BaseDistribution {
|
|||
protected abstract evaluateVersions(nodeVersions: string[]): string;
|
||||
|
||||
public async getNodeJsInfo() {
|
||||
if (this.nodeInfo.checkLatest) {
|
||||
const nodeVersions = await this.getNodejsVersions();
|
||||
const versions = this.filterVersions(nodeVersions);
|
||||
const evaluatedVersion = this.evaluateVersions(versions);
|
||||
|
||||
if (evaluatedVersion) {
|
||||
this.nodeInfo.versionSpec = evaluatedVersion;
|
||||
}
|
||||
}
|
||||
|
||||
let toolPath = this.findVersionInHoostedToolCacheDirectory();
|
||||
if (toolPath) {
|
||||
core.info(`Found in cache @ ${toolPath}`);
|
||||
|
@ -34,6 +44,11 @@ export default abstract class BaseDistribution {
|
|||
const nodeVersions = await this.getNodejsVersions();
|
||||
const versions = this.filterVersions(nodeVersions);
|
||||
const evaluatedVersion = this.evaluateVersions(versions);
|
||||
if (!evaluatedVersion) {
|
||||
throw new Error(
|
||||
`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`
|
||||
);
|
||||
}
|
||||
const toolName = this.getNodejsDistInfo(evaluatedVersion, this.osPlat);
|
||||
toolPath = await this.downloadNodejs(toolName);
|
||||
}
|
||||
|
@ -79,6 +94,9 @@ export default abstract class BaseDistribution {
|
|||
|
||||
protected async downloadNodejs(info: INodeVersionInfo) {
|
||||
let downloadPath = '';
|
||||
core.info(
|
||||
`Acquiring ${info.resolvedVersion} - ${info.arch} from ${info.downloadUrl}`
|
||||
);
|
||||
try {
|
||||
downloadPath = await tc.downloadTool(info.downloadUrl);
|
||||
} catch (err) {
|
||||
|
|
|
@ -29,7 +29,7 @@ function identifyDistribution(versionSpec: string) {
|
|||
|
||||
export function getNodejsDistribution(
|
||||
installerOptions: INodejs
|
||||
): BaseDistribution | null {
|
||||
): BaseDistribution {
|
||||
const distributionName = identifyDistribution(installerOptions.versionSpec);
|
||||
switch (distributionName) {
|
||||
case Distributions.NIGHTLY:
|
||||
|
@ -38,9 +38,7 @@ export function getNodejsDistribution(
|
|||
return new CanaryBuild(installerOptions);
|
||||
case Distributions.RC:
|
||||
return new RcBuild(installerOptions);
|
||||
case Distributions.DEFAULT:
|
||||
return new OfficialBuilds(installerOptions);
|
||||
default:
|
||||
return null;
|
||||
return new OfficialBuilds(installerOptions);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ export default class NightlyNodejs extends BaseDistribution {
|
|||
|
||||
return prerelease[0].includes('nightly');
|
||||
});
|
||||
localVersionPaths.sort(semver.rcompare);
|
||||
const localVersion = this.evaluateVersions(localVersionPaths);
|
||||
if (localVersion) {
|
||||
toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
|
||||
|
|
|
@ -37,7 +37,7 @@ export default class OfficialBuilds extends BaseDistribution {
|
|||
const versions = this.filterVersions(nodeVersions);
|
||||
this.nodeInfo.versionSpec = this.evaluateVersions(versions);
|
||||
|
||||
core.info(`getting latest node version...`);
|
||||
core.info('getting latest node version...');
|
||||
}
|
||||
|
||||
if (this.nodeInfo.checkLatest) {
|
||||
|
@ -63,22 +63,28 @@ export default class OfficialBuilds extends BaseDistribution {
|
|||
if (toolPath) {
|
||||
core.info(`Found in cache @ ${toolPath}`);
|
||||
} else {
|
||||
let downloadPath = '';
|
||||
try {
|
||||
core.info(`Attempting to download ${this.nodeInfo.versionSpec}...`);
|
||||
const osArch = this.translateArchToDistUrl(this.nodeInfo.arch);
|
||||
const versionInfo = await this.getInfoFromManifest(
|
||||
this.nodeInfo.versionSpec,
|
||||
this.nodeInfo.arch,
|
||||
osArch,
|
||||
manifest
|
||||
);
|
||||
if (versionInfo) {
|
||||
core.info(
|
||||
`Acquiring ${versionInfo.resolvedVersion} - ${versionInfo.arch} from ${versionInfo.downloadUrl}`
|
||||
);
|
||||
toolPath = await tc.downloadTool(
|
||||
downloadPath = await tc.downloadTool(
|
||||
versionInfo.downloadUrl,
|
||||
undefined,
|
||||
this.nodeInfo.auth
|
||||
);
|
||||
|
||||
if (downloadPath) {
|
||||
toolPath = await this.extractArchive(downloadPath, versionInfo);
|
||||
}
|
||||
} else {
|
||||
core.info(
|
||||
'Not found in manifest. Falling back to download directly from Node'
|
||||
|
@ -100,11 +106,19 @@ export default class OfficialBuilds extends BaseDistribution {
|
|||
core.info('Falling back to download directly from Node');
|
||||
}
|
||||
|
||||
const nodeVersions = await this.getNodejsVersions();
|
||||
const versions = this.filterVersions(nodeVersions);
|
||||
const evaluatedVersion = this.evaluateVersions(versions);
|
||||
const toolName = this.getNodejsDistInfo(evaluatedVersion, this.osPlat);
|
||||
toolPath = await this.downloadNodejs(toolName);
|
||||
if (!toolPath) {
|
||||
const nodeVersions = await this.getNodejsVersions();
|
||||
core.info('came here undefined');
|
||||
const versions = this.filterVersions(nodeVersions);
|
||||
const evaluatedVersion = this.evaluateVersions(versions);
|
||||
if (!evaluatedVersion) {
|
||||
throw new Error(
|
||||
`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`
|
||||
);
|
||||
}
|
||||
const toolName = this.getNodejsDistInfo(evaluatedVersion, this.osPlat);
|
||||
toolPath = await this.downloadNodejs(toolName);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.osPlat != 'win32') {
|
||||
|
@ -204,7 +218,7 @@ export default class OfficialBuilds extends BaseDistribution {
|
|||
|
||||
private async resolveVersionFromManifest(
|
||||
versionSpec: string,
|
||||
osArch: string = this.translateArchToDistUrl(os.arch()),
|
||||
osArch: string,
|
||||
manifest: tc.IToolRelease[] | undefined
|
||||
): Promise<string | undefined> {
|
||||
try {
|
||||
|
|
|
@ -23,7 +23,7 @@ export default class CanaryBuild extends BaseDistribution {
|
|||
|
||||
return prerelease[0].includes('v8-canary');
|
||||
});
|
||||
|
||||
localVersionPaths.sort(semver.rcompare);
|
||||
const localVersion = this.evaluateVersions(localVersionPaths);
|
||||
if (localVersion) {
|
||||
toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
|
||||
|
|
71
src/main.ts
71
src/main.ts
|
@ -1,5 +1,4 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
|
@ -9,6 +8,7 @@ import * as path from 'path';
|
|||
import {restoreCache} from './cache-restore';
|
||||
import {isCacheFeatureAvailable} from './cache-utils';
|
||||
import {getNodejsDistribution} from './distibutions/installer-factory';
|
||||
import {parseNodeVersionFile, printEnvDetailsAndSetOutput} from './util';
|
||||
|
||||
export async function run() {
|
||||
try {
|
||||
|
@ -40,16 +40,12 @@ export async function run() {
|
|||
(core.getInput('check-latest') || 'false').toUpperCase() === 'TRUE';
|
||||
const nodejsInfo = {
|
||||
versionSpec: version,
|
||||
checkLatest: checkLatest,
|
||||
checkLatest,
|
||||
auth,
|
||||
arch: arch
|
||||
arch
|
||||
};
|
||||
const nodeDistribution = getNodejsDistribution(nodejsInfo);
|
||||
if (nodeDistribution) {
|
||||
await nodeDistribution?.getNodeJsInfo();
|
||||
} else {
|
||||
throw new Error(`Could not resolve version: ${version} for build`);
|
||||
}
|
||||
await nodeDistribution.getNodeJsInfo();
|
||||
}
|
||||
|
||||
await printEnvDetailsAndSetOutput();
|
||||
|
@ -111,62 +107,3 @@ function resolveVersionInput(): string {
|
|||
|
||||
return version;
|
||||
}
|
||||
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
export async function printEnvDetailsAndSetOutput() {
|
||||
core.startGroup('Environment details');
|
||||
|
||||
const promises = ['node', 'npm', 'yarn'].map(async tool => {
|
||||
const output = await getToolVersion(tool, ['--version']);
|
||||
|
||||
if (tool === 'node') {
|
||||
core.setOutput(`${tool}-version`, output);
|
||||
}
|
||||
|
||||
core.info(`${tool}: ${output}`);
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
async function getToolVersion(tool: string, options: string[]) {
|
||||
try {
|
||||
const {stdout, stderr, exitCode} = await exec.getExecOutput(tool, options, {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
});
|
||||
|
||||
if (exitCode > 0) {
|
||||
core.warning(`[warning]${stderr}`);
|
||||
return '';
|
||||
}
|
||||
|
||||
return stdout.trim();
|
||||
} catch (err) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
63
src/util.ts
Normal file
63
src/util.ts
Normal file
|
@ -0,0 +1,63 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
export async function printEnvDetailsAndSetOutput() {
|
||||
core.startGroup('Environment details');
|
||||
|
||||
const promises = ['node', 'npm', 'yarn'].map(async tool => {
|
||||
const output = await getToolVersion(tool, ['--version']);
|
||||
|
||||
return {tool, output};
|
||||
});
|
||||
|
||||
const tools = await Promise.all(promises);
|
||||
tools.forEach(({tool, output}) => {
|
||||
if (tool === 'node') {
|
||||
core.setOutput(`${tool}-version`, output);
|
||||
}
|
||||
core.info(`${tool}: ${output}`);
|
||||
});
|
||||
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
async function getToolVersion(tool: string, options: string[]) {
|
||||
try {
|
||||
const {stdout, stderr, exitCode} = await exec.getExecOutput(tool, options, {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
});
|
||||
|
||||
if (exitCode > 0) {
|
||||
core.info(`[warning]${stderr}`);
|
||||
return '';
|
||||
}
|
||||
|
||||
return stdout.trim();
|
||||
} catch (err) {
|
||||
return '';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue