-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
cli.ts
342 lines (322 loc) · 9.68 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* Copyright (c) Crew Dev.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
customPackage,
existImports,
installPackages,
} from "./handlers/handle_packages.ts";
import {
CommandNotFound,
HelpCommand,
LogHelp,
LogPackages,
updateTrex,
Version,
} from "./utils/logs.ts";
import { createPackage, getImportMap } from "./handlers/handle_files.ts";
import { flags, helpsInfo, keyWords, VERSION } from "./utils/info.ts";
import { checkDepsUpdates } from "./handlers/handler_check.ts";
import { deletepackage } from "./handlers/delete_package.ts";
import { execution } from "./handlers/handle_execution.ts";
import { Config } from "./handlers/global_configs.ts";
import { purge } from "./handlers/purge_package.ts";
import { packageTreeInfo } from "./tools/logs.ts";
import type { importMap } from "./utils/types.ts";
import { LoadingSpinner } from "./tools/logs.ts";
import { Run, Scripts } from "./commands/run.ts";
import * as colors from "fmt/colors.ts";
import { exists } from "tools-fs";
import { Spinner } from "wait";
const { bold, green, yellow, red } = colors;
async function Main() {
try {
const Args = Deno.args;
// * install some packages
if (keyWords.install.includes(Args[0])) {
// * prevent error in trex install
if (Args[1]) {
CommandNotFound({
commands: keyWords.install,
flags: [...flags.map, ...flags.nest, ...flags.pkg, ...flags.help],
});
}
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: keyWords.install,
description: "install a package",
},
flags: [
{ alias: flags.map, description: "install package from deno.land" },
{ alias: flags.nest, description: "install package from nest.land" },
{
alias: flags.pkg,
description: "install package from some repository",
},
{ alias: flags.help, description: "show command help" },
],
});
}
if (await exists(`./${Config.getConfig("importMap")}`)) {
try {
const data = (await getImportMap()) as any;
const oldPackage = existImports(data);
const newPackage = await installPackages(Args);
await createPackage({ ...oldPackage, ...newPackage }, true);
} catch (error) {
throw new Error(error).message;
}
} else {
await createPackage(await installPackages(Args), true);
}
const runJson = await Scripts();
// post install hook
if (runJson?.scripts?.postinstall) await Run("postinstall");
} // * display trex version
else if (flags.version.includes(Args[0])) {
Version(VERSION.VERSION);
} // * show help info
else if (flags.help.includes(Args[0])) {
LogHelp(helpsInfo);
} // * install a custom package
else if (flags.custom.includes(Args[0])) {
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: flags.custom,
description: "install custom package",
},
flags: [{ alias: flags.help, description: "show command help" }],
});
}
customPackage(Args);
} // * uninstall some package
else if (Args[0] === keyWords.uninstall) {
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: [keyWords.uninstall],
description: "delete a package",
},
flags: [
{
alias: flags.help,
description: "show command help",
},
],
});
}
let loading: Spinner;
const [, ...pkgs] = Args;
for (const pkg of pkgs) {
// @ts-ignore
loading = LoadingSpinner(
green(
`Removing ${bold(yellow(pkg))} from ${Config.getConfig("importMap")}`,
),
)!;
await deletepackage(pkg);
loading?.stop();
}
} // * update to lastest version of trex
else if (Args[0] === keyWords.upgrade) {
if (Args[1]) {
CommandNotFound({
commands: [keyWords.upgrade],
flags: [...flags.help, "--canary"],
});
}
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: [keyWords.upgrade],
description: "update trex",
},
flags: [
{ alias: flags.help, description: "show command help" },
{ alias: ["--canary"], description: "install from dev branch" },
],
});
}
await updateTrex();
} // * shows the dependency tree of a package
else if (Args[0] === keyWords.tree) {
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: [keyWords.tree],
description: "view dependency tree",
},
flags: [{ alias: flags.help, description: "show command help" }],
});
}
if (!Args[1]) {
throw new Error(red("you need to pass a package name")).message;
}
await packageTreeInfo(...Args);
} // * run script aliases
else if (Args[0] === keyWords.run) {
if (flags.help.includes(Args[1])) {
return HelpCommand({
command: {
alias: [keyWords.run],
description: "run a script alias in a file run.json",
},
flags: [
{ alias: flags.help, description: "show command help" },
{
alias: ["--watch", "-w"],
description: "use reboot script alias protocol (rsap)",
},
{
alias: ["-wv"],
description: "verbose output in --watch mode (rsap)",
},
],
});
}
// ignore '--watch' and '-w' in injected args
const runArgs = Args[2]?.match(/^(--watch|-w|-wv)$/)
? Args.slice(3)
: Args.slice(2);
await Run(Args[1], runArgs);
} // * purge command
else if (Args[0] === keyWords.purge) {
if (flags.help.includes(Args[1])) {
HelpCommand({
command: {
alias: [keyWords.purge],
description: "remove a package or url from cache",
},
flags: [
{
alias: flags.help,
description: "show command help",
},
],
});
}
await purge();
} // * ls command
else if (Args[0] === keyWords.ls) {
if (flags.help.includes(Args[1])) {
HelpCommand({
command: {
alias: [keyWords.ls],
description: "shows the list of installed packages",
},
flags: [
{
alias: flags.help,
description: "show command help",
},
],
});
} else {
const map = (await getImportMap<importMap>())!;
LogPackages(map?.imports, false);
}
} // * execute a cli with out install
else if (Args[0] === keyWords.exec) {
if (flags.help.includes(Args[1])) {
HelpCommand({
command: {
alias: [keyWords.exec],
description: "execute a cli tool with out install then",
},
flags: [
{
alias: flags.help,
description: "show command help",
},
{
alias: ["--perms"],
description: "specify cli permisions",
},
],
});
} else {
await execution();
}
} // * check deno.land updates
else if (Args[0] === keyWords.check) {
if (flags.help.includes(Args[1])) {
HelpCommand({
command: {
alias: [keyWords.check],
description: "check deno.land [std/x] dependencies updates",
},
flags: [
{
alias: flags.help,
description: "show command help",
},
{
alias: flags.fix,
description: "update outdate dependencies",
},
],
});
} else {
await checkDepsUpdates();
}
} // * set globals config
else if (Args[0] === keyWords.globalConfig) {
if (flags.help.includes(Args[1])) {
HelpCommand({
command: {
alias: [keyWords.globalConfig],
description: "set and get global configurations",
},
flags: [
{
alias: ["--importMap="],
description: "set import map default name",
},
{
alias: ["--getImportMap"],
description: "get current import map name configuration",
},
],
});
} else {
const flag = Args[1].trim();
if (/(--importMap=\w*).(\w*)/gim.test(flag)) {
const [, value] = flag.split("=");
Config.setConfig("importMap", value.trim());
} else if (/--getImportMap/) {
console.log(Config.getConfig("importMap"));
}
}
} // * displays help information
else {
return CommandNotFound({
commands: [
keyWords.purge,
keyWords.run,
keyWords.tree,
...keyWords.install,
keyWords.uninstall,
keyWords.upgrade,
...flags.help,
...flags.version,
keyWords.setup,
keyWords.ls,
keyWords.exec,
keyWords.check,
keyWords.globalConfig,
],
flags: [],
});
}
} catch (error) {
console.error(error)
}
}
if (import.meta.main) {
await Main();
}