UNPKG

7.84 kBPlain TextView Raw
1import program from 'commander';
2import {Utility} from "./classLibrary/Utility";
3import {InstallTemplate} from "./install";
4import chalk from 'chalk';
5import {
6 CreateGroup,
7 DeleteGroup,
8 ExistGroup,
9 InstallGroup,
10 ListGroup,
11 ListTemplates,
12 PushGroup,
13 UpdateGroup
14} from "./group";
15import {Core, Kore} from "@kirinnee/core";
16import {Try} from "./try";
17import {Permute} from "./permute";
18import {Create} from "./create";
19import {ObjectX, Objex} from "@kirinnee/objex";
20import {Dependency} from "./Depedency";
21import {ApiSdk} from "./classLibrary/sdk/ApiSdk";
22import {IAutoInquire, IAutoMapper} from "./classLibrary/TargetUtil/CyanResponse";
23import {AutoInquire} from "./classLibrary/TargetUtil/AutoInquire";
24import {AutoMapper} from "./classLibrary/TargetUtil/AutoMapper";
25import {RemoveTemplate} from "./remove";
26import {UpdateEverything, UpdateTemplate, UpdateTemplatesInGroup} from "./upgrade";
27import {GroupResponse} from "./classLibrary/GroupData";
28import {PushTemplate} from "./push";
29
30declare global {
31 interface String {
32 isFile(): boolean;
33
34 FileName(): string;
35 }
36
37 const VERSION: string;
38 const PRODUCTION: boolean;
39}
40
41const core: Core = new Kore();
42core.ExtendPrimitives();
43
44const objex: Objex = new ObjectX(core);
45objex.ExtendPrimitives();
46
47const u: Utility = new Utility(core);
48const api: ApiSdk = new ApiSdk(PRODUCTION ? "https://cyanprint.icu" : "https://cyanprint.icu");
49const autoMapper: IAutoMapper = new AutoMapper(u);
50const autoInquirer: IAutoInquire = new AutoInquire(u, autoMapper);
51
52const dep: Dependency = {
53 core,
54 objex,
55 util: u,
56 api,
57 autoInquirer,
58 autoMapper,
59};
60
61program
62 .version(VERSION);
63
64// error on unknown commands
65program
66 .on('command:*', function () {
67 console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
68 process.exit(1);
69 });
70
71const create = async function (folderName: string) {
72 let reply: string = await Create(dep, folderName);
73 console.log(reply);
74 process.exit(0);
75};
76const group = async function (action: string, key: string, name: string, email: string) {
77 switch (action.toLowerCase()) {
78 case "c":
79 case "create":
80 if (name != null && key != null && email != null) {
81 console.log(CreateGroup(key, name, email, "README.MD"));
82 } else {
83 console.log(chalk.yellowBright("Usage:") + " group create <group-key> <group-name> <author-email>");
84 }
85 break;
86 case "r":
87 case "delete":
88 if (key != null) {
89 const r = await DeleteGroup(key);
90 console.log(r);
91 } else {
92 console.log(chalk.yellowBright("Usage:") + " group delete <group-key>");
93 }
94 break;
95 case "l":
96 case "list":
97 if (key == null) {
98 console.log(ListGroup());
99 } else {
100 console.log(ListTemplates(key));
101 }
102 break;
103 case "i":
104 case "install":
105 if (key != null) {
106 const r = await InstallGroup(dep, key);
107 console.log(r)
108 } else {
109 console.log(chalk.yellowBright("Usage:") + " group install <group-key>");
110 }
111 break;
112 case "u":
113 case "update":
114 if (key != null) {
115 const r = await UpdateGroup(dep, key);
116 console.log(r);
117 } else {
118 console.log(chalk.yellowBright("Usage:") + " group update <group-key>");
119 }
120 break;
121 case "p":
122 case "push":
123 if (key != null && name != null) {
124 const r = await PushGroup(dep, key, name);
125 console.log(r);
126 } else {
127 console.log(chalk.yellowBright("Usage:") + " group push <group-key> <secret-token>");
128 }
129 break;
130 default:
131 console.log(chalk.redBright("Unknown group sub-command: available commands: \n\tcreate - creates a new group\n\tdelete - deletes a group\n\tlist - show the list of groups\n\tupdate - update the group and all its template\n\tinstall - install a group from online repository\n\tpush - pushes the group to online host"));
132 }
133 process.exit(0);
134};
135const permute = async function (from: string, to: string, cmd) {
136
137 let copyNode: boolean = cmd["copyNode"] != null;
138 let git: boolean = cmd["git"] != null;
139 let reply = await Permute(dep, git, copyNode, from, to);
140 console.log(reply);
141 process.exit(0);
142};
143const install = function (link, dir, cmd) {
144 let group: string = dir || "main";
145 let copyNode: boolean = cmd["copyNode"] != null;
146 if (ExistGroup(group)) {
147 InstallTemplate(link, group, copyNode, dep)
148 .then(reply => console.log(reply))
149 .then(() => process.exit(0));
150 } else {
151 console.log(chalk.redBright("Group ") + chalk.yellow(group) + chalk.redBright(" does not exist!"));
152 process.exit(0);
153 }
154};
155const tryRun = async function (from: string, to: string, cmd) {
156
157 let copyNode: boolean = cmd["copyNode"] != null;
158 let git: boolean = cmd["git"] != null;
159
160 let reply = await Try(dep, from, to, git, copyNode);
161 console.log(reply);
162 process.exit(0);
163};
164const remove = async function (group: string, key: string) {
165 const reply = await RemoveTemplate(dep, key, group);
166 console.log(reply);
167 process.exit(0);
168};
169
170const update = async function (group: string, key: string) {
171 if (group == null) {
172 const r = await UpdateEverything(dep);
173 console.log(r);
174 } else if (key == null) {
175 const reply = await UpdateTemplatesInGroup(dep, group);
176 console.log(reply);
177 } else {
178 const reply = await UpdateTemplate(dep, key, group);
179 console.log(reply);
180 }
181 process.exit(0);
182};
183
184program
185 .command("create <app-name>")
186 .description("create a project from installed templates")
187 .action(create);
188
189program
190 .command("c <app-name>")
191 .description("create a project from installed templates")
192 .action(create);
193
194program
195 .command("install <dir> [group]")
196 .description("install templates from git or from local machine")
197 .option("-N --copy-node", "Copies node_module to target area")
198 .action(install);
199
200
201program
202 .command("i <dir> [group]")
203 .description("install templates from git or from local machine")
204 .option("-N --copy-node", "Copies node_module to target area")
205 .action(install);
206
207program
208 .command("g <action> [key] [name] [email]")
209 .description("Group functions - [create (c), deleted (d), list (l), install (i)]")
210 .action(group);
211program
212 .command("group <action> [key] [name] [email]")
213 .description("Group functions - [create (c), deleted (d), list (l), install (i)]")
214 .action(group);
215
216program
217 .command("permute <from> <to>")
218 .description("Permute all possible outcome of the template")
219 .option("-N --copy-node", "Copies node_module to target area")
220 .option("-G --git", "Simulates a git install from local folders, the target folder needs to be a git repository")
221 .action(permute);
222
223program
224 .command("try <from> <to>")
225 .description("Try out the template")
226 .option("-N --copy-node", "Copies node_module to target area")
227 .option("-G --git", "Simulates a git install from local folders, the target folder needs to be a git repository")
228 .action(tryRun);
229
230program
231 .command("remove <group> <key>")
232 .description("Deletes or removes the template from the group")
233 .action(remove);
234
235program
236 .command("r <group> <key>")
237 .description("Deletes or removes the template from the group")
238 .action(remove);
239
240
241program
242 .command("upgrade [group] [key]")
243 .description("updates a template of a group")
244 .action(update);
245
246program
247 .command("u [group] [key]")
248 .description("updates a template of a group")
249 .action(update);
250
251program
252 .command("test")
253 .action(async () => {
254 const gd: GroupResponse = await api.GetGroupData("sample");
255 const content = await api.getReadMeContent(gd.readme);
256 console.log(content);
257 });
258
259program
260 .command("push <secret>")
261 .description("Pushes the CyanPrint template in the current directory to the repository.")
262 .action(async (secret) => {
263 if (secret == null) {
264 console.log(chalk.yellowBright("Usage:") + " push <secret>");
265 } else {
266 const r = await PushTemplate(dep, secret);
267 console.log(r);
268 }
269 });
270
271program.parse(process.argv);
272
273let NO_COMMAND_SPECIFIED = program.args.length === 0;
274
275if (NO_COMMAND_SPECIFIED) {
276 program.help();
277}