-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.ts
103 lines (94 loc) · 2.38 KB
/
cmd.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
import { App } from "./mod.ts";
import { version } from "./version.ts";
import { StorageRepository } from "./repository.ts";
import { Store } from "./store.ts";
import { Config } from "./config.ts";
const defaultConfigFilePath = "dem.json";
enum SubCommandType {
Version = "version",
Init = "init",
Add = "add",
Link = "link",
Update = "update",
Remove = "remove",
Unlink = "unlink",
Ensure = "ensure",
Prune = "prune",
Alias = "alias",
Unalias = "unalias",
}
function isSubCommandType(t: string): t is SubCommandType {
const commandTypes = Object.values(SubCommandType) as string[];
return commandTypes.includes(t);
}
async function main(args: string[]): Promise<void> {
const subCmdType = args[0];
if (!subCmdType) {
const subCmdTypes = Object.values(SubCommandType).join(", ");
console.error(`sub command must be given: ${subCmdTypes}`);
return;
}
if (!isSubCommandType(subCmdType)) {
console.error(`sub command ${subCmdType} does not exist.`);
return;
}
const repo = new StorageRepository(defaultConfigFilePath);
let config: Config;
try {
config = await repo.loadConfig();
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
console.error(`failed to get config, ${e}`);
return;
}
config = {
modules: [],
aliases: {},
};
}
const store: Store = { config };
const dem = new App(store, repo);
const excludes = ["vendor", "node_modules"];
switch (subCmdType) {
case SubCommandType.Version:
console.log(`dem: ${version}`);
break;
case SubCommandType.Init:
dem.init();
break;
case SubCommandType.Add:
dem.addModule(args[1]);
break;
case SubCommandType.Link:
dem.addLink(args[1]);
break;
case SubCommandType.Update:
dem.updateModule(args[1]);
break;
case SubCommandType.Unlink:
dem.removeLink(args[1]);
break;
case SubCommandType.Remove:
dem.removeModule(args[1]);
break;
case SubCommandType.Ensure:
dem.ensure(excludes);
break;
case SubCommandType.Prune:
dem.prune(excludes);
break;
case SubCommandType.Alias:
dem.addAlias(args[1], args[2]);
break;
case SubCommandType.Unalias:
dem.removeAlias(args[1]);
break;
}
}
if (import.meta.main) {
let { args } = Deno;
if (args[0] === "--") {
args = args.slice(1);
}
main(args);
}