UNPKG

4.59 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Versions = void 0;
7// external
8const version_clean_1 = __importDefault(require("version-clean"));
9const version_compare_1 = __importDefault(require("version-compare"));
10const version_range_1 = __importDefault(require("version-range"));
11// local
12const version_js_1 = __importDefault(require("./version.js"));
13/** Versions */
14class Versions {
15 /** The list of listeners we will call when updates happen. */
16 listeners = [];
17 /** The list of Version instances. */
18 array = [];
19 /** Create our Versions instance */
20 constructor(versions, listeners = []) {
21 this.listeners.push(...listeners);
22 this.add(versions);
23 }
24 /** The comparator to use for sorting an array of versions. */
25 static comparator(left, right) {
26 const a = (0, version_clean_1.default)(left.version);
27 const b = (0, version_clean_1.default)(right.version);
28 if (!a) {
29 // a was an alias, put it last
30 return 1;
31 }
32 else if (!b) {
33 // b was an alias, put it last
34 return -1;
35 }
36 else {
37 // otherwise do normal sorting
38 return (0, version_compare_1.default)(a, b);
39 }
40 }
41 /** Add version(s) to the list. */
42 add(versions) {
43 if (!Array.isArray(versions))
44 versions = [versions];
45 this.array.push(...versions.map((v) => {
46 const V = new version_js_1.default(v, this.listeners);
47 return V;
48 }));
49 this.compact();
50 return this;
51 }
52 /** Get version(s) from the list. */
53 get(version) {
54 return (this.array.find((V) => V.version === version) ||
55 this.array.find((V) => V.aliases.includes(version)) ||
56 this.array.find((V) => (0, version_range_1.default)(V.version, version)) ||
57 null);
58 }
59 /** Sort our versions by our comparator. */
60 sort() {
61 this.array.sort(Versions.comparator);
62 return this;
63 }
64 /** Compact versions that share the same exact version number together. */
65 compact() {
66 const map = {};
67 this.array.forEach(function (V) {
68 if (map[V.version]) {
69 map[V.version].alias = V.alias;
70 }
71 else {
72 map[V.version] = V;
73 }
74 });
75 this.array = Object.values(map);
76 this.sort();
77 return this;
78 }
79 /** Unless every version of ours was successful, then this will be `false`. */
80 get success() {
81 const failure = this.array.some((V) => V.success === false);
82 return !failure;
83 }
84 /** Get how many versions we have. */
85 get length() {
86 return this.array.length;
87 }
88 /** Get the results of our versions as a JSON object. */
89 get json() {
90 const results = { success: true };
91 this.array.forEach(function (V) {
92 results[V.status] = (results[V.status] || []).concat(V.version);
93 if (V.success === false)
94 results.success = false;
95 });
96 return results;
97 }
98 /** An array of {@link Version.row} results for use of displaying our status as a neat table. */
99 get table() {
100 return this.array.map((V) => V.row);
101 }
102 /** An array of the {@link Version.message} results for use of displaying detailed status updates of each version. */
103 get messages() {
104 return this.array.map((V) => V.message);
105 }
106 /** Loads each of our versions. */
107 async load(compact = true) {
108 await Promise.all(this.array.map((V) => V.load()));
109 if (compact) {
110 this.compact();
111 }
112 else {
113 this.sort();
114 }
115 return this;
116 }
117 /** Installs any missing versions. */
118 async install() {
119 await Promise.all(this.array.map((V) => V.install()));
120 return this;
121 }
122 /** Runs the test for each version. */
123 async test(command, serial = false) {
124 if (!command) {
125 throw new Error('no command provided to the testen versions runner');
126 }
127 if (serial) {
128 for (const V of this.array) {
129 await V.test(command);
130 }
131 }
132 else {
133 await Promise.all(this.array.map((V) => V.test(command)));
134 }
135 return this;
136 }
137}
138exports.Versions = Versions;
139exports.default = Versions;