-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathparser.js
244 lines (215 loc) · 9.41 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
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
const chalk = require('chalk');
const commander = require('commander');
const Table = require('cli-table3');
const { description } = require('../package.json');
const { globalConfig } = require("./config.js");
const os = require('os');
const Client = require("./client");
const cliConfig = {
verbose: false,
json: false,
force: false,
all: false,
ids: [],
report: false,
reportData: {}
};
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])) {
rowValues.push(JSON.stringify(row[key]));
} else if (typeof row[key] === 'object') {
rowValues.push(JSON.stringify(row[key]));
} 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.report) {
(async () => {
let appwriteVersion = 'unknown';
const endpoint = globalConfig.getEndpoint();
const isCloud = endpoint.includes('cloud.appwrite.io') ? 'Yes' : 'No';
try {
const client = new Client().setEndpoint(endpoint);
const res = await client.call('get', '/health/version');
appwriteVersion = res.version;
} catch {
}
const version = '6.1.0';
const stepsToReproduce = `Running \`appwrite ${cliConfig.reportData.data.args.join(' ')}\``;
const yourEnvironment = `CLI version: ${version}\nOperation System: ${os.type()}\nAppwrite version: ${appwriteVersion}\nIs Cloud: ${isCloud}`;
const stack = '```\n' + err.stack + '\n```';
const githubIssueUrl = new URL('https://github.com/appwrite/appwrite/issues/new');
githubIssueUrl.searchParams.append('labels', 'bug');
githubIssueUrl.searchParams.append('template', 'bug.yaml');
githubIssueUrl.searchParams.append('title', `🐛 Bug Report: ${err.message}`);
githubIssueUrl.searchParams.append('actual-behavior', `CLI Error:\n${stack}`);
githubIssueUrl.searchParams.append('steps-to-reproduce', stepsToReproduce);
githubIssueUrl.searchParams.append('environment', yourEnvironment);
log(`To report this error you can:\n - Create a support ticket in our Discord server https://appwrite.io/discord \n - Create an issue in our Github\n ${githubIssueUrl.href}\n`);
error('\n Stack Trace: \n');
console.error(err);
process.exit(1);
})()
} else {
if (cliConfig.verbose) {
console.error(err);
} else {
log('For detailed error pass the --verbose or --report flag');
error(err.message);
}
process.exit(1);
}
}
const actionRunner = (fn) => {
return (...args) => {
if (cliConfig.all && (Array.isArray(cliConfig.ids) && cliConfig.ids.length !== 0)) {
error(`The '--all' and '--id' flags cannot be used together.`);
process.exit(1);
}
return 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 warn = (message) => {
console.log(`${chalk.yellow.bold("ℹ Warning:")} ${chalk.yellow(message ?? "")}`);
}
const hint = (message) => {
console.log(`${chalk.cyan.bold("♥ Hint:")} ${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 and query and filter lists of documents.`,
"init": `The init command provides a convenient wrapper for creating and initializing projects, functions, collections, buckets, teams, and messaging-topics in Appwrite.`,
"push": `The push command provides a convenient wrapper for pushing your functions, collections, buckets, teams, and messaging-topics.`,
"run": `The run command allows you to run the project locally to allow easy development and quick debugging.`,
"functions": `The functions command allows you to view, create, and manage your Cloud Functions.`,
"health": `The health command allows you to both validate and monitor your Appwrite server's health.`,
"pull": `The pull command helps you pull your Appwrite project, functions, collections, buckets, teams, and messaging-topics`,
"locale": `The locale command allows you to customize your app based on your users' location.`,
"storage": `The storage command allows you to manage your project files.`,
"teams": `The teams command allows you to group users of your project 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 log out of your Appwrite account.`,
"whoami": `The whomai command gives information about the currently logged-in user.`,
"register": `Outputs the link to create an Appwrite account.`,
"console" : `The console command gives you access to the APIs used by the Appwrite Console.`,
"assistant": `The assistant command allows you to interact with the Appwrite Assistant AI`,
"messaging": `The messaging command allows you to manage topics and targets and send messages.`,
"migrations": `The migrations command allows you to migrate data between services.`,
"vcs": `The vcs command allows you to interact with VCS providers and manage your code repositories.`,
"main": chalk.redBright(`${logo}${description}`),
}
module.exports = {
drawTable,
parse,
actionRunner,
parseInteger,
parseBool,
log,
warn,
hint,
success,
error,
commandDescriptions,
cliConfig,
drawTable
}