1 | import Rule from "./rule.js";
|
2 | import Preset from "./preset.js";
|
3 | import Provider from "./providers.js";
|
4 | import Notification from "./notification.js";
|
5 | import {Collection, lib} from "./rally-tools.js";
|
6 | import {configObject} from "./config.js";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export default class SupplyChain{
|
15 | constructor(startingRule, stopRule){
|
16 | if(startingRule){
|
17 | this.startingRule = startingRule;
|
18 | this.stopRule = stopRule
|
19 | this.remote = startingRule.remote;
|
20 | }
|
21 | }
|
22 | async downloadPresetCode(objs = this.allPresets){
|
23 | log("Downloading code... ");
|
24 | await lib.keepalive(objs.arr.map(x => () => x.downloadCode()));
|
25 | }
|
26 | async calculate(){
|
27 | log("Getting rules... ");
|
28 | this.allRules = await Rule.getAll(this.remote);
|
29 | log(this.allRules.length);
|
30 |
|
31 | log("Getting presets... ");
|
32 | this.allPresets = await Preset.getAll(this.remote);
|
33 | log(this.allPresets.length);
|
34 |
|
35 | log("Getting providers... ");
|
36 | this.allProviders = await Provider.getAll(this.remote);
|
37 | log(this.allProviders.length);
|
38 |
|
39 | log("Getting notifications... ");
|
40 | this.allNotifications = await Notification.getAll(this.remote);
|
41 | log(this.allNotifications.length);
|
42 |
|
43 | if(!this.startingRule){
|
44 | this.rules = this.allRules;
|
45 | this.presets = this.allPresets;
|
46 | this.notifications = new Collection([]);
|
47 |
|
48 | await this.downloadPresetCode();
|
49 | return
|
50 | }else{
|
51 | await this.downloadPresetCode();
|
52 | }
|
53 |
|
54 | log("Done!");
|
55 |
|
56 |
|
57 |
|
58 | write("Calculating Supply chain... ");
|
59 | log(this.startingRule.chalkPrint());
|
60 |
|
61 | let allRuleNames = this.allRules.arr.map(x => x.name).filter(x => x.length >= 4);
|
62 | let allPresetNames = this.allPresets.arr.map(x => x.name).filter(x => x.length >= 4);
|
63 | let allNotifNames = this.allNotifications.arr.map(x => x.name).filter(x => x.length >= 4);
|
64 | let requiredNotifications = new Set();
|
65 |
|
66 | let ruleQueue = [this.startingRule];
|
67 | let presetQueue = [];
|
68 | for(let currentRule of ruleQueue){
|
69 | if(currentRule === this.stopRule) continue;
|
70 | let {
|
71 | eNext, pNext, preset,
|
72 | passNotif, errorNotif, enterNotif
|
73 | } = await currentRule.resolve();
|
74 |
|
75 |
|
76 | passNotif .forEach(n => requiredNotifications.add(n));
|
77 | enterNotif.forEach(n => requiredNotifications.add(n));
|
78 | errorNotif.forEach(n => requiredNotifications.add(n));
|
79 |
|
80 | if(eNext && !ruleQueue.includes(eNext)) ruleQueue.push(eNext);
|
81 | if(pNext && !ruleQueue.includes(eNext)) ruleQueue.push(pNext);
|
82 |
|
83 | let neededPresets = preset.findStringsInCode(allPresetNames);
|
84 | neededPresets = neededPresets.map(x => this.allPresets.findByName(x));
|
85 |
|
86 | let neededRules = preset.findStringsInCode(allRuleNames);
|
87 | neededRules = neededRules.map(x => this.allRules.findByName(x));
|
88 |
|
89 | preset
|
90 | .findStringsInCode(allNotifNames)
|
91 | .map(str => this.allNotifications.findByName(str))
|
92 | .forEach(notif => requiredNotifications.add(notif));
|
93 |
|
94 | neededPresets.push(preset);
|
95 |
|
96 | for(let p of neededPresets) if(!presetQueue.includes(p)) presetQueue.push(p);
|
97 | for(let p of neededRules) if(!ruleQueue .includes(p)) ruleQueue .push(p);
|
98 |
|
99 | if(configObject.verbose){
|
100 | write(currentRule.chalkPrint(false));
|
101 | log(":");
|
102 | write(" ");
|
103 | write(preset.chalkPrint(false));
|
104 | log(":");
|
105 | write(" Pass Next: "); if(pNext) write(pNext.chalkPrint(false)); else write("None");
|
106 | log("");
|
107 | write(" Err Next: "); if(eNext) write(eNext.chalkPrint(false)); else write("None");
|
108 | log("");
|
109 | log(" Rules:");
|
110 |
|
111 | for(let p of neededRules) log(" " + p.chalkPrint(true));
|
112 |
|
113 | log(" Presets:");
|
114 | for(let p of neededPresets) log(" " + p.chalkPrint(true));
|
115 |
|
116 | log("\n");
|
117 | }
|
118 | }
|
119 |
|
120 | log("Done!")
|
121 |
|
122 | this.rules = new Collection(ruleQueue);
|
123 | this.presets = new Collection(presetQueue);
|
124 | requiredNotifications.delete(undefined);
|
125 | this.notifications = new Collection([...requiredNotifications]);
|
126 | }
|
127 | async log(){
|
128 | if(this.notifications.arr.length > 0){
|
129 | log("Required notifications: ");
|
130 | this.notifications.log();
|
131 | }
|
132 |
|
133 | if(this.rules.arr.length > 0){
|
134 | write("Required rules: ");
|
135 | log(this.rules.arr.length);
|
136 | this.rules.log();
|
137 | }
|
138 |
|
139 | if(this.presets.arr.length > 0){
|
140 | write("Required presets: ");
|
141 | log(this.presets.arr.length);
|
142 | this.presets.log();
|
143 | }
|
144 |
|
145 | if(configObject.rawOutput){
|
146 | return {presets: this.presets.arr, rules: this.rules.arr, notifications: this.notifications.arr};
|
147 | }
|
148 | }
|
149 | async deleteTo(env){
|
150 | for(let preset of this.presets){
|
151 | try{
|
152 | await preset.deleteRemoteVersion(env);
|
153 | }catch(e){log(e);}
|
154 | }
|
155 | }
|
156 | async syncTo(env){
|
157 | for(let preset of this.presets){
|
158 | try{
|
159 | await preset.save(env);
|
160 | }catch(e){log(e);}
|
161 | }
|
162 | if(this.rules.arr[0]){
|
163 | log("Starting create phase for rules")
|
164 | for(let rule of this.rules){
|
165 | try{
|
166 | await rule.saveA(env);
|
167 | }catch(e){log(e);}
|
168 | }
|
169 |
|
170 | log("OK")
|
171 | log("Starting link phase for rules");
|
172 | Rule.removeCache(env);
|
173 |
|
174 | for(let rule of this.rules){
|
175 | try{
|
176 | await rule.saveB(env);
|
177 | }catch(e){log(e);}
|
178 | }
|
179 | }
|
180 | }
|
181 | }
|