-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.js
61 lines (55 loc) · 1.46 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
/* eslint-disable no-console */
require('dotenv').config()
const prompt = require('prompt')
const mongoose = require('mongoose')
const Admin = require('./server/models/admin.model')
const secrets = require('./secrets')
console.log('Press Ctrl + C to quit at any time if the process hangs!')
const promptAttributes = [
{
name: 'email',
message: 'Email with which you would like to login to dashboard',
hidden: false
}
]
prompt.start()
prompt.get(promptAttributes, async (err, result) => {
if (err) {
console.error(err)
} else {
const { MONGO_URI } = secrets
if (!MONGO_URI) {
console.log(
'MONGO_URI and ELASTIC_URL is required in the .env file. Please specify it!'
)
return
}
try {
await mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: false,
useFindAndModify: false
})
} catch (error) {
console.log(
'Error while connecting to mongodb! Please check the MONGO_URI in .env'
)
console.error(error)
return
}
}
try {
if (!result.email || !result.email.trim()) {
console.log('Email cannot be empty!')
return
}
await Admin.create({ email: result.email })
console.log('Email assigned to admin access!')
} catch (error) {
console.log('Cannot insert email to admins collection!')
console.error(error)
return
}
console.log('Done! Press Ctrl + C to quit!')
})