1 | import {Dependency} from "./Depedency";
|
2 | import {Group} from "./classLibrary/Group";
|
3 | import path from "path";
|
4 | import chalk from "chalk";
|
5 | import {Template} from "./classLibrary/Template";
|
6 | import {TemplateResponse} from "./classLibrary/TemplateData";
|
7 | import {IFileFactory, RootFileFactory} from "./classLibrary/RootFileFactory";
|
8 | import {FileWriter} from "./classLibrary/FileWriter";
|
9 | import {Installer} from "./classLibrary/Installer";
|
10 | import fse from "fs-extra";
|
11 | import fetch, {Response} from "node-fetch"
|
12 | import {GroupData} from "./classLibrary/GroupData";
|
13 | import rimraf = require("rimraf");
|
14 |
|
15 | async function UpdateEverything(dep): Promise<string> {
|
16 | const sure = await dep.autoInquirer.InquirePredicate(`Update all templates to the latest version? This cannot be undone.`);
|
17 | if (!sure) return "User cancelled";
|
18 | let root = path.resolve(__dirname, '../templates');
|
19 | let g: Group = new Group(dep.core, dep.objex, root, dep.util);
|
20 | const groups: string[] = g.ListAsArray().Map(([_, v]) => v);
|
21 | for (const group of groups) {
|
22 | await UpdateTemplatesInGroup(dep, group, false);
|
23 | }
|
24 | return "Completed updating all templates!";
|
25 | }
|
26 |
|
27 | async function UpdateTemplatesInGroup(dep: Dependency, group: string, confirm = true): Promise<string> {
|
28 | let root = path.resolve(__dirname, '../templates');
|
29 | let g: Group = new Group(dep.core, dep.objex, root, dep.util);
|
30 |
|
31 |
|
32 | if (!g.Exist(group)) return chalk.red(`Group ${group} does not exist!`);
|
33 |
|
34 |
|
35 | if (confirm) {
|
36 | const sure = await dep.autoInquirer.InquirePredicate(`Update all templates from Group ${group} to the latest version? This cannot be undone.`);
|
37 | if (!sure) return "User cancelled";
|
38 | }
|
39 |
|
40 |
|
41 | const data: GroupData = g.ObtainGroupData(group);
|
42 | for (const key in data.templates) {
|
43 | if (data.templates.hasOwnProperty(key)) {
|
44 | const [t, s]: [string, boolean] = await UpdateTemplate(dep, key, group, false);
|
45 | if (s) {
|
46 | console.log(t);
|
47 | } else {
|
48 | console.log(chalk.red(`Failed updating template ${key}:`));
|
49 | console.log(t);
|
50 | }
|
51 |
|
52 | }
|
53 | }
|
54 | return "Done updating templates!";
|
55 | }
|
56 |
|
57 | async function UpdateTemplate(dep: Dependency, key: string, group: string, confirm: boolean = true): Promise<[string, boolean]> {
|
58 |
|
59 | const exist = await dep.api.TemplateExist(key);
|
60 | if (!exist) return [chalk.red(`Template ${key} not longer exist!`), false];
|
61 |
|
62 |
|
63 | const r: TemplateResponse = await dep.api.GetTemplateData(key);
|
64 |
|
65 |
|
66 | const resp: Response = await fetch(r.repository, {method: "HEAD"});
|
67 | if (resp.status !== 200) return [chalk.red("Repository does not exist! Please contact the owner of the template"), false];
|
68 |
|
69 | const g: Group = new Group(dep.core, dep.objex, path.resolve(__dirname, '../templates'), dep.util);
|
70 |
|
71 | if (!g.Exist(group)) return [chalk.red(`Group ${chalk.cyanBright(key)} does not exist!`), false];
|
72 |
|
73 |
|
74 | const template: Template = new Template(group, key, r.display_name, r.repository, g);
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | if (confirm) {
|
80 | const sure = await dep.autoInquirer.InquirePredicate(`Update ${key} from Group ${group} to the latest version? This cannot be undone.`);
|
81 | if (!sure) return ["User cancelled", false];
|
82 | }
|
83 |
|
84 |
|
85 | const old = template.Target + "_old";
|
86 | const oldExist = fse.existsSync(old);
|
87 | if (oldExist) rimraf.sync(old);
|
88 | await fse.move(template.Target, old);
|
89 |
|
90 | try {
|
91 |
|
92 | const fileFactory: IFileFactory = new RootFileFactory(dep.core, __dirname, path.relative(__dirname, process.cwd()), template.Template);
|
93 | const writer: FileWriter = new FileWriter(dep.util);
|
94 | const installer: Installer = new Installer(dep.core, fileFactory, dep.util, writer);
|
95 | const string = await installer.Install(template.Link, false);
|
96 | if (string !== "Installation Completed") throw string;
|
97 | if (template.Exist()) {
|
98 | const data = template.TemplateData;
|
99 | const verify = data.key === template.key && template.name == data.name && template.link === data.repo;
|
100 | if (verify) {
|
101 | const g = chalk.greenBright;
|
102 | const c = chalk.cyanBright;
|
103 | rimraf.sync(old);
|
104 | return [`${g("Successfully updated Template")} ${c(template.name)} ${g("in Group")} ${c(template.group)}`, true];
|
105 | }
|
106 | }
|
107 | const red = chalk.red;
|
108 | const cyan = chalk.cyan;
|
109 | throw red("Failed to upgrade Template ") + cyan(template.name) + red(" in Group ") + cyan(template.group) +
|
110 | red(` possibly due to the following reason
|
111 | \t- The target folder does not exist
|
112 | \t- The target git repository does not exist
|
113 | \t- The target folder or repository does not contain [cyan.config.js] in the root folder
|
114 | \t- The target folder or repository does not contain [cyan.json] in the root folder
|
115 | \t- The data in the target repository's [cyan.json] does not match with server's
|
116 | `)
|
117 | } catch (e) {
|
118 |
|
119 | rimraf.sync(template.Target);
|
120 | await fse.move(old, template.Target);
|
121 | return [e, false];
|
122 | }
|
123 | }
|
124 |
|
125 | export {UpdateTemplate, UpdateTemplatesInGroup, UpdateEverything}
|