1 | import path from 'path';
|
2 | import {Group} from "./classLibrary/Group";
|
3 | import {Core, Kore} from "@kirinnee/core";
|
4 | import {ObjectX, Objex} from "@kirinnee/objex";
|
5 | import {Utility} from "./classLibrary/Utility";
|
6 | import chalk from "chalk";
|
7 | import {AutoMapper} from "./classLibrary/TargetUtil/AutoMapper";
|
8 | import {AutoInquire} from "./classLibrary/TargetUtil/AutoInquire";
|
9 | import {Dependency} from "./Depedency";
|
10 | import {GroupData, GroupResponse} from "./classLibrary/GroupData";
|
11 | import {InstallTemplate} from "./install";
|
12 | import fse from "fs-extra";
|
13 | import fs from "graceful-fs";
|
14 | import rimraf = require("rimraf");
|
15 |
|
16 | const core: Core = new Kore();
|
17 | core.ExtendPrimitives();
|
18 |
|
19 | const objex: Objex = new ObjectX(core);
|
20 | objex.ExtendPrimitives();
|
21 |
|
22 | const util = new Utility(core);
|
23 |
|
24 | let root = path.resolve(__dirname, '../templates');
|
25 | let group: Group = new Group(core, objex, root, util);
|
26 |
|
27 | function CreateGroup(key: string, name: string, email: string, readme: string): string {
|
28 | const content =
|
29 | `# ${name}
|
30 | *Unique Key*: ${key}
|
31 |
|
32 | # Author
|
33 | ${email}
|
34 | `;
|
35 | const success = group.Create(key, name, email, readme, content);
|
36 | if (success)
|
37 | return chalk.greenBright("The Group " + chalk.yellowBright(key) + ` (${name}) has been created!`);
|
38 | return chalk.redBright("The Group " + chalk.yellowBright(key) + ` (${name}) already exist!`);
|
39 | }
|
40 |
|
41 | async function DeleteGroup(key: string): Promise<string> {
|
42 | const autoMapper = new AutoMapper(util);
|
43 | const autoInquirer = new AutoInquire(util, autoMapper);
|
44 | const confirm =
|
45 | await autoInquirer.InquirePredicate(
|
46 | `Are you sure you want to delete Group ${key} and all templates and content with the group? This cannot be undone`
|
47 | );
|
48 | if (!confirm) return "User cancelled";
|
49 | const success = group.Delete(key);
|
50 | if (success)
|
51 | return chalk.greenBright("The Group " + chalk.yellowBright(key) + " has been deleted!");
|
52 | return chalk.redBright("The Group " + chalk.yellowBright(key) + " does not exist!");
|
53 | }
|
54 |
|
55 | function ListGroup(): string {
|
56 | return group.ListAsArray().Map(([k, v]) => `${chalk.cyanBright(k)} ( ${chalk.red(v)} )`).join("\n");
|
57 | }
|
58 |
|
59 | function ExistGroup(name: string): boolean {
|
60 | return group.Exist(name);
|
61 | }
|
62 |
|
63 | function ListTemplates(key: string): string {
|
64 | if (!group.Exist(key)) return chalk.red(`Group ${key} does not exist`);
|
65 | return group.ListTemplate(key).Map(([k, v]) => `${chalk.cyanBright(v)} ( ${chalk.red(k)} )`).join("\n");
|
66 | }
|
67 |
|
68 | async function UpdateGroup(dep, key: string): Promise<string> {
|
69 | const red = chalk.red;
|
70 | const cyan = chalk.cyanBright;
|
71 |
|
72 |
|
73 | const exist = await dep.api.GroupExist(key);
|
74 | if (!exist) return red(`Group ${key} does not exist`);
|
75 |
|
76 |
|
77 | const groupData: GroupResponse = await dep.api.GetGroupData(key);
|
78 |
|
79 |
|
80 | const target: string = path.resolve(root, key);
|
81 | const old: string = target + "_old_kirin_temp_folder";
|
82 | fse.moveSync(target, old);
|
83 | try {
|
84 | const content = await dep.api.getReadMeContent(groupData.readme);
|
85 | const success = group.Create(groupData.unique_key, groupData.display_name, groupData.author, "README.MD", content);
|
86 | if (!success) throw red("Failed to create group, possibly due to old version still existing. Please try again.");
|
87 |
|
88 | for (const e of groupData.templates) {
|
89 | console.log(cyan("========================"));
|
90 | console.log(cyan(`Installing ${e}...`));
|
91 | const out: string = await InstallTemplate(e, key, false, dep);
|
92 | console.log(out);
|
93 | }
|
94 | rimraf.sync(old);
|
95 | return cyan.greenBright(`Update of group ${key} completed!`);
|
96 | } catch (e) {
|
97 |
|
98 | rimraf.sync(target);
|
99 | fse.moveSync(old, target);
|
100 | return e;
|
101 | }
|
102 |
|
103 |
|
104 | }
|
105 |
|
106 | async function InstallGroup(dep: Dependency, key: string): Promise<string> {
|
107 | const red = chalk.red;
|
108 | const cyan = chalk.cyanBright;
|
109 |
|
110 | const exist = await dep.api.GroupExist(key);
|
111 | if (!exist) return red(`Group ${key} does not exist`);
|
112 |
|
113 |
|
114 |
|
115 | const groupData: GroupResponse = await dep.api.GetGroupData(key);
|
116 |
|
117 | if (group.Exist(groupData.unique_key)) {
|
118 | const override = await dep.autoInquirer.InquirePredicate(`Group ${key} already exist, do you want to re-install? This cannot be undone.`);
|
119 | if (!override) return red(`User cancelled`);
|
120 | const success = group.Delete(key);
|
121 | if (!success) return red("Failed to remove old version, please try again");
|
122 | }
|
123 |
|
124 |
|
125 | const content = await dep.api.getReadMeContent(groupData.readme);
|
126 | const success = group.Create(groupData.unique_key, groupData.display_name, groupData.author, "README.MD", content);
|
127 | if (!success) return red("Failed to create group, possibly due to old version still existing. Please try again.");
|
128 |
|
129 |
|
130 | for (const e of groupData.templates) {
|
131 | console.log(cyan("========================"));
|
132 | console.log(cyan(`Installing ${e}...`));
|
133 | const out: string = await InstallTemplate(e, key, false, dep);
|
134 | console.log(out);
|
135 | }
|
136 | return cyan.greenBright(`Installation of group ${key} completed!`);
|
137 | }
|
138 |
|
139 | async function PushGroup(dep: Dependency, key: string, secret: string): Promise<string> {
|
140 | if (!group.Exist(key)) return chalk.red(`Group ${key} does not exist!`);
|
141 | const groupData: GroupData = group.ObtainGroupData(key);
|
142 | const readMePath: string = path.resolve(root, key, groupData.readme);
|
143 | const readMeBinary = fs.readFileSync(readMePath);
|
144 | groupData.readme = Buffer.from(readMeBinary).toString('base64');
|
145 | try {
|
146 | await dep.api.UpdateGroup(groupData, secret);
|
147 | return chalk.greenBright(`Successfully pushed group ${key} to CyanPrint host!`);
|
148 | } catch (e) {
|
149 | return chalk.red(JSON.stringify(e.message));
|
150 | }
|
151 |
|
152 | }
|
153 |
|
154 | export {CreateGroup, DeleteGroup, ListGroup, ExistGroup, ListTemplates, InstallGroup, UpdateGroup, PushGroup};
|