-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
184 lines (170 loc) · 4.4 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const { Seeder } = require('mongo-seeding')
const mongoose = require('mongoose')
const inquirer = require('inquirer')
const { createHash } = require('crypto')
const { ObjectId } = require('mongodb')
const { saltHashPassword } = require('./src/utils/password')
const chalk = require('chalk')
const shortID = require('shortid')
const fs = require('fs')
let admin = {}
let collections = [
{
name: 'user',
documents: [],
},
]
let seeding = {
database: {},
dropDatabase: true,
}
const questions = [
{
type: 'input',
name: 'host',
message: 'Enter the host you want OPE to run on',
default: 'localhost'
},
{
type: 'confirm',
name: 'https',
message: 'Do you want to use https?',
default: false
},
{
type: 'input',
name: 'port',
message: 'Enter the port for OPE',
default: 3000
},
{
type: 'list',
name: 'env',
message: 'Please select your environment:',
choices: [
'development',
'test',
'production'
]
},
{
type: 'input',
name: 'db_host',
message: 'Please enter the host for your mongoDB',
default: 'localhost'
},
{
type: 'input',
name: 'db_port',
message: 'Please enter the port for your mongoDB',
default: 27017
},
{
type: 'input',
name: 'db_name',
message: 'Please enter a database name',
default: 'openproductevaluation'
},
{
type: 'input',
name: 'email',
message: 'What should be the email of your OPE admin account?'
},
{
type: 'input',
name: 'firstname',
message: 'What should be the firstname of your OPE admin account?',
},
{
type: 'input',
name: 'lastname',
message: 'What should be the lastname of your OPE admin account?',
},
{
type: 'password',
name: 'password',
message: 'Please enter a password for your admin account'
}
]
inquirer.prompt(questions)
.then(answers => {
// setup database seeding
seeding.database.host = answers.db_host
seeding.database.port = answers.db_port
seeding.database.name = answers.db_name
// setup admin account
const hash = createHash('sha1')
.update(answers.email, 'utf8')
.digest('hex')
admin._id = ObjectId(hash.substring(0, 24))
admin.creationDate = new Date()
admin.lastUpdate = new Date()
admin.firstName = answers.firstname
admin.lastName = answers.lastname
admin.passwordData = saltHashPassword(answers.password)
admin.email = answers.email
admin.isAdmin = true
// add admin to user collection
collections[0].documents.push(admin)
// check for successful mongodb connection
checkConnection(answers.db_host, answers.db_port, answers.db_name)
.then(() => {
const env = {
'OPE_HOST': answers.host,
'OPE_HTTPS': answers.https,
'OPE_PORT': answers.port,
'OPE_SECRET': shortID.generate(),
'NODE_ENV': answers.env,
'DB_NAME': answers.db_name,
'DB_PORT': answers.db_port,
'DB_HOST': answers.db_host,
}
// try to seed database
seed(seeding, collections).then(() => {
let output = ''
for(let value in env) {
output += `${value}=${env[value]}\n`
}
fs.writeFile('./.env', output, (err) => {
if(err) {
console.log(chalk.red('Could not write .env!'))
console.log(chalk.red('Make sure you have access and try again!'))
process.exit(1)
}
process.exit()
})
})
})
})
const checkConnection = async (host, port, database) => {
console.log(chalk.cyan('Checking mongoDB details...'))
try {
await mongoose.connect(`mongodb://${host}:${port}/${database}`, {useNewUrlParser: true})
console.log(chalk.green('Connection successful!'))
} catch (e) {
console.log(chalk.red('Could not connect to mongoDB using your details!'))
console.log(chalk.red('Check your settings below and try again'))
console.log(
JSON.stringify(
{
host,
port,
database
},
null,
2
)
)
process.exit(1)
}
}
const seed = async (config, collections) => {
try {
const seeder = new Seeder(config)
await seeder.import(collections)
console.log(chalk.green('Database setup successful!'))
} catch(e) {
console.log(chalk.red('Database setup failed'))
process.exit(1)
}
}