setup-node/src/distributions/nightly/nightly_builds.ts

95 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-12-13 10:09:39 +01:00
import * as core from '@actions/core';
2022-12-13 15:31:03 +01:00
import * as tc from '@actions/tool-cache';
2022-12-13 10:09:39 +01:00
import semver from 'semver';
import BaseDistribution from '../base-distribution';
2022-12-22 01:09:28 +01:00
import {INodejs} from '../base-models';
2022-12-13 10:09:39 +01:00
export default class NightlyNodejs extends BaseDistribution {
2022-12-22 01:51:36 +01:00
protected distribution = 'nightly';
2022-12-13 10:09:39 +01:00
constructor(nodeInfo: INodejs) {
super(nodeInfo);
}
2022-12-27 17:44:25 +01:00
protected findVersionInHostedToolCacheDirectory(): string {
2022-12-13 16:33:20 +01:00
let toolPath = '';
2022-12-13 16:27:32 +01:00
const localVersionPaths = tc
.findAllVersions('node', this.nodeInfo.arch)
.filter(i => {
const prerelease = semver.prerelease(i);
if (!prerelease) {
return false;
}
2022-12-22 01:51:36 +01:00
return prerelease[0].includes(this.distribution);
2022-12-13 16:27:32 +01:00
});
2022-12-22 00:05:42 +01:00
localVersionPaths.sort(semver.rcompare);
2022-12-13 15:31:03 +01:00
const localVersion = this.evaluateVersions(localVersionPaths);
2022-12-13 16:33:20 +01:00
if (localVersion) {
toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
}
2022-12-13 15:31:03 +01:00
return toolPath;
}
protected evaluateVersions(versions: string[]): string {
2022-12-13 10:09:39 +01:00
let version = '';
core.debug(`evaluating ${versions.length} versions`);
const {includePrerelease, range} = this.createRangePreRelease(
2022-12-22 01:51:36 +01:00
this.nodeInfo.versionSpec
2022-12-13 10:09:39 +01:00
);
2022-12-13 12:25:27 +01:00
for (let i = 0; i < versions.length; i++) {
2022-12-13 10:46:26 +01:00
const potential: string = versions[i];
const satisfied: boolean = semver.satisfies(
2022-12-22 01:51:36 +01:00
potential.replace(this.distribution, `${this.distribution}.`),
2022-12-13 10:46:26 +01:00
range,
{
includePrerelease: includePrerelease
}
);
2022-12-13 10:09:39 +01:00
if (satisfied) {
version = potential;
break;
}
}
if (version) {
core.debug(`matched: ${version}`);
} else {
core.debug('match not found');
}
return version;
}
protected getDistributionUrl(): string {
return 'https://nodejs.org/download/nightly';
}
2022-12-22 01:51:36 +01:00
protected createRangePreRelease(versionSpec: string) {
2022-12-13 15:31:03 +01:00
let range: string;
2022-12-13 10:09:39 +01:00
const [raw, prerelease] = this.splitVersionSpec(versionSpec);
const isValidVersion = semver.valid(raw);
const rawVersion = (isValidVersion ? raw : semver.coerce(raw))!;
2022-12-22 01:51:36 +01:00
if (prerelease !== this.distribution) {
2022-12-13 15:31:03 +01:00
range = `${rawVersion}-${prerelease.replace(
2022-12-22 01:51:36 +01:00
this.distribution,
`${this.distribution}.`
2022-12-13 10:09:39 +01:00
)}`;
} else {
2022-12-22 01:51:36 +01:00
range = `${semver.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
2022-12-13 10:09:39 +01:00
}
return {range, includePrerelease: !isValidVersion};
}
2022-12-13 15:31:03 +01:00
protected splitVersionSpec(versionSpec: string) {
2022-12-13 10:09:39 +01:00
return versionSpec.split(/-(.*)/s);
}
}