-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_cfg.js
executable file
·58 lines (46 loc) · 1.47 KB
/
init_cfg.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
#!/bin/env node
const inquirer = require('inquirer')
const fs = require('fs')
const path = require('path')
const mysql = require('promise-mysql')
const CONFIG_FILENAME = 'db_config.js'
const CONFIG_PATH = path.resolve(__dirname, CONFIG_FILENAME)
if (!process.version.includes('v7') && !process.version.includes('v8')) {
console.log('Required node v7 or better')
process.abort()
}
if (fs.existsSync(CONFIG_PATH)) {
fs.unlinkSync(CONFIG_PATH)
}
createConfig()
async function createConfig() {
try {
const data = await getUserData()
console.log('Trying to connect to db')
const connection = await mysql.createConnection(data)
console.log('Connection successful')
console.log('Check if users exists')
const users = await connection.query('SELECT * FROM users LIMIT 1')
if (users.length === 0) {
throw new Error('Cannot find any users')
} else {
console.log('Users found. DB is correct')
}
fs.writeFileSync(CONFIG_PATH, 'module.exports=' + JSON.stringify(data))
console.log(`Config file ${CONFIG_FILENAME} successfully created`)
process.exit(1)
}
catch (e) {
console.log(e)
console.log('aborting')
process.abort()
}
}
function getUserData() {
return inquirer.prompt([
{type: 'input', name: 'user', message: 'User login'},
{type: 'password', name: 'password', message: 'User password'},
{type: 'input', name: 'database', message: 'Database name'},
{type: 'input', name: 'host', message: 'Host'}
])
}