forked from jedireza/drywall
-
Notifications
You must be signed in to change notification settings - Fork 70
/
init.js
284 lines (277 loc) · 8.41 KB
/
init.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var workflow = new (require('events').EventEmitter)();
var async = require('async');
var prompt = require('prompt');
workflow.on('collectUserInput', function(){
prompt.message = ''; prompt.delimiter = '';
async.waterfall([function(cb){
//1. admin username, email, and password
console.log('=====Create admin user=====');
var schema = {
properties: {
username: {
description: 'username',
type: 'string', // Specify the type of input to expect.
pattern: /^\w+$/,
message: 'Username must be letters',
default: 'root'
},
email: {
description: 'email',
pattern: /^[a-zA-Z0-9\-\_\.\+]+@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9\-\_]+$/,
message: 'Not a valid email address',
required: true
},
password: {
description: 'password', // Prompt displayed to the user. If not supplied name will be used.
type: 'string', // Specify the type of input to expect.
pattern: /^\w+$/, // Regular expression that input must be valid against.
message: 'Password must be letters', // Warning message to display if validation fails.
hidden: true, // If true, characters entered will not be output to console.
required: true
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
if(err){
return cb(err);
}
workflow.admin = {
username: result.username,
email: result.email,
password: result.password
};
cb();
});
}, function(cb){
//2. mongo connection
console.log('=====Setup Mongo DB=====');
var schema = {
properties: {
host: {
description: 'MongoDB host',
type: 'string', // Specify the type of input to expect.
default: 'localhost'
},
port: {
description: 'MongoDB port',
type: 'number',
default: 27017
},
database: {
description: 'MongoDB database',
type: 'string',
default: 'angular-drywall'
},
user: {
description: 'MongoDB user',
type: 'string',
default: ''
},
password: {
description: 'MongoDB password',
type: 'string',
default: '',
hidden: true
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
if(err){
return cb(err);
}
workflow.mongo = {
host: result.host,
port: result.port,
database: result.database,
user: result.user? result.user: null,
password: result.password? result.password: null
};
cb();
});
}, function(cb){
//3. smtp email server and password
console.log('=====(Optional) Set smtp server credential (to send notification email)=====');
var schema = {
properties: {
email: {
description: 'smtp username',
default: workflow.admin.email
},
password: {
description: 'smtp password',
type: 'string',
hidden: true
},
host: {
description: 'smtp server host',
type: 'string',
default: 'smtp.gmail.com'
}
}
};
prompt.start();
prompt.get(schema, function (err, result) {
if(err){
return cb(err);
}
workflow.smtp = {
email: result.email,
password: result.password,
host: result.host
};
cb();
});
}], function(err, res){
if(err){
console.log('Error collecting config info, please try again.');
process.exit(-1);
}
return workflow.emit('checkDbConnection');
})
});
workflow.on('checkDbConnection', function(){
// Connection URL
// var url = 'mongodb://localhost/angular-drwayll';
var uri = ['mongodb://'];
if(workflow.mongo.user && workflow.mongo.password){
uri = uri.concat([workflow.mongo.user, ':', workflow.mongo.password, '@']);
}
uri = uri.concat([workflow.mongo.host, ':', workflow.mongo.port, '/', workflow.mongo.database]).join('');
workflow.mongo.uri = uri;
require('mongodb').MongoClient.connect(uri, function(err, db) {
if(err){
console.log('error connecting to db, please verify Mongodb setting then try again.');
process.exit(-1);
}else if(db){
workflow.db = db;
}
return workflow.emit('initDb');
});
});
workflow.on('initDb', function(){
var db = workflow.db;
async.waterfall([function(cb){
// drop db if exists
db.dropDatabase(function(err, result){
return err? cb(err): cb();
});
}, function(cb){
// insert one admingroup doc
db.collection('admingroups').insert({ _id: 'root', name: 'Root' }, function(err, res){
return err? cb(err): cb();
});
}, function(cb){
// insert one admin doc
var admins = db.collection('admins');
admins.insert({ name: {first: 'Root', last: 'Admin', full: 'Root Admin'}, groups: ['root'] }, function(err, res){
return err? cb(err): cb();
});
}, function(cb){
// insert one account doc
db.collection('accounts').insert({isVerified: 'yes'}, function(err, res){
return err? cb(err): cb();
});
}, function(cb){
// encrypt password
var bcrypt = require('bcrypt');
bcrypt.genSalt(10, function(err, salt) {
if (err) {
return cb(err);
}
bcrypt.hash(workflow.admin.password, salt, function(err, hash) {
cb(err, hash);
});
});
}, function(hash, cb){
// insert one user doc
db.collection('admins').findOne(function(err, admin){
if(err) return cb(err);
db.collection('accounts').findOne(function(err, account){
if(err) return cb(err);
var user = {
username: workflow.admin.username,
password: hash,
isActive: 'yes',
email: workflow.admin.email,
roles: {
admin: admin._id,
account: account._id
}
};
db.collection('users').insert(user, function(err, res){
return cb(err, admin._id, account._id);
});
});
});
}, function(adminId, accountId, cb){
//patch admin
db.collection('users').findOne(function(err, user){
if(err) {
return cb(err);
}
db.collection('admins').update({_id: adminId}, {$set: { user: { id: user._id, name: user.username } }}, function(err, res){
return cb(err, accountId, user);
});
});
}, function(accountId, user, cb){
//patch account
db.collection('accounts').update({_id: accountId}, {$set: { user: { id: user._id, name: user.username }}}, function(err, res){
return err? cb(err): cb();
});
}], function(err, result){
if(err){
console.log('error initializing mongodb, please try again.');
process.exit(-1);
}
return workflow.emit('generateConfigJS');
});
});
workflow.on('generateConfigJS', function(){
async.waterfall([
function(cb){
// retrieve config.example.js from file system
require('fs').readFile('./config.example.js', {encoding: 'utf8'}, function(err, data){
return err? cb(err): cb(null, data);
});
},
function(content, cb){
// find and replace with information collected
var smtpEnabled = !!workflow.smtp.password;
var map = {
'{{MONGO_URI}}': workflow.mongo.uri,
'{{ADMIN_EMAIL}}': workflow.admin.email,
'{{SMTP_EMAIL}}': smtpEnabled? workflow.smtp.email: '',
'{{SMTP_PASSWORD}}': smtpEnabled? workflow.smtp.password: '',
'{{SMTP_HOST}}': smtpEnabled? workflow.smtp.host: ''
};
for(var key in map){
if(map.hasOwnProperty(key)){
content = content.replace(new RegExp(key, 'g'), map[key]);
}
}
cb(null, content);
},
function(content, cb){
// output config.js back to filesystem
require('fs').writeFile('./config.js', content, function(err, res){
return err? cb(err): cb();
});
}
], function(err, result){
if(err){
console.log('error generating config.js.');
process.exit(-1);
}
return workflow.emit('complete');
});
});
workflow.on('complete', function(){
if(workflow.db){
workflow.db.close();
}
console.log('=====Angular-Drywall initialization complete=====');
process.exit(0);
});
workflow.emit('collectUserInput');