From eec2a7e0d86ff7bdda3cbaf671ac70bb52ed246e Mon Sep 17 00:00:00 2001
From: Dmitry Shibanov <shibanov-1997@inbox.ru>
Date: Wed, 28 Dec 2022 15:36:07 +0100
Subject: [PATCH] minor changes

---
 dist/setup/index.js                           | 166 ++++++++++--------
 .../base-distribution-prerelease.ts           |  53 ++++++
 src/distributions/base-distribution.ts        |  37 ++--
 src/distributions/base-models.ts              |   2 +-
 src/distributions/installer-factory.ts        |   4 +-
 src/distributions/nightly/nightly_builds.ts   |  52 +-----
 .../official_builds/official_builds.ts        |   4 +-
 src/distributions/rc/rc_builds.ts             |   4 +-
 src/distributions/v8-canary/canary_builds.ts  |  10 +-
 9 files changed, 184 insertions(+), 148 deletions(-)
 create mode 100644 src/distributions/base-distribution-prerelease.ts

diff --git a/dist/setup/index.js b/dist/setup/index.js
index 0409f3bf..4c01ab52 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -73220,6 +73220,81 @@ var Outputs;
 })(Outputs = exports.Outputs || (exports.Outputs = {}));
 
 
+/***/ }),
+
+/***/ 957:
+/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
+
+"use strict";
+
+var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
+}) : (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    o[k2] = m[k];
+}));
+var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
+    Object.defineProperty(o, "default", { enumerable: true, value: v });
+}) : function(o, v) {
+    o["default"] = v;
+});
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
+    __setModuleDefault(result, mod);
+    return result;
+};
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", ({ value: true }));
+const tc = __importStar(__nccwpck_require__(7784));
+const semver_1 = __importDefault(__nccwpck_require__(5911));
+const base_distribution_1 = __importDefault(__nccwpck_require__(7));
+class BasePrereleaseNodejs extends base_distribution_1.default {
+    constructor(nodeInfo) {
+        super(nodeInfo);
+    }
+    findVersionInHostedToolCacheDirectory() {
+        let toolPath = '';
+        const localVersionPaths = tc
+            .findAllVersions('node', this.nodeInfo.arch)
+            .filter(i => {
+            const prerelease = semver_1.default.prerelease(i);
+            if (!prerelease) {
+                return false;
+            }
+            return prerelease[0].includes(this.distribution);
+        });
+        localVersionPaths.sort(semver_1.default.rcompare);
+        const localVersion = this.evaluateVersions(localVersionPaths);
+        if (localVersion) {
+            toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
+        }
+        return toolPath;
+    }
+    validRange(versionSpec) {
+        let range;
+        const [raw, prerelease] = this.splitVersionSpec(versionSpec);
+        const isValidVersion = semver_1.default.valid(raw);
+        const rawVersion = (isValidVersion ? raw : semver_1.default.coerce(raw));
+        if (prerelease !== this.distribution) {
+            range = versionSpec;
+        }
+        else {
+            range = `${semver_1.default.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
+        }
+        return { range, options: { includePrerelease: !isValidVersion } };
+    }
+    splitVersionSpec(versionSpec) {
+        return versionSpec.split(/-(.*)/s);
+    }
+}
+exports["default"] = BasePrereleaseNodejs;
+
+
 /***/ }),
 
 /***/ 7:
@@ -73281,24 +73356,15 @@ class BaseDistribution {
         return __awaiter(this, void 0, void 0, function* () {
             let nodeJsVersions;
             if (this.nodeInfo.checkLatest) {
-                nodeJsVersions = yield this.getNodeJsVersions();
-                const versions = this.filterVersions(nodeJsVersions);
-                const evaluatedVersion = this.evaluateVersions(versions);
-                if (evaluatedVersion) {
-                    this.nodeInfo.versionSpec = evaluatedVersion;
-                }
+                const evaluatedVersion = yield this.findVersionInDist(nodeJsVersions);
+                this.nodeInfo.versionSpec = evaluatedVersion;
             }
             let toolPath = this.findVersionInHostedToolCacheDirectory();
             if (toolPath) {
                 core.info(`Found in cache @ ${toolPath}`);
             }
             else {
-                nodeJsVersions = nodeJsVersions !== null && nodeJsVersions !== void 0 ? nodeJsVersions : (yield this.getNodeJsVersions());
-                const versions = this.filterVersions(nodeJsVersions);
-                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 evaluatedVersion = yield this.findVersionInDist(nodeJsVersions);
                 const toolName = this.getNodejsDistInfo(evaluatedVersion);
                 toolPath = yield this.downloadNodejs(toolName);
             }
@@ -73308,6 +73374,19 @@ class BaseDistribution {
             core.addPath(toolPath);
         });
     }
+    findVersionInDist(nodeJsVersions) {
+        return __awaiter(this, void 0, void 0, function* () {
+            if (!nodeJsVersions) {
+                nodeJsVersions = yield this.getNodeJsVersions();
+            }
+            const versions = this.filterVersions(nodeJsVersions);
+            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}.`);
+            }
+            return evaluatedVersion;
+        });
+    }
     evaluateVersions(versions) {
         let version = '';
         const { range, options } = this.validRange(this.nodeInfo.versionSpec);
@@ -73541,74 +73620,19 @@ exports.getNodejsDistribution = getNodejsDistribution;
 
 "use strict";
 
-var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
-    if (k2 === undefined) k2 = k;
-    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
-}) : (function(o, m, k, k2) {
-    if (k2 === undefined) k2 = k;
-    o[k2] = m[k];
-}));
-var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
-    Object.defineProperty(o, "default", { enumerable: true, value: v });
-}) : function(o, v) {
-    o["default"] = v;
-});
-var __importStar = (this && this.__importStar) || function (mod) {
-    if (mod && mod.__esModule) return mod;
-    var result = {};
-    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
-    __setModuleDefault(result, mod);
-    return result;
-};
 var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
