UNPKG

5.61 kBPlain TextView Raw
1import path from 'path';
2import {Group} from "./classLibrary/Group";
3import {Core, Kore} from "@kirinnee/core";
4import {ObjectX, Objex} from "@kirinnee/objex";
5import {Utility} from "./classLibrary/Utility";
6import chalk from "chalk";
7import {AutoMapper} from "./classLibrary/TargetUtil/AutoMapper";
8import {AutoInquire} from "./classLibrary/TargetUtil/AutoInquire";
9import {Dependency} from "./Depedency";
10import {GroupData, GroupResponse} from "./classLibrary/GroupData";
11import {InstallTemplate} from "./install";
12import fse from "fs-extra";
13import fs from "graceful-fs";
14import rimraf = require("rimraf");
15
16const core: Core = new Kore();
17core.ExtendPrimitives();
18
19const objex: Objex = new ObjectX(core);
20objex.ExtendPrimitives();
21
22const util = new Utility(core);
23
24let root = path.resolve(__dirname, '../templates');
25let group: Group = new Group(core, objex, root, util);
26
27function 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
41async 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
55function ListGroup(): string {
56 return group.ListAsArray().Map(([k, v]) => `${chalk.cyanBright(k)} ( ${chalk.red(v)} )`).join("\n");
57}
58
59function ExistGroup(name: string): boolean {
60 return group.Exist(name);
61}
62
63function 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
68async function UpdateGroup(dep, key: string): Promise<string> {
69 const red = chalk.red;
70 const cyan = chalk.cyanBright;
71
72 // Check if group exist
73 const exist = await dep.api.GroupExist(key);
74 if (!exist) return red(`Group ${key} does not exist`);
75
76 //Pull group data
77 const groupData: GroupResponse = await dep.api.GetGroupData(key);
78
79 //Move old data
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 //Roll back
98 rimraf.sync(target);
99 fse.moveSync(old, target);
100 return e;
101 }
102
103
104}
105
106async function InstallGroup(dep: Dependency, key: string): Promise<string> {
107 const red = chalk.red;
108 const cyan = chalk.cyanBright;
109 // Check if group exist
110 const exist = await dep.api.GroupExist(key);
111 if (!exist) return red(`Group ${key} does not exist`);
112
113
114 //Pull group data
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
139async 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
154export {CreateGroup, DeleteGroup, ListGroup, ExistGroup, ListTemplates, InstallGroup, UpdateGroup, PushGroup};