1 | import {GitSimulator} from "./classLibrary/GitSimulator";
|
2 | import chalk from "chalk";
|
3 | import {CyanSafe, IAutoInquire} from "./classLibrary/TargetUtil/CyanResponse";
|
4 | import * as path from "path";
|
5 | import {ExecuteCommandSimple, GenerateTemplate, Interrogate} from "./generator";
|
6 | import {FakeInquirer} from "./classLibrary/Permute/FakeInquirer";
|
7 | import {InquirerSpy} from "./classLibrary/Permute/InquirerSpy";
|
8 | import {IPermuteManager, PermuteManager} from "./classLibrary/Permute/PermuteManager";
|
9 | import {Bar} from "cli-progress";
|
10 | import {GuidResolver} from "./classLibrary/ParsingStrategy/GuidResolver";
|
11 | import * as fs from "fs";
|
12 | import * as rimraf from "rimraf";
|
13 | import {Dependency} from "./Depedency";
|
14 |
|
15 | async function Concurrent<R>(array: any[], a: ((e) => Promise<R>)): Promise<R[]> {
|
16 | let prom: Promise<R>[] = [];
|
17 | array.Each(e => {
|
18 | let x = new Promise<R>(function (resolve) {
|
19 | a(e).then(function (r: R) {
|
20 | resolve(r);
|
21 | });
|
22 | });
|
23 | prom.push(x);
|
24 | }
|
25 | );
|
26 | return Promise.all(prom);
|
27 | }
|
28 |
|
29 |
|
30 | export async function Permute(dep: Dependency, git: boolean, copyNode: boolean, from: string, to: string): Promise<string> {
|
31 | let f: string = path.resolve(process.cwd(), from);
|
32 | if (git) {
|
33 | let gitSimulate: GitSimulator = new GitSimulator(dep.core);
|
34 | f = await gitSimulate.SimulateGit(process.cwd(), from);
|
35 | console.log(chalk.blueBright(f));
|
36 | }
|
37 | let ret = "";
|
38 | try {
|
39 | let fakeInquire: IAutoInquire = new FakeInquirer(dep.autoInquirer);
|
40 | let cyanSafe: { key: string, val: CyanSafe }[] = await Spy(dep, f, to);
|
41 |
|
42 | console.info(chalk.greenBright("Completed getting all permutations!"));
|
43 | let folders: string[] = cyanSafe.Map(c => path.join(c.key, c.val.npm || ""));
|
44 |
|
45 | cyanSafe.Each(c => c.val.docs.usage.git = false).Each(c => delete c.val.npm);
|
46 |
|
47 | let logs: string[] = await Concurrent(cyanSafe, (c: { key: string, val: CyanSafe }) => StealLogs(dep, f, c, fakeInquire, copyNode));
|
48 | let JSONLog: string = path.resolve(process.cwd(), to, "logs.json");
|
49 | let dest: string = path.resolve(process.cwd(), to, "logs.txt");
|
50 | dep.util.SafeWriteFile(dest, logs.join("\n\n"));
|
51 | let obj: Map<string, boolean[]>[] = cyanSafe
|
52 | .Map(e => e.val.flags)
|
53 | .Each(e => delete e["cyan.docs"])
|
54 | .Map(e => e.AsMap() as Map<string, boolean>)
|
55 | .Map((e: Map<string, boolean>) => e.MapValue(v => dep.core.WrapArray<boolean>(v) as boolean[]));
|
56 |
|
57 | dep.util.SafeWriteFile(JSONLog, JSON.stringify(
|
58 | obj.Reduce((a, b) => {
|
59 | let values: boolean[][] = a.Values().Map((e, i) => e.concat(b.Values()[i]));
|
60 | return a.Keys().Merge(values);
|
61 | }).AsObject()
|
62 | ));
|
63 | let hasYarn = await ExecuteCommandSimple("yarn", ["-v"], "", true);
|
64 | if (hasYarn) {
|
65 | let s: boolean[] = await Concurrent(folders, (e => ExecuteCommandSimple("yarn", ["--prefer-offline"], e, true)));
|
66 | console.log(s);
|
67 | } else {
|
68 | let s: boolean[] = await Concurrent(folders, (e => ExecuteCommandSimple("npm", ["i"], e)));
|
69 | console.log(s);
|
70 | }
|
71 |
|
72 | ret = "Completed!";
|
73 | } catch (e) {
|
74 | ret = chalk.redBright(e);
|
75 | }
|
76 | if (git) {
|
77 | rimraf.sync(f);
|
78 | }
|
79 | return ret;
|
80 | }
|
81 |
|
82 | async function StealLogs(dep: Dependency, templatePath: string, settings: { key: string, val: CyanSafe }, autoInquire: IAutoInquire, copyNode: boolean): Promise<string> {
|
83 | let logger = console.log;
|
84 | let logs: string = "\n\n==== start ====\n\n";
|
85 |
|
86 | Bar.prototype.start = function () {};
|
87 | Bar.prototype.update = function () {};
|
88 | Bar.prototype.stop = function () {};
|
89 | Bar.prototype.getTotal = function () {};
|
90 |
|
91 | console.log = function (...args: any[]): any {
|
92 |
|
93 |
|
94 |
|
95 | logs += (JSON.stringify(args) + "\n").Without([
|
96 | "\\u001b",
|
97 | "[91m",
|
98 | "[92m",
|
99 | "[93m",
|
100 | "[31m",
|
101 | "[39m",
|
102 | "[96m"
|
103 | ]);
|
104 | logs += "\n";
|
105 |
|
106 | };
|
107 |
|
108 | let dest: string = settings.key;
|
109 | await GenerateTemplate(dep, templatePath, dest, settings.val, copyNode);
|
110 | logs += "\n==== end ====\n\n";
|
111 | let logFile: string = path.resolve(process.cwd(), dest, "cyanprint_logs.txt");
|
112 | let cyanDiagram: string = path.resolve(process.cwd(), dest, "cyanprint_tree.json");
|
113 | dep.util.SafeWriteFile(logFile, logs);
|
114 | dep.util.SafeWriteFile(cyanDiagram, JSON.stringify(settings.val, null, 4));
|
115 | console.log = logger;
|
116 | return logs;
|
117 | }
|
118 |
|
119 | async function Spy(dep: Dependency, f: string, to: string): Promise<{ key: string, val: CyanSafe }[]> {
|
120 | let permute: IPermuteManager = new PermuteManager(dep.core);
|
121 | let spyInquire: IAutoInquire = new InquirerSpy(dep.util, permute);
|
122 |
|
123 | let ret: { key: string, val: CyanSafe }[] = [];
|
124 |
|
125 | let flag = false;
|
126 | let count: number = 0;
|
127 | while (!permute.Completed() || !flag) {
|
128 | if (!flag) {
|
129 | permute.Start(true);
|
130 | flag = true;
|
131 | } else {
|
132 | permute.Start();
|
133 | }
|
134 | let guid: GuidResolver = new GuidResolver(dep.core);
|
135 | let dest: string = path.join(to, findPath(to, guid));
|
136 | let cyan: CyanSafe = await Interrogate(dep, spyInquire, f, dest);
|
137 | permute.End();
|
138 | ret.push({key: dest, val: cyan});
|
139 | count++;
|
140 | }
|
141 | console.log("Number of Permutations: " + count);
|
142 | return ret;
|
143 | }
|
144 |
|
145 | function findPath(to: string, guid: GuidResolver): string {
|
146 | let ret = guid.GenerateGuid();
|
147 | let check = path.resolve(process.cwd(), to, ret);
|
148 | if (fs.existsSync(check)) {
|
149 | return findPath(to, guid);
|
150 | }
|
151 | return ret;
|
152 | }
|