-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
87 lines (75 loc) · 2.36 KB
/
database.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
// set application env
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var minimist = require('minimist');
var inquirer = require('inquirer');
var config = require('./config');
var migration = require('./data/migration');
var argv = minimist(process.argv.slice(2));
var kenxInstance;
if (process.argv.length === 2) {
process.exit(0);
}
// 初始化配置
(function() {
config();
})();
if (argv.hasOwnProperty('install')) {
var question = getQuestion('Are you sure to install database?');
responseTrue(question, function() {
// 初始化数据库
console.log('installing database...');
return migration.migrateUpFreshDb().then(function() {
console.log('install database success.');
}).catch(function(errors) {
console.log(errors);
console.log('install database failure.');
});
});
} else if (argv.hasOwnProperty('reset')) {
var question = getQuestion('Are you sure to reset database?');
responseTrue(question, function() {
// 重置数据库
console.log('reseting database...');
return migration.dropTables().then(function(data) {
console.log('deleting database...');
return migration.migrateUpFreshDb();
}).then(function() {
console.log('reset successful.');
}).catch(function(errors) {
console.log(errors);
console.log('reset database failure.');
});
});
} else if (argv.hasOwnProperty('uninstall')) {
var question = getQuestion('Are you sure to uninstall database?');
responseTrue(question, function() {
// 删除数据库
console.log('droping tables...');
return dropTables().then(function() {
console.log('droping tables success.');
}).catch(function(errors) {
console.log(errors);
console.log('uninstall database failure.');
});
});
} else {
process.exit(0);
}
function getQuestion(str) {
return {
name: 'confirm',
message: str,
default: false
};
}
function responseTrue(question, callback) {
inquirer.prompt(question, function(answers) {
if (answers.confirm === 'true') {
callback().finally(function() {
process.exit(0);
});
} else {
process.exit(0);
}
});
}