mirror of
https://code.forgejo.org/actions/setup-node.git
synced 2025-06-09 05:31:11 +00:00
Expand node version file support to include lts based codenames
This commit is contained in:
parent
c3812bd36a
commit
a0d376d3fa
7 changed files with 311 additions and 43 deletions
|
@ -1,21 +1,12 @@
|
|||
import os = require('os');
|
||||
import * as assert from 'assert';
|
||||
import * as core from '@actions/core';
|
||||
import * as hc from '@actions/http-client';
|
||||
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 = require('fs');
|
||||
|
||||
//
|
||||
// Node versions interface
|
||||
// see https://nodejs.org/dist/index.json
|
||||
//
|
||||
export interface INodeVersion {
|
||||
version: string;
|
||||
files: string[];
|
||||
}
|
||||
import {INodeVersion, getVersionsFromDist} from './node-version';
|
||||
|
||||
interface INodeVersionInfo {
|
||||
downloadUrl: string;
|
||||
|
@ -274,7 +265,7 @@ async function queryDistForMatch(versionSpec: string): Promise<string> {
|
|||
}
|
||||
|
||||
let versions: string[] = [];
|
||||
let nodeVersions = await module.exports.getVersionsFromDist();
|
||||
let nodeVersions = await getVersionsFromDist();
|
||||
|
||||
nodeVersions.forEach((nodeVersion: INodeVersion) => {
|
||||
// ensure this version supports your os and platform
|
||||
|
@ -288,16 +279,6 @@ async function queryDistForMatch(versionSpec: string): Promise<string> {
|
|||
return version;
|
||||
}
|
||||
|
||||
export async function getVersionsFromDist(): Promise<INodeVersion[]> {
|
||||
let dataUrl = 'https://nodejs.org/dist/index.json';
|
||||
let httpClient = new hc.HttpClient('setup-node', [], {
|
||||
allowRetries: true,
|
||||
maxRetries: 3
|
||||
});
|
||||
let response = await httpClient.getJson<INodeVersion[]>(dataUrl);
|
||||
return response.result || [];
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as auth from './authutil';
|
|||
import fs = require('fs');
|
||||
import * as path from 'path';
|
||||
import {URL} from 'url';
|
||||
import {parseNodeVersionFile} from './node-version-file';
|
||||
|
||||
export async function run() {
|
||||
try {
|
||||
|
@ -21,7 +22,9 @@ export async function run() {
|
|||
|
||||
if (!!versionFile) {
|
||||
const versionFilePath = path.join(__dirname, '..', versionFile);
|
||||
version = fs.readFileSync(versionFilePath, 'utf8');
|
||||
version = await parseNodeVersionFile(
|
||||
fs.readFileSync(versionFilePath, 'utf8')
|
||||
);
|
||||
core.info(`Resolved ${versionFile} as ${version}`);
|
||||
}
|
||||
}
|
||||
|
|
65
src/node-version-file.ts
Normal file
65
src/node-version-file.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import * as semvar from 'semver';
|
||||
import {INodeVersion, getVersionsFromDist} from './node-version';
|
||||
|
||||
export async function parseNodeVersionFile(contents: string): Promise<string> {
|
||||
contents = contents.trim();
|
||||
|
||||
if (/^v\d/.test(contents)) {
|
||||
contents = contents.substring(1);
|
||||
}
|
||||
|
||||
const nodeVersions = await getVersionsFromDist();
|
||||
|
||||
let nodeVersion: string;
|
||||
|
||||
if (contents.startsWith('lts/')) {
|
||||
nodeVersion = findLatestLts(nodeVersions, contents).version;
|
||||
} else if (semvar.valid(contents) || isPartialMatch(contents)) {
|
||||
nodeVersion = contents;
|
||||
} else {
|
||||
throw new Error(`Couldn't resolve node version: '${contents}'`);
|
||||
}
|
||||
|
||||
return stripVPrefix(nodeVersion);
|
||||
}
|
||||
|
||||
function findLatestLts(
|
||||
nodeVersions: INodeVersion[],
|
||||
codename: string
|
||||
): INodeVersion {
|
||||
let nodeVersion: INodeVersion | undefined;
|
||||
|
||||
if (codename === 'lts/*') {
|
||||
nodeVersion = nodeVersions.reduce((latest, nodeVersion) => {
|
||||
if (!nodeVersion.lts) {
|
||||
return latest;
|
||||
}
|
||||
|
||||
return semvar.gt(nodeVersion.version, latest.version)
|
||||
? nodeVersion
|
||||
: latest;
|
||||
});
|
||||
} else {
|
||||
codename = codename.replace('lts/', '').toLowerCase();
|
||||
|
||||
nodeVersion = nodeVersions.find(
|
||||
nodeVersion => `${nodeVersion.lts}`.toLowerCase() === codename
|
||||
);
|
||||
}
|
||||
|
||||
if (!nodeVersion) {
|
||||
throw new Error(
|
||||
`Couldn't find matching release for codename: '${codename}'`
|
||||
);
|
||||
}
|
||||
|
||||
return nodeVersion;
|
||||
}
|
||||
|
||||
function isPartialMatch(version: string): boolean {
|
||||
return /^\d+(\.\d+(\.\d+)?)?$/.test(version);
|
||||
}
|
||||
|
||||
function stripVPrefix(version: string): string {
|
||||
return /^v\d/.test(version) ? version.substring(1) : version;
|
||||
}
|
21
src/node-version.ts
Normal file
21
src/node-version.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import * as hc from '@actions/http-client';
|
||||
|
||||
//
|
||||
// Node versions interface
|
||||
// see https://nodejs.org/dist/index.json
|
||||
//
|
||||
export interface INodeVersion {
|
||||
version: string;
|
||||
files: string[];
|
||||
lts: boolean | string;
|
||||
}
|
||||
|
||||
export async function getVersionsFromDist(): Promise<INodeVersion[]> {
|
||||
let dataUrl = 'https://nodejs.org/dist/index.json';
|
||||
let httpClient = new hc.HttpClient('setup-node', [], {
|
||||
allowRetries: true,
|
||||
maxRetries: 3
|
||||
});
|
||||
let response = await httpClient.getJson<INodeVersion[]>(dataUrl);
|
||||
return response.result || [];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue