1 | import {Installer} from "./classLibrary/Installer";
|
2 | import {IFileFactory, RootFileFactory} from "./classLibrary/RootFileFactory";
|
3 | import path from 'path';
|
4 | import fs from 'graceful-fs';
|
5 | import chalk from 'chalk';
|
6 | import {FileWriter} from "./classLibrary/FileWriter";
|
7 | import rimraf from 'rimraf';
|
8 | import {Dependency} from "./Depedency";
|
9 | import {Template} from "./classLibrary/Template";
|
10 | import {Group} from "./classLibrary/Group";
|
11 | import {TemplateResponse} from "./classLibrary/TemplateData";
|
12 | import fetch, {Response} from "node-fetch";
|
13 |
|
14 |
|
15 | export async function InstallTemplate(key: string, group: string, copyNode: boolean, dep: Dependency): Promise<string> {
|
16 |
|
17 | const fromPath: string = path.relative(__dirname, process.cwd());
|
18 |
|
19 |
|
20 | const g: Group = new Group(dep.core, dep.objex, path.resolve(__dirname, '../templates'), dep.util);
|
21 |
|
22 |
|
23 | const exist = await dep.api.TemplateExist(key);
|
24 | if (!exist) return chalk.red(`Template ${key} does not exist!`);
|
25 |
|
26 |
|
27 | const r: TemplateResponse = await dep.api.GetTemplateData(key);
|
28 |
|
29 |
|
30 | const resp: Response = await fetch(r.repository, {method: "HEAD"});
|
31 | if (resp.status !== 200) return chalk.red("Repository does not exist! Please contact the owner of the template");
|
32 |
|
33 | const template: Template = new Template(group, r.unique_key, r.display_name, r.repository, g);
|
34 |
|
35 | let fileFactory: IFileFactory = new RootFileFactory(dep.core, __dirname, fromPath, template.Template);
|
36 | if (fs.existsSync(template.Target)) {
|
37 | const overwrite = await dep.autoInquirer.InquirePredicate("The following template already exist, do you want to re-install it?");
|
38 | if (overwrite) {
|
39 | rimraf.sync(template.Target);
|
40 | return DownloadTemplate(template, copyNode, fileFactory, dep);
|
41 | } else {
|
42 | return chalk.yellow("Installation has been stopped.");
|
43 | }
|
44 | } else {
|
45 | return DownloadTemplate(template, copyNode, fileFactory, dep);
|
46 | }
|
47 | }
|
48 |
|
49 |
|
50 | async function DownloadTemplate(template: Template, copyNode: boolean, fileFactory: IFileFactory, dep: Dependency): Promise<string> {
|
51 | const writer: FileWriter = new FileWriter(dep.util);
|
52 | const installer: Installer = new Installer(dep.core, fileFactory, dep.util, writer);
|
53 | const string = await installer.Install(template.Link, copyNode);
|
54 | if (string !== "Installation Completed") return string;
|
55 | template.CreateGroupEntry();
|
56 | if (template.Exist()) {
|
57 | const data = template.TemplateData;
|
58 | const verify = data.key === template.key && template.name == data.name && template.link === data.repo;
|
59 | if (verify)
|
60 | return chalk.greenBright("Template ") +
|
61 | chalk.blueBright(template.name) +
|
62 | chalk.greenBright(" has been installed in Group ") +
|
63 | chalk.blueBright(template.group);
|
64 | }
|
65 | template.DeleteGroupEntry();
|
66 | rimraf.sync(template.Target);
|
67 | return chalk.redBright("Template ") +
|
68 | chalk.blueBright(template.name) +
|
69 | chalk.redBright(" failed to installed in Group ") +
|
70 | chalk.blueBright(template.group) +
|
71 | chalk.redBright(`, possibly due to the following reason:
|
72 | \t- The target folder does not exist
|
73 | \t- The target git repository does not exist
|
74 | \t- The target folder or repository does not contain [cyan.config.js] in the root folder
|
75 | \t- The target folder or repository does not contain [cyan.json] in the root folder
|
76 | \t- The data in the target repository's [cyan.json] does not match with server's
|
77 | `);
|
78 | }
|