-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
164 lines (142 loc) · 4.71 KB
/
setup.ts
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
// this little script can be used to setup the application
import readline from 'readline';
import mongoose from 'mongoose';
import { MongoError } from 'mongodb';
import { Config } from './src/models/Config';
import crypto from 'crypto';
// used for getting user input
const r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// this little function can be used to get input from the user
async function ask(question: string): Promise<string> {
return new Promise(resolve => {
r1.question(question, resolve);
})
}
async function setup() {
console.log('Snappy Book Review application setup.');
let mongoUrl: string;
let connectionOK: boolean;
// get the mongodb connection url and check if it's correct
// continue asking until it is correct
do {
mongoUrl = await ask('Please enter the MongoDB connection URL: ');
console.log('Attempting to connect...');
try {
await mongoose.connect(mongoUrl);
connectionOK = true;
console.log('Connection successful!');
} catch (err) {
if (err instanceof MongoError) {
connectionOK = false;
console.log('Failed to connect to MongoDB!');
} else {
throw err;
}
}
} while (!connectionOK)
// get the site preferred protocol and the address
const sitePreferredProtocol = await ask('Please enter the preferred ' +
'protocol of your site (most likely http or https): ');
const siteAddress = await ask('Please enter your site address (e.g. ' +
'localhost, localhost:3000, example.com): ');
const sessionMaxAge = await ask('Please enter the maximum session length ' +
'in milliseconds (e.g. 60000 for 1 minutes): ');
const smtpConnectionUrl = await ask('Please enter the SMTP connection ' +
'URL (for nodemailer): ');
const emailFrom = await ask('Please enter the email address from which ' +
'the app will send emails in the format ' +
'\'"Firstname Lastname" <[email protected]>\': ');
// session secret
console.log('Generating session secret...');
const sessionSecret = crypto.randomBytes(64).toString('hex');
console.log('Done');
const config = {
sitePreferredProtocol: sitePreferredProtocol,
siteAddress: siteAddress,
sessionSecret: sessionSecret,
sessionMaxAge: sessionMaxAge,
smtpConnectionUrl: smtpConnectionUrl,
emailFrom: emailFrom
}
// ask user to confirm if the script can write the config data to the
// database
let answerValid: boolean;
do {
const answer = await ask('Confirm to write config data to database ' +
'[y/n]: ');
const answerLowerCase = answer.toLowerCase();
if (answerLowerCase === 'y' || answerLowerCase === 'yes') {
answerValid = true;
} else if (answerLowerCase === 'n' || answerLowerCase === 'no') {
console.log('Aborted.');
try {
await mongoose.disconnect();
} catch (err) {
if (err instanceof MongoError) {
console.log('Failed to properly disconnect from MongoDB!');
} else {
throw err;
}
}
r1.close();
return;
answerValid = true;
} else {
console.log('Please enter yes or no.');
answerValid = false;
}
} while(!answerValid)
// write the config to the database
console.log('Writing config data to database...');
try {
const doc = new Config(config);
await doc.save();
} catch (err) {
if (err instanceof MongoError) {
console.log('Failed to write config data!');
try {
await mongoose.disconnect();
} catch (err) {
if (err instanceof MongoError) {
console.log('Failed to properly disconnect from MongoDB!');
} else {
throw err;
}
}
r1.close();
return;
} else {
throw err;
}
}
console.log('Config data was successfully written to database!');
// disconnect mongoose from the database
try {
await mongoose.disconnect();
} catch (err) {
if (err instanceof MongoError) {
console.log('Failed to properly disconnect from MongoDB!');
} else {
throw err;
}
}
// close the readline object (used for getting user input)
r1.close();
// instruct user to set some environment variables
console.log('Please set the following environment variables in your ' +
'application (For simplicity, you can just create a .env file in the ' +
'project root and paste it in there): ');
console.log();
// use the production environment
console.log('NOVE_ENV=production');
// the mongodb connection url
console.log('MONGODB_URI=' + mongoUrl);
// no more environment variables are needed as all config is stored in the
// database
console.log();
console.log('Setup complete!');
}
setup();