-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (51 loc) · 1.55 KB
/
index.js
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
import parseArgs from './app/terminal/inputUtils/parseArgs.js';
import commands from './app/terminal/commands.js';
import getInputErrors from './app/terminal/inputUtils/getInputErrors.js';
import { models, db } from './singletons.js';
// #############################################
// Routes
// #############################################
const args = parseArgs(process.argv);
let command;
const error = (() => {
if (args.error) return args.error;
if (!commands[args.module]) {
return `A module called "${args.module}" does not exist.`;
}
if (!commands[args.module][args.fn]) {
return `The module "${args.module}" does not have a function named "${args.fn}".`;
}
command = commands[args.module][args.fn];
const validationErrors = Object.entries(getInputErrors(command.signature, args.args));
if (validationErrors.length) {
return `The following input data errors are present:\n${validationErrors.map(err => err.join(': ')).join('\n')}`;
}
})();
if (error) {
console.log(error);
} else {
(async () => {
const result = await command.fn({
deps: { db, models, commands },
args: args.args,
});
if (result.formattedData) {
console.log(result.formattedData);
}
if (result.data) {
let format = 'json';
if (Array.isArray(result.data)) format = 'table';
if (args.options.format) format = args.options.format;
switch (format) {
case 'table':
console.table(result.data);
break;
case 'json':
console.log(result.data);
break;
default:
console.log(`Invalid output format: "${format}"`);
}
}
})();
}