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