1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.Versions = void 0;
|
7 |
|
8 | const version_clean_1 = __importDefault(require("version-clean"));
|
9 | const version_compare_1 = __importDefault(require("version-compare"));
|
10 | const version_range_1 = __importDefault(require("version-range"));
|
11 |
|
12 | const version_js_1 = __importDefault(require("./version.js"));
|
13 |
|
14 | class Versions {
|
15 |
|
16 | listeners = [];
|
17 |
|
18 | array = [];
|
19 |
|
20 | constructor(versions, listeners = []) {
|
21 | this.listeners.push(...listeners);
|
22 | this.add(versions);
|
23 | }
|
24 |
|
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 |
|
30 | return 1;
|
31 | }
|
32 | else if (!b) {
|
33 |
|
34 | return -1;
|
35 | }
|
36 | else {
|
37 |
|
38 | return (0, version_compare_1.default)(a, b);
|
39 | }
|
40 | }
|
41 |
|
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 |
|
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 |
|
60 | sort() {
|
61 | this.array.sort(Versions.comparator);
|
62 | return this;
|
63 | }
|
64 |
|
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 |
|
80 | get success() {
|
81 | const failure = this.array.some((V) => V.success === false);
|
82 | return !failure;
|
83 | }
|
84 |
|
85 | get length() {
|
86 | return this.array.length;
|
87 | }
|
88 |
|
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 |
|
99 | get table() {
|
100 | return this.array.map((V) => V.row);
|
101 | }
|
102 |
|
103 | get messages() {
|
104 | return this.array.map((V) => V.message);
|
105 | }
|
106 |
|
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 |
|
118 | async install() {
|
119 | await Promise.all(this.array.map((V) => V.install()));
|
120 | return this;
|
121 | }
|
122 |
|
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 | }
|
138 | exports.Versions = Versions;
|
139 | exports.default = Versions;
|