From 9eba46a481571156edad799d128f0d2ec454680c Mon Sep 17 00:00:00 2001
From: Dmitry Shibanov <shibanov-1997@inbox.ru>
Date: Tue, 13 Dec 2022 15:31:03 +0100
Subject: [PATCH] change evaluateVersions

---
 dist/setup/index.js                           | 71 ++++++-------------
 src/distibutions/base-distribution.ts         |  5 +-
 src/distibutions/nightly/nightly_builds.ts    | 29 +++++---
 .../official_builds/official_builds.ts        | 56 ++-------------
 .../official_builds/rc/rc_builds.ts           |  4 +-
 src/distibutions/v8-canary/canary_builds.ts   |  3 +-
 6 files changed, 53 insertions(+), 115 deletions(-)

diff --git a/dist/setup/index.js b/dist/setup/index.js
index a588788c..55f89f9f 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -73229,7 +73229,8 @@ class BaseDistribution {
         return __awaiter(this, void 0, void 0, function* () {
             let toolPath = this.findVersionInHoostedToolCacheDirectory();
             if (!toolPath) {
-                const versions = yield this.getNodejsVersions();
+                const nodeVersions = yield this.getNodejsVersions();
+                const versions = this.filterVersions(nodeVersions);
                 const evaluatedVersion = this.evaluateVersions(versions);
                 const toolName = this.getNodejsDistInfo(evaluatedVersion, this.osPlat);
                 toolPath = yield this.downloadNodejs(toolName);
@@ -73474,17 +73475,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
 const core = __importStar(__nccwpck_require__(2186));
+const tc = __importStar(__nccwpck_require__(7784));
 const semver_1 = __importDefault(__nccwpck_require__(5911));
 const base_distribution_1 = __importDefault(__nccwpck_require__(8653));
 class NightlyNodejs extends base_distribution_1.default {
     constructor(nodeInfo) {
         super(nodeInfo);
     }
-    evaluateVersions(nodeVersions) {
+    findVersionInHoostedToolCacheDirectory() {
+        const localVersionPaths = tc.findAllVersions('node', this.nodeInfo.arch);
+        const localVersion = this.evaluateVersions(localVersionPaths);
+        const toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
+        return toolPath;
+    }
+    evaluateVersions(versions) {
         let version = '';
-        const versions = this.filterVersions(nodeVersions);
         core.debug(`evaluating ${versions.length} versions`);
-        const { includePrerelease, range } = this.createRangePreRelease(this.nodeInfo.versionSpec, '-nightly');
+        const { includePrerelease, range } = this.createRangePreRelease(this.nodeInfo.versionSpec, 'nightly');
         for (let i = 0; i < versions.length; i++) {
             const potential = versions[i];
             const satisfied = semver_1.default.satisfies(potential.replace('nightly', 'nightly.'), range, {
@@ -73519,11 +73526,11 @@ class NightlyNodejs extends base_distribution_1.default {
         const [raw, prerelease] = this.splitVersionSpec(versionSpec);
         const isValidVersion = semver_1.default.valid(raw);
         const rawVersion = (isValidVersion ? raw : semver_1.default.coerce(raw));
-        if (`-${prerelease}` !== distribution) {
-            range = `${rawVersion}${`-${prerelease}`.replace(distribution, `${distribution}.`)}`;
+        if (prerelease !== distribution) {
+            range = `${rawVersion}-${prerelease.replace(distribution, `${distribution}.`)}`;
         }
         else {
-            range = `${semver_1.default.validRange(`^${rawVersion}${distribution}`)}-0`;
+            range = `${semver_1.default.validRange(`^${rawVersion}-${distribution}`)}-0`;
         }
         return { range, includePrerelease: !isValidVersion };
     }
@@ -73571,41 +73578,6 @@ class OfficialBuilds extends base_distribution_1.default {
     constructor(nodeInfo) {
         super(nodeInfo);
     }
-    queryDistForMatch(versionSpec, arch = os_1.default.arch(), nodeVersions) {
-        return __awaiter(this, void 0, void 0, function* () {
-            let osPlat = os_1.default.platform();
-            let osArch = this.translateArchToDistUrl(arch);
-            // node offers a json list of versions
-            let dataFileName;
-            switch (osPlat) {
-                case 'linux':
-                    dataFileName = `linux-${osArch}`;
-                    break;
-                case 'darwin':
-                    dataFileName = `osx-${osArch}-tar`;
-                    break;
-                case 'win32':
-                    dataFileName = `win-${osArch}-exe`;
-                    break;
-                default:
-                    throw new Error(`Unexpected OS '${osPlat}'`);
-            }
-            if (this.isLatestSyntax(versionSpec)) {
-                core.info(`getting latest node version...`);
-                return nodeVersions[0].version;
-            }
-            const versions = [];
-            nodeVersions.forEach((nodeVersion) => {
-                // ensure this version supports your os and platform
-                if (nodeVersion.files.indexOf(dataFileName) >= 0) {
-                    versions.push(nodeVersion.version);
-                }
-            });
-            // get the latest version that matches the version spec
-            const version = this.evaluateVersions(nodeVersions);
-            return version;
-        });
-    }
     getNodeJsInfo() {
         return __awaiter(this, void 0, void 0, function* () {
             let manifest = [];
@@ -73618,7 +73590,8 @@ class OfficialBuilds extends base_distribution_1.default {
             }
             if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
                 nodeVersions = yield this.getNodejsVersions();
-                this.nodeInfo.versionSpec = yield this.queryDistForMatch(this.nodeInfo.versionSpec, this.nodeInfo.arch, nodeVersions);
+                const versions = this.filterVersions(nodeVersions);
+                this.nodeInfo.versionSpec = this.evaluateVersions(versions);
                 core.info(`getting latest node version...`);
             }
             let toolPath = this.findVersionInHoostedToolCacheDirectory();
@@ -73645,7 +73618,8 @@ class OfficialBuilds extends base_distribution_1.default {
                     core.debug(err.stack);
                     core.info('Falling back to download directly from Node');
                 }
-                const versions = yield this.getNodejsVersions();
+                const nodeVersions = yield this.getNodejsVersions();
+                const versions = this.filterVersions(nodeVersions);
                 const evaluatedVersion = this.evaluateVersions(versions);
                 const toolName = this.getNodejsDistInfo(evaluatedVersion, this.osPlat);
                 toolPath = yield this.downloadNodejs(toolName);
@@ -73656,9 +73630,8 @@ class OfficialBuilds extends base_distribution_1.default {
             core.addPath(toolPath);
         });
     }
-    evaluateVersions(nodeVersions) {
+    evaluateVersions(versions) {
         let version = '';
-        const versions = this.filterVersions(nodeVersions);
         if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
             core.info(`getting latest node version...`);
             return versions[0];
@@ -73790,9 +73763,8 @@ class RcBuild extends base_distribution_1.default {
             return response.result || [];
         });
     }
-    evaluateVersions(nodeVersions) {
+    evaluateVersions(versions) {
         let version = '';
-        const versions = this.filterVersions(nodeVersions);
         core.debug(`evaluating ${versions.length} versions`);
         for (let i = 0; i < versions.length; i++) {
             const potential = versions[i];
@@ -73854,9 +73826,8 @@ class CanaryBuild extends base_distribution_1.default {
     getDistributionUrl() {
         return 'https://nodejs.org/download/v8-canary';
     }
-    evaluateVersions(nodeVersions) {
+    evaluateVersions(versions) {
         let version = '';
-        const versions = this.filterVersions(nodeVersions);
         core.debug(`evaluating ${versions.length} versions`);
         const { includePrerelease, range } = this.createRangePreRelease(this.nodeInfo.versionSpec, 'v8-canary');
         for (let i = 0; i < versions.length; i++) {
diff --git a/src/distibutions/base-distribution.ts b/src/distibutions/base-distribution.ts
index c898602f..f843e732 100644
--- a/src/distibutions/base-distribution.ts
+++ b/src/distibutions/base-distribution.ts
@@ -25,12 +25,13 @@ export default abstract class BaseDistribution {
 
   protected abstract getDistributionUrl(): string;
   protected abstract getNodejsVersions(): Promise<INodeVersion[]>;
-  protected abstract evaluateVersions(nodeVersions: INodeVersion[]): string;
+  protected abstract evaluateVersions(nodeVersions: string[]): string;
 
   public async getNodeJsInfo() {
     let toolPath = this.findVersionInHoostedToolCacheDirectory();
     if (!toolPath) {
-      const versions = await this.getNodejsVersions();
+      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);
diff --git a/src/distibutions/nightly/nightly_builds.ts b/src/distibutions/nightly/nightly_builds.ts
index b3a5654a..6ea3f80f 100644
--- a/src/distibutions/nightly/nightly_builds.ts
+++ b/src/distibutions/nightly/nightly_builds.ts
@@ -1,4 +1,5 @@
 import * as core from '@actions/core';
+import * as tc from '@actions/tool-cache';
 
 import semver from 'semver';
 
@@ -10,15 +11,22 @@ export default class NightlyNodejs extends BaseDistribution {
     super(nodeInfo);
   }
 
-  protected evaluateVersions(nodeVersions: INodeVersion[]): string {
+  protected findVersionInHoostedToolCacheDirectory(): string {
+    const localVersionPaths = tc.findAllVersions('node', this.nodeInfo.arch);
+    const localVersion = this.evaluateVersions(localVersionPaths);
+    const toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
+
+    return toolPath;
+  }
+
+  protected evaluateVersions(versions: string[]): string {
     let version = '';
-    const versions = this.filterVersions(nodeVersions);
 
     core.debug(`evaluating ${versions.length} versions`);
 
     const {includePrerelease, range} = this.createRangePreRelease(
       this.nodeInfo.versionSpec,
-      '-nightly'
+      'nightly'
     );
 
     for (let i = 0; i < versions.length; i++) {
@@ -57,25 +65,28 @@ export default class NightlyNodejs extends BaseDistribution {
     return response.result || [];
   }
 
-  createRangePreRelease(versionSpec: string, distribution: string = '') {
-    let range: string | undefined;
+  protected createRangePreRelease(
+    versionSpec: string,
+    distribution: string = ''
+  ) {
+    let range: string;
     const [raw, prerelease] = this.splitVersionSpec(versionSpec);
     const isValidVersion = semver.valid(raw);
     const rawVersion = (isValidVersion ? raw : semver.coerce(raw))!;
 
-    if (`-${prerelease}` !== distribution) {
-      range = `${rawVersion}${`-${prerelease}`.replace(
+    if (prerelease !== distribution) {
+      range = `${rawVersion}-${prerelease.replace(
         distribution,
         `${distribution}.`
       )}`;
     } else {
-      range = `${semver.validRange(`^${rawVersion}${distribution}`)}-0`;
+      range = `${semver.validRange(`^${rawVersion}-${distribution}`)}-0`;
     }
 
     return {range, includePrerelease: !isValidVersion};
   }
 
-  splitVersionSpec(versionSpec: string) {
+  protected splitVersionSpec(versionSpec: string) {
     return versionSpec.split(/-(.*)/s);
   }
 }
diff --git a/src/distibutions/official_builds/official_builds.ts b/src/distibutions/official_builds/official_builds.ts
index beeb3fd0..85e68efd 100644
--- a/src/distibutions/official_builds/official_builds.ts
+++ b/src/distibutions/official_builds/official_builds.ts
@@ -17,48 +17,6 @@ export default class OfficialBuilds extends BaseDistribution {
     super(nodeInfo);
   }
 
-  protected async queryDistForMatch(
-    versionSpec: string,
-    arch: string = os.arch(),
-    nodeVersions: INodeVersion[]
-  ): Promise<string> {
-    let osPlat: string = os.platform();
-    let osArch: string = this.translateArchToDistUrl(arch);
-
-    // node offers a json list of versions
-    let dataFileName: string;
-    switch (osPlat) {
-      case 'linux':
-        dataFileName = `linux-${osArch}`;
-        break;
-      case 'darwin':
-        dataFileName = `osx-${osArch}-tar`;
-        break;
-      case 'win32':
-        dataFileName = `win-${osArch}-exe`;
-        break;
-      default:
-        throw new Error(`Unexpected OS '${osPlat}'`);
-    }
-
-    if (this.isLatestSyntax(versionSpec)) {
-      core.info(`getting latest node version...`);
-      return nodeVersions[0].version;
-    }
-
-    const versions: string[] = [];
-    nodeVersions.forEach((nodeVersion: INodeVersion) => {
-      // ensure this version supports your os and platform
-      if (nodeVersion.files.indexOf(dataFileName) >= 0) {
-        versions.push(nodeVersion.version);
-      }
-    });
-
-    // get the latest version that matches the version spec
-    const version = this.evaluateVersions(nodeVersions);
-    return version;
-  }
-
   public async getNodeJsInfo() {
     let manifest: tc.IToolRelease[] = [];
     let nodeVersions: INodeVersion[] = [];
@@ -77,11 +35,9 @@ export default class OfficialBuilds extends BaseDistribution {
 
     if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
       nodeVersions = await this.getNodejsVersions();
-      this.nodeInfo.versionSpec = await this.queryDistForMatch(
-        this.nodeInfo.versionSpec,
-        this.nodeInfo.arch,
-        nodeVersions
-      );
+      const versions = this.filterVersions(nodeVersions);
+      this.nodeInfo.versionSpec = this.evaluateVersions(versions);
+
       core.info(`getting latest node version...`);
     }
 
@@ -126,7 +82,8 @@ export default class OfficialBuilds extends BaseDistribution {
         core.info('Falling back to download directly from Node');
       }
 
-      const versions = await this.getNodejsVersions();
+      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);
@@ -139,9 +96,8 @@ export default class OfficialBuilds extends BaseDistribution {
     core.addPath(toolPath);
   }
 
-  protected evaluateVersions(nodeVersions: INodeVersion[]): string {
+  protected evaluateVersions(versions: string[]): string {
     let version = '';
-    const versions = this.filterVersions(nodeVersions);
 
     if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
       core.info(`getting latest node version...`);
diff --git a/src/distibutions/official_builds/rc/rc_builds.ts b/src/distibutions/official_builds/rc/rc_builds.ts
index 6339bd42..3c95fc95 100644
--- a/src/distibutions/official_builds/rc/rc_builds.ts
+++ b/src/distibutions/official_builds/rc/rc_builds.ts
@@ -18,9 +18,9 @@ export default class RcBuild extends BaseDistribution {
     return response.result || [];
   }
 
-  protected evaluateVersions(nodeVersions: INodeVersion[]): string {
+  protected evaluateVersions(versions: string[]): string {
     let version = '';
-    const versions = this.filterVersions(nodeVersions);
+
     core.debug(`evaluating ${versions.length} versions`);
 
     for (let i = 0; i < versions.length; i++) {
diff --git a/src/distibutions/v8-canary/canary_builds.ts b/src/distibutions/v8-canary/canary_builds.ts
index 2ebf5321..d4f3118a 100644
--- a/src/distibutions/v8-canary/canary_builds.ts
+++ b/src/distibutions/v8-canary/canary_builds.ts
@@ -14,9 +14,8 @@ export default class CanaryBuild extends BaseDistribution {
     return 'https://nodejs.org/download/v8-canary';
   }
 
-  protected evaluateVersions(nodeVersions: INodeVersion[]): string {
+  protected evaluateVersions(versions: string[]): string {
     let version = '';
-    const versions = this.filterVersions(nodeVersions);
 
     core.debug(`evaluating ${versions.length} versions`);