UNPKG

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