-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·112 lines (95 loc) · 3.23 KB
/
cli.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
#!/usr/bin/env node
// config order goes cosmic > args
const { cosmiconfig } = require('cosmiconfig')
const Confirm = require('prompt-confirm')
const { writeFile } = require('fs').promises
const sqldef = require('./index')
// get a reasonable port
const getPort = args => args.port || ((args.type === 'mysql') ? 3306 : 5432)
const run = async () => {
const explorer = cosmiconfig('sqldef')
const r = (await explorer.search()) || { config: {} }
const { config = {} } = r
require('yargs')
.demandCommand()
.usage('Usage: $0 <command> [options]')
.help('?')
.alias('?', 'help')
.alias('v', 'version')
.example('$0 export --user=cool --password=mysecret --database=mydatabase', 'Save your current schema, from your mysql database, in schema.sql')
.example('$0 import --user=cool --password=mysecret --database=mydatabase', 'Update your database to match schema.sql')
.command('export', 'Export your database to a file', a => {}, async args => {
args.port = args.port || getPort(args)
const { file, type, host, database, user, password, socket, port = getPort(args) } = args
const out = await sqldef({ type, host, database, user, password, socket, port, ...config, get: true })
await writeFile(file, out)
})
.command('import', 'Import your database from a file', a => {}, async args => {
const { file, type, host, database, user, password, socket, noConfirm, port = getPort(args) } = args
if (!noConfirm) {
console.log(await sqldef({ file, type, host, database, user, password, socket, port, ...config, dry: true }))
const prompt = new Confirm('Do you want to run this?')
if (!(await prompt.run())) {
console.error('Export canceled.')
process.exit(1)
}
}
console.log(await sqldef({ file, type, host, database, user, password, socket, port, ...config }))
})
.option('f', {
alias: 'file',
describe: 'The schema file to import/export',
nargs: 1,
default: config.file || 'schema.sql'
})
.option('t', {
alias: 'type',
describe: 'The type of the database',
nargs: 1,
default: config.type || 'mysql'
})
.option('h', {
alias: 'host',
describe: 'The host of the database',
nargs: 1,
default: config.host || 'localhost'
})
.option('d', {
alias: 'database',
describe: 'The name of the database',
nargs: 1,
default: config.database || 'test'
})
.option('u', {
alias: 'user',
describe: 'The user of the database',
nargs: 1,
default: config.user || 'root'
})
.option('P', {
alias: 'password',
describe: 'The password for the database',
nargs: 1,
default: config.password || ''
})
.option('p', {
alias: 'port',
describe: 'The port of the database',
nargs: 1,
default: config.port
})
.option('s', {
alias: 'socket',
describe: 'The socket of the database (only for mysql)',
nargs: 1,
default: config.socket
})
.option('x', {
alias: 'no-confirm',
describe: "Don't confirm before running the import/export",
type: 'boolean',
default: config['no-confirm']
})
.argv
}
run()