forked from larskarbo/napchart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
169 lines (125 loc) · 3.21 KB
/
install.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
/**
This module handles database config
**/
var install = {};
install.setup = function(callback){
var prompt = require('prompt'),
logger = require('./logger.js'),
fs = require('fs'),
nconf = require('nconf');
nconf.argv()
.file({ file: 'config.json' });
var questions = [
{
name: 'mysql:host',
description: 'MySQL host',
default: nconf.get('mysql:host') || 'localhost',
required: true
},
{
name: 'mysql:port',
description: 'MySQL port',
default: nconf.get('mysql:port') || '3306'
},
{
name: 'mysql:database',
description: 'MySQL database name',
default: nconf.get('mysql:database') || 'napchart',
required: true
},
{
name: 'mysql:user',
description: 'MySQL user to access database',
default: nconf.get('mysql:user') || 'napchart'
},
{
name: 'mysql:password',
description: 'MySQL user password',
default: nconf.get('mysql:password'),
hidden: true
}
];
logger.info('Napchart Setup Triggered via Command Line');
logger.info('Welcome to Napchart!');
logger.info('This interactive setup will create a config.json file with your mysql connection credentials');
logger.info('Press enter to accept the default setting (shown in brackets).');
prompt.start();
prompt.get(questions, function (err, result) {
for(var question in result){
nconf.set(question,result[question]);
}
nconf.save(function (err) {
if(err){
logger.error(err);
}
if(nconf.get('mysql')){
logger.info('Config completed successfully');
logger.info('To create tables, please run node script --create-tables');
return callback();
}
});
});
}
install.createTables = function(callback){
var prompt = require('prompt');
var nconf = require('nconf');
var Sequelize = require('sequelize');
var sequelize;
var credentials;
var logger = require('./logger.js')
nconf.argv()
.file({ file: 'config.json' });
if(!nconf.get('mysql')){
logger.error('No credentials found. Please run node script --setup');
return callback();
}
var questions = [
{
name: 'force',
description: 'Do you want to remove existing tables before you create new ones?',
default: nconf.get('deleteTables') || 'no',
required: true,
before: function(value) {
if(value == 'yes')
return true;
else
return false;
}
}
];
logger.info('Here you can create tables for the napchart database');
prompt.start();
prompt.get(questions, function (err, result) {
logger.info('Delete tables: %s', result.force);
createTables(result.force);
});
function createTables(force){
credentials = nconf.get('mysql');
sequelize = new Sequelize(credentials.database, credentials.user, credentials.password, {
host: credentials.host,
port: credentials.port,
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
},
define: {
freezeTableName: true
}
});
var config = {
force : force
}
var feedback = sequelize.import(__dirname + '/models/feedback');
var chart = sequelize.import(__dirname + '/models/chart');
var chartitem = sequelize.import(__dirname + '/models/chartitem');
feedback.sync(config).then(function(){
chart.sync(config).then(function(){
chartitem.sync(config).then(function(){
})
})
});
}
}
module.exports = install;