-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
91 lines (75 loc) · 2.32 KB
/
cli.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
import * as inquirer from 'inquirer';
import type { CheckboxQuestion } from 'inquirer';
import { spawn } from 'child_process';
import { textSync } from 'figlet';
import knex, { Knex } from 'knex';
import * as fuzzy from 'fuzzy';
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'));
console.log(textSync('Falak', { horizontalLayout: 'full' }));
// const spinner = ora(`Getting tables info from ${DB_NAME}`);
// spinner.start();
const ask = async () => {
const { database } = await inquirer.prompt({
type: 'input',
name: 'database',
message: 'What is the MySQL DB name?',
});
const { user } = await inquirer.prompt({
type: 'input',
name: 'user',
message: 'Enter usename',
});
const { password } = await inquirer.prompt({
type: 'password',
name: 'password',
message: 'Enter password',
});
const { host } = await inquirer.prompt({
type: 'input',
name: 'host',
default: '127.0.0.1',
message: 'Enter host',
});
const { port } = await inquirer.prompt({
type: 'number',
name: 'port',
default: 3306,
message: 'Enter port',
});
const mysqlQueryBuilder: Knex = knex({
client: 'mysql',
connection: { host, port, user, password, database },
});
console.log(`Getting tables info from ${database} ...`);
const [result] = await mysqlQueryBuilder.raw(
`SELECT table_name as name FROM information_schema.tables WHERE table_schema = '${database}';`
);
const names = result?.map(({ name }) => name);
const q01: Omit<CheckboxQuestion, 'type'> & {
type: 'checkbox-plus';
highlight: boolean;
searchable: true;
source: (answersSoFar: string[], input: string) => Promise<any>;
} = {
type: 'checkbox-plus',
message: 'Select table(s) to monitor',
pageSize: 10,
highlight: true,
searchable: true,
name: 'tableNames',
source: async (_: string[], input = '') =>
fuzzy.filter(input, names).map(({ original }) => original),
};
const { tableNames }: inquirer.Answers = await inquirer.prompt([q01]);
const child = spawn(`yarn`, ['falak-server'], {
shell: true,
env: { DB_TABLES: tableNames, DB_NAME: database },
});
child.stdout.on('data', (data) => {
console.log(data.toString());
});
child.stdout.on('error', (data) => {
console.log(data.toString());
});
};
ask();