Refactored code and removed redundant logic

This commit is contained in:
La'Kaleigh Harris 2021-10-01 19:16:00 +00:00 committed by GitHub
parent bca281829f
commit 82441b3f82
4 changed files with 50 additions and 89 deletions

View file

@ -2,11 +2,18 @@ import os = require('os');
import * as assert from 'assert';
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as hc from '@actions/http-client';
import * as tc from '@actions/tool-cache';
import * as path from 'path';
import * as semver from 'semver';
import fs = require('fs');
import {INodeVersion, getVersionsFromDist} from './node-version';
interface INodeVersion {
version: string;
files: string[];
}
interface INodeVersionInfo {
downloadUrl: string;
@ -376,6 +383,16 @@ async function queryDistForMatch(
return version;
}
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
@ -445,3 +462,32 @@ function translateArchToDistUrl(arch: string): string {
return arch;
}
}
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 (semver.valid(contents) || isPartialMatch(contents)) {
nodeVersion = contents;
} else {
throw new Error(`Couldn't resolve node version: '${contents}'`);
}
return stripVPrefix(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;
}