-const tc = __importStar(__nccwpck_require__(7784));
-const semver_1 = __importDefault(__nccwpck_require__(5911));
-const base_distribution_1 = __importDefault(__nccwpck_require__(7));
-class NightlyNodejs extends base_distribution_1.default {
+const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957));
+class NightlyNodejs extends base_distribution_prerelease_1.default {
     constructor(nodeInfo) {
         super(nodeInfo);
         this.distribution = 'nightly';
     }
-    findVersionInHostedToolCacheDirectory() {
-        let toolPath = '';
-        const localVersionPaths = tc
-            .findAllVersions('node', this.nodeInfo.arch)
-            .filter(i => {
-            const prerelease = semver_1.default.prerelease(i);
-            if (!prerelease) {
-                return false;
-            }
-            return prerelease[0].includes(this.distribution);
-        });
-        localVersionPaths.sort(semver_1.default.rcompare);
-        const localVersion = this.evaluateVersions(localVersionPaths);
-        if (localVersion) {
-            toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
-        }
-        return toolPath;
-    }
     getDistributionUrl() {
         return 'https://nodejs.org/download/nightly';
     }
-    validRange(versionSpec) {
-        let range;
-        const [raw, prerelease] = this.splitVersionSpec(versionSpec);
-        const isValidVersion = semver_1.default.valid(raw);
-        const rawVersion = (isValidVersion ? raw : semver_1.default.coerce(raw));
-        if (prerelease !== this.distribution) {
-            range = versionSpec;
-        }
-        else {
-            range = `${semver_1.default.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
-        }
-        return { range, options: { includePrerelease: !isValidVersion } };
-    }
-    splitVersionSpec(versionSpec) {
-        return versionSpec.split(/-(.*)/s);
-    }
 }
 exports["default"] = NightlyNodejs;
 
@@ -73853,8 +73877,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
-const nightly_builds_1 = __importDefault(__nccwpck_require__(7127));
-class CanaryBuild extends nightly_builds_1.default {
+const base_distribution_prerelease_1 = __importDefault(__nccwpck_require__(957));
+class CanaryBuild extends base_distribution_prerelease_1.default {
     constructor(nodeInfo) {
         super(nodeInfo);
         this.distribution = 'v8-canary';
diff --git a/src/distributions/base-distribution-prerelease.ts b/src/distributions/base-distribution-prerelease.ts
new file mode 100644
index 00000000..f3214af0
--- /dev/null
+++ b/src/distributions/base-distribution-prerelease.ts
@@ -0,0 +1,53 @@
+import * as tc from '@actions/tool-cache';
+
+import semver from 'semver';
+
+import BaseDistribution from './base-distribution';
+import {NodeInputs} from './base-models';
+
+export default abstract class BasePrereleaseNodejs extends BaseDistribution {
+  protected abstract distribution: string;
+  constructor(nodeInfo: NodeInputs) {
+    super(nodeInfo);
+  }
+
+  protected findVersionInHostedToolCacheDirectory(): string {
+    let toolPath = '';
+    const localVersionPaths = tc
+      .findAllVersions('node', this.nodeInfo.arch)
+      .filter(i => {
+        const prerelease = semver.prerelease(i);
+        if (!prerelease) {
+          return false;
+        }
+
+        return prerelease[0].includes(this.distribution);
+      });
+    localVersionPaths.sort(semver.rcompare);
+    const localVersion = this.evaluateVersions(localVersionPaths);
+    if (localVersion) {
+      toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
+    }
+
+    return toolPath;
+  }
+
+  protected validRange(versionSpec: string) {
+    let range: string;
+    const [raw, prerelease] = this.splitVersionSpec(versionSpec);
+    const isValidVersion = semver.valid(raw);
+    const rawVersion = (isValidVersion ? raw : semver.coerce(raw))!;
+
+    if (prerelease !== this.distribution) {
+      range = versionSpec;
+    } else {
+      range = `${semver.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
+    }
+
+    return {range, options: {includePrerelease: !isValidVersion}};
+  }
+
+  protected splitVersionSpec(versionSpec: string) {
+    return versionSpec.split(/-(.*)/s);
+  }
+}
diff --git a/src/distributions/base-distribution.ts b/src/distributions/base-distribution.ts
index c1d2601f..d214d406 100644
--- a/src/distributions/base-distribution.ts
+++ b/src/distributions/base-distribution.ts
@@ -10,13 +10,13 @@ import * as path from 'path';
 import os from 'os';
 import fs from 'fs';
 
-import {INodejs, INodeVersion, INodeVersionInfo} from './base-models';
+import {NodeInputs, INodeVersion, INodeVersionInfo} from './base-models';
 
 export default abstract class BaseDistribution {
   protected httpClient: hc.HttpClient;
   protected osPlat = os.platform();
 
-  constructor(protected nodeInfo: INodejs) {
+  constructor(protected nodeInfo: NodeInputs) {
     this.httpClient = new hc.HttpClient('setup-node', [], {
       allowRetries: true,
       maxRetries: 3
@@ -28,27 +28,15 @@ export default abstract class BaseDistribution {
   public async setupNodeJs() {
     let nodeJsVersions: INodeVersion[] | undefined;
     if (this.nodeInfo.checkLatest) {
-      nodeJsVersions = await this.getNodeJsVersions();
-      const versions = this.filterVersions(nodeJsVersions);
-      const evaluatedVersion = this.evaluateVersions(versions);
-
-      if (evaluatedVersion) {
-        this.nodeInfo.versionSpec = evaluatedVersion;
-      }
+      const evaluatedVersion = await this.findVersionInDist(nodeJsVersions);
+      this.nodeInfo.versionSpec = evaluatedVersion;
     }
 
     let toolPath = this.findVersionInHostedToolCacheDirectory();
     if (toolPath) {
       core.info(`Found in cache @ ${toolPath}`);
     } else {
-      nodeJsVersions = nodeJsVersions ?? (await this.getNodeJsVersions());
-      const versions = this.filterVersions(nodeJsVersions);
-      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 evaluatedVersion = await this.findVersionInDist(nodeJsVersions);
       const toolName = this.getNodejsDistInfo(evaluatedVersion);
       toolPath = await this.downloadNodejs(toolName);
     }
@@ -60,6 +48,21 @@ export default abstract class BaseDistribution {
     core.addPath(toolPath);
   }
 
+  protected async findVersionInDist(nodeJsVersions?: INodeVersion[]) {
+    if (!nodeJsVersions) {
+      nodeJsVersions = await this.getNodeJsVersions();
+    }
+    const versions = this.filterVersions(nodeJsVersions);
+    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}.`
+      );
+    }
+
+    return evaluatedVersion;
+  }
+
   protected evaluateVersions(versions: string[]): string {
     let version = '';
 
diff --git a/src/distributions/base-models.ts b/src/distributions/base-models.ts
index 300461bb..3304ba7c 100644
--- a/src/distributions/base-models.ts
+++ b/src/distributions/base-models.ts
@@ -1,4 +1,4 @@
-export interface INodejs {
+export interface NodeInputs {
   versionSpec: string;
   arch: string;
   auth?: string;
diff --git a/src/distributions/installer-factory.ts b/src/distributions/installer-factory.ts
index 6f4c7e8f..01438cce 100644
--- a/src/distributions/installer-factory.ts
+++ b/src/distributions/installer-factory.ts
@@ -1,5 +1,5 @@
 import BaseDistribution from './base-distribution';
-import {INodejs} from './base-models';
+import {NodeInputs} from './base-models';
 import NightlyNodejs from './nightly/nightly_builds';
 import OfficialBuilds from './official_builds/official_builds';
 import RcBuild from './rc/rc_builds';
@@ -13,7 +13,7 @@ enum Distributions {
 }
 
 export function getNodejsDistribution(
-  installerOptions: INodejs
+  installerOptions: NodeInputs
 ): BaseDistribution {
   const versionSpec = installerOptions.versionSpec;
   let distribution: BaseDistribution;
diff --git a/src/distributions/nightly/nightly_builds.ts b/src/distributions/nightly/nightly_builds.ts
index 0c4a23dc..86a89eed 100644
--- a/src/distributions/nightly/nightly_builds.ts
+++ b/src/distributions/nightly/nightly_builds.ts
@@ -1,57 +1,13 @@
-import * as tc from '@actions/tool-cache';
+import BasePrereleaseNodejs from '../base-distribution-prerelease';
+import {NodeInputs} from '../base-models';
 
-import semver from 'semver';
-
-import BaseDistribution from '../base-distribution';
-import {INodejs} from '../base-models';
-
-export default class NightlyNodejs extends BaseDistribution {
+export default class NightlyNodejs extends BasePrereleaseNodejs {
   protected distribution = 'nightly';
-  constructor(nodeInfo: INodejs) {
+  constructor(nodeInfo: NodeInputs) {
     super(nodeInfo);
   }
 
-  protected findVersionInHostedToolCacheDirectory(): string {
-    let toolPath = '';
-    const localVersionPaths = tc
-      .findAllVersions('node', this.nodeInfo.arch)
-      .filter(i => {
-        const prerelease = semver.prerelease(i);
-        if (!prerelease) {
-          return false;
-        }
-
-        return prerelease[0].includes(this.distribution);
-      });
-    localVersionPaths.sort(semver.rcompare);
-    const localVersion = this.evaluateVersions(localVersionPaths);
-    if (localVersion) {
-      toolPath = tc.find('node', localVersion, this.nodeInfo.arch);
-    }
-
-    return toolPath;
-  }
-
   protected getDistributionUrl(): string {
     return 'https://nodejs.org/download/nightly';
   }
-
-  protected validRange(versionSpec: string) {
-    let range: string;
-    const [raw, prerelease] = this.splitVersionSpec(versionSpec);
-    const isValidVersion = semver.valid(raw);
-    const rawVersion = (isValidVersion ? raw : semver.coerce(raw))!;
-
-    if (prerelease !== this.distribution) {
-      range = versionSpec;
-    } else {
-      range = `${semver.validRange(`^${rawVersion}-${this.distribution}`)}-0`;
-    }
-
-    return {range, options: {includePrerelease: !isValidVersion}};
-  }
-
-  protected splitVersionSpec(versionSpec: string) {
-    return versionSpec.split(/-(.*)/s);
-  }
 }
diff --git a/src/distributions/official_builds/official_builds.ts b/src/distributions/official_builds/official_builds.ts
index 53d7e910..fa07dc96 100644
--- a/src/distributions/official_builds/official_builds.ts
+++ b/src/distributions/official_builds/official_builds.ts
@@ -3,14 +3,14 @@ import * as tc from '@actions/tool-cache';
 import path from 'path';
 
 import BaseDistribution from '../base-distribution';
-import {INodejs, INodeVersion, INodeVersionInfo} from '../base-models';
+import {NodeInputs, INodeVersion, INodeVersionInfo} from '../base-models';
 
 interface INodeRelease extends tc.IToolRelease {
   lts?: string;
 }
 
 export default class OfficialBuilds extends BaseDistribution {
-  constructor(nodeInfo: INodejs) {
+  constructor(nodeInfo: NodeInputs) {
     super(nodeInfo);
   }
 
diff --git a/src/distributions/rc/rc_builds.ts b/src/distributions/rc/rc_builds.ts
index dd57ef82..40cdb192 100644
--- a/src/distributions/rc/rc_builds.ts
+++ b/src/distributions/rc/rc_builds.ts
@@ -1,8 +1,8 @@
 import BaseDistribution from '../base-distribution';
-import {INodejs} from '../base-models';
+import {NodeInputs} from '../base-models';
 
 export default class RcBuild extends BaseDistribution {
-  constructor(nodeInfo: INodejs) {
+  constructor(nodeInfo: NodeInputs) {
     super(nodeInfo);
   }
 
diff --git a/src/distributions/v8-canary/canary_builds.ts b/src/distributions/v8-canary/canary_builds.ts
index f7656796..257151b4 100644
--- a/src/distributions/v8-canary/canary_builds.ts
+++ b/src/distributions/v8-canary/canary_builds.ts
@@ -1,10 +1,10 @@
-import {INodejs} from '../base-models';
-import NightlyNodejs from '../nightly/nightly_builds';
+import BasePrereleaseNodejs from '../base-distribution-prerelease';
+import {NodeInputs} from '../base-models';
 
-export default class CanaryBuild extends NightlyNodejs {
-  constructor(nodeInfo: INodejs) {
+export default class CanaryBuild extends BasePrereleaseNodejs {
+  protected distribution = 'v8-canary';
+  constructor(nodeInfo: NodeInputs) {
     super(nodeInfo);
-    this.distribution = 'v8-canary';
   }
 
   protected getDistributionUrl(): string {