forked from jedireza/frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
executable file
·204 lines (163 loc) · 6.11 KB
/
setup.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
#!/usr/bin/env node
var Fs = require('fs');
var Path = require('path');
var Async = require('async');
var Promptly = require('promptly');
var Mongodb = require('mongodb');
var Handlebars = require('handlebars');
var configTemplatePath = Path.resolve(__dirname, 'config.example');
var configPath = Path.resolve(__dirname, 'config.js');
if (process.env.NODE_ENV === 'test') {
var options = { encoding: 'utf-8' };
var source = Fs.readFileSync(configTemplatePath, options);
var configTemplate = Handlebars.compile(source);
var context = {
projectName: 'Frame',
mongodbUrl: 'mongodb://localhost:27017/frame',
rootEmail: 'root@root',
rootPassword: 'root',
systemEmail: 'sys@tem',
smtpHost: 'smtp.gmail.com',
smtpPort: 465,
smtpUsername: '',
smtpPassword: ''
};
Fs.writeFileSync(configPath, configTemplate(context));
console.log('Setup complete.');
process.exit(0);
}
Async.auto({
projectName: function (done) {
Promptly.prompt('Project name: (Frame)', { default: 'Frame' }, done);
},
mongodbUrl: ['projectName', function (done, results) {
var promptOptions = {
default: 'mongodb://localhost:27017/frame'
};
Promptly.prompt('MongoDB URL: (mongodb://localhost:27017/frame)', promptOptions, done);
}],
testMongo: ['rootPassword', function (done, results) {
Mongodb.MongoClient.connect(results.mongodbUrl, {}, function (err, db) {
if (err) {
console.error('Failed to connect to Mongodb.');
return done(err);
}
db.close();
done(null, true);
});
}],
rootEmail: ['mongodbUrl', function (done, results) {
Promptly.prompt('Root user email:', done);
}],
rootPassword: ['rootEmail', function (done, results) {
Promptly.password('Root user password:', { default: null }, done);
}],
systemEmail: ['rootPassword', function (done, results) {
var promptOptions = {
default: results.rootEmail
};
Promptly.prompt('System email: (' + results.rootEmail + ')', promptOptions, done);
}],
smtpHost: ['systemEmail', function (done, results) {
Promptly.prompt('SMTP host: (smtp.gmail.com)', { default: 'smtp.gmail.com' }, done);
}],
smtpPort: ['smtpHost', function (done, results) {
Promptly.prompt('SMTP port: (465)', { default: 465 }, done);
}],
smtpUsername: ['smtpPort', function (done, results) {
var promptOptions = {
default: results.systemEmail
};
Promptly.prompt('SMTP username: (' + results.systemEmail + ')', promptOptions, done);
}],
smtpPassword: ['smtpUsername', function (done, results) {
Promptly.password('SMTP password:', done);
}],
createConfig: ['smtpPassword', function (done, results) {
var fsOptions = { encoding: 'utf-8' };
Fs.readFile(configTemplatePath, fsOptions, function (err, src) {
if (err) {
console.error('Failed to read config template.');
return done(err);
}
configTemplate = Handlebars.compile(src);
Fs.writeFile(configPath, configTemplate(results), done);
});
}],
setupRootUser: ['createConfig', function (done, results) {
var BaseModel = require('hapi-mongo-models').BaseModel;
var User = require('./server/models/user');
var Admin = require('./server/models/admin');
var AdminGroup = require('./server/models/admin-group');
Async.auto({
connect: function (done) {
BaseModel.connect({ url: results.mongodbUrl }, done);
},
clean: ['connect', function (done) {
Async.parallel([
User.deleteMany.bind(User, {}),
Admin.deleteMany.bind(Admin, {}),
AdminGroup.deleteMany.bind(AdminGroup, {})
], done);
}],
adminGroup: ['clean', function (done) {
AdminGroup.create('Root', done);
}],
admin: ['clean', function (done) {
Admin.create('Root Admin', done);
}],
user: ['clean', function (done, dbResults) {
User.create('root', results.rootPassword, results.rootEmail, done);
}],
adminMembership: ['admin', function (done, dbResults) {
var id = dbResults.admin._id.toString();
var update = {
$set: {
groups: {
root: 'Root'
}
}
};
Admin.findByIdAndUpdate(id, update, done);
}],
linkUser: ['admin', 'user', function (done, dbResults) {
var id = dbResults.user._id.toString();
var update = {
$set: {
'roles.admin': {
id: dbResults.admin._id.toString(),
name: 'Root Admin'
}
}
};
User.findByIdAndUpdate(id, update, done);
}],
linkAdmin: ['admin', 'user', function (done, dbResults) {
var id = dbResults.admin._id.toString();
var update = {
$set: {
user: {
id: dbResults.user._id.toString(),
name: 'root'
}
}
};
Admin.findByIdAndUpdate(id, update, done);
}]
}, function (err, dbResults) {
if (err) {
console.error('Failed to setup root user.');
return done(err);
}
done(null, true);
});
}]
}, function (err, results) {
if (err) {
console.error('Setup failed.');
console.error(err);
return process.exit(1);
}
console.log('Setup complete.');
process.exit(0);
});