-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathparser.js
190 lines (169 loc) · 7.04 KB
/
parser.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
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
const chalk = require('chalk');
const commander = require('commander');
const Table = require('cli-table3');
const { description } = require('../package.json');
const cliConfig = {
verbose: false,
json: false
};
const parse = (data) => {
if (cliConfig.json) {
drawJSON(data);
return;
}
for (let key in data) {
if (Array.isArray(data[key])) {
console.log(`${chalk.yellow.bold.underline(key)}`);
if (typeof data[key][0] === 'object') {
drawTable(data[key]);
} else {
drawJSON(data[key]);
}
} else if (typeof data[key] === 'object') {
if (data[key]?.constructor?.name === 'BigNumber') {
console.log(`${chalk.yellow.bold(key)} : ${data[key]}`);
} else {
console.log(`${chalk.yellow.bold.underline(key)}`)
drawTable([data[key]]);
}
} else {
console.log(`${chalk.yellow.bold(key)} : ${data[key]}`);
}
}
}
const drawTable = (data) => {
if (data.length == 0) {
console.log("[]")
return;
}
// Create an object with all the keys in it
let obj = data.reduce((res, item) => ({ ...res, ...item }));
// Get those keys as an array
let keys = Object.keys(obj);
// Create an object with all keys set to the default value ''
let def = keys.reduce((result, key) => {
result[key] = '-'
return result;
}, {});
// Use object destrucuring to replace all default values with the ones we have
data = data.map((item) => ({ ...def, ...item }));
let columns = Object.keys(data[0]);
let table = new Table({
head: columns.map(c => chalk.cyan.italic.bold(c)),
chars: {
'top': ' ',
'top-mid': ' ',
'top-left': ' ',
'top-right': ' ',
'bottom': ' ',
'bottom-mid': ' ',
'bottom-left': ' ',
'bottom-right': ' ',
'left': ' ',
'left-mid': ' ',
'mid': chalk.cyan('─'),
'mid-mid': chalk.cyan('┼'),
'right': ' ',
'right-mid': ' ',
'middle': chalk.cyan('│')
}
});
data.forEach(row => {
let rowValues = [];
for (let key in row) {
if (row[key] === null) {
rowValues.push("-");
} else if (Array.isArray(row[key])) {
switch (row[key].length) {
case 1:
if (typeof row[key][0] === 'object') {
rowValues.push(`array(${row[key].length})`);
} else {
rowValues.push(row[key][0]);
}
break;
default:
rowValues.push(`array(${row[key].length})`);
break;
}
} else if (typeof row[key] === 'object') {
rowValues.push("object");
} else {
rowValues.push(row[key]);
}
}
table.push(rowValues);
});
console.log(table.toString());
}
const drawJSON = (data) => {
console.log(JSON.stringify(data, null, 2));
}
const parseError = (err) => {
if(cliConfig.verbose) {
console.error(err);
}
error(err.message);
process.exit(1)
}
const actionRunner = (fn) => {
return (...args) => fn(...args).catch(parseError);
}
const parseInteger = (value) => {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.');
}
return parsedValue;
}
const parseBool = (value) => {
if (value === 'true') return true;
if (value === 'false') return false;
throw new commander.InvalidArgumentError('Not a boolean.');
}
const log = (message) => {
console.log(`${chalk.cyan.bold("ℹ Info")} ${chalk.cyan(message ?? "")}`);
}
const success = (message) => {
console.log(`${chalk.green.bold("✓ Success")} ${chalk.green(message ?? "")}`);
}
const error = (message) => {
console.error(`${chalk.red.bold("✗ Error")} ${chalk.red(message ?? "")}`);
}
const logo = "\n _ _ _ ___ __ _____ \n \/_\\ _ __ _ ____ ___ __(_) |_ ___ \/ __\\ \/ \/ \\_ \\\n \/\/_\\\\| '_ \\| '_ \\ \\ \/\\ \/ \/ '__| | __\/ _ \\ \/ \/ \/ \/ \/ \/\\\/\n \/ _ \\ |_) | |_) \\ V V \/| | | | || __\/ \/ \/___\/ \/___\/\\\/ \/_ \n \\_\/ \\_\/ .__\/| .__\/ \\_\/\\_\/ |_| |_|\\__\\___| \\____\/\\____\/\\____\/ \n |_| |_| \n\n";
const commandDescriptions = {
"account": `The account command allows you to authenticate and manage a user account.`,
"graphql": `The graphql command allows you to query and mutate any resource type on your Appwrite server.`,
"avatars": `The avatars command aims to help you complete everyday tasks related to your app image, icons, and avatars.`,
"databases": `The databases command allows you to create structured collections of documents, query and filter lists of documents.`,
"deploy": `The deploy command provides a convenient wrapper for deploying your functions and collections.`,
"functions": `The functions command allows you view, create and manage your Cloud Functions.`,
"health": `The health command allows you to both validate and monitor your Appwrite server's health.`,
"init": `The init command helps you initialize your Appwrite project, functions and collections`,
"locale": `The locale command allows you to customize your app based on your users' location.`,
"projects": `The projects command allows you to view, create and manage your Appwrite projects.`,
"storage": `The storage command allows you to manage your project files.`,
"teams": `The teams command allows you to group users of your project and to enable them to share read and write access to your project resources`,
"users": `The users command allows you to manage your project users.`,
"client": `The client command allows you to configure your CLI`,
"login": `The login command allows you to authenticate and manage a user account.`,
"logout": `The logout command allows you to logout of your Appwrite account.`,
"console" : `The console command allows gives you access to the APIs used by the Appwrite console.`,
"assistant": `The assistant command allows you to interact with the Appwrite Assistant AI`,
"migrations": `The migrations command allows you to migrate data between services.`,
"project": `The project command is for overall project administration.`,
"proxy": `The proxy command allows you to configure behavior for your attached domains.`,
"vcs": `The vcs command allows you to interact with VCS providers and manage your code repositories.`,
"main": chalk.redBright(`${logo}${description}`),
}
module.exports = {
parse,
actionRunner,
parseInteger,
parseBool,
log,
success,
error,
commandDescriptions,
cliConfig
}