-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
88 lines (82 loc) · 2.4 KB
/
Copy pathcommands.ts
File metadata and controls
88 lines (82 loc) · 2.4 KB
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
import { GraphError } from "../domain/errors";
import { callGraphTool } from "../graph/tools";
import { JsonGraphRepository, validateGraphPath } from "../storage/json";
import { LINK_VERSION } from "../version";
import { loadConfig } from "./config";
import { runUpdateCommand } from "./update";
import {
formatGraphActionHelp,
formatGraphHelp,
formatTopLevelHelp,
type CliCommand,
} from "./cli";
const noopRealtime = {
broadcast: () => {},
};
function printGraphError(error: GraphError): void {
console.error(error.message);
if (error.details !== undefined) console.error(JSON.stringify(error.details, null, 2));
}
export async function runCliCommand(command: CliCommand): Promise<number | undefined> {
try {
switch (command.kind) {
case "help":
console.log(formatTopLevelHelp());
return 0;
case "web":
return undefined;
case "validate":
return runValidateCommand();
case "seed":
return runSeedCommand();
case "update":
return await runUpdateCommand(command, { currentVersion: LINK_VERSION });
case "graph-help":
console.log(formatGraphHelp());
return 0;
case "graph-action-help":
console.log(formatGraphActionHelp(command.action));
return 0;
case "graph-action":
return runGraphActionCommand(command);
}
} catch (error) {
if (error instanceof GraphError) {
printGraphError(error);
return 1;
}
console.error(error);
return 1;
}
}
export function runValidateCommand(): number {
const config = loadConfig();
validateGraphPath(config.graphPath);
console.log(`Graph data is valid: ${config.graphPath}`);
return 0;
}
export function runSeedCommand(): number {
const config = loadConfig();
const repository = new JsonGraphRepository(config.graphPath, { seed: true });
try {
repository.getSnapshot();
console.log(`Graph seed checked: ${config.graphPath}`);
return 0;
} finally {
repository.close();
}
}
function runGraphActionCommand(command: Extract<CliCommand, { kind: "graph-action" }>): number {
const config = loadConfig();
const repository = new JsonGraphRepository(config.graphPath);
try {
const result = callGraphTool(command.action, command.args, {
repository,
realtime: noopRealtime,
});
console.log(JSON.stringify(result, null, 2));
return 0;
} finally {
repository.close();
}
}