-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
210 lines (199 loc) · 6.77 KB
/
setup.js
File metadata and controls
210 lines (199 loc) · 6.77 KB
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
const fs = require('fs');
const prompts = require('prompts');
const { validate: emailValidate } = require('deep-email-validator');
require('colors');
async function emailValidator (email) {
const validateEmail = await emailValidate({
email,
validateRegex: true,
validateMx: true,
validateTypo: false,
validateDisposable: true,
validateSMTP: false
});
return validateEmail.valid;
}
const questions = [
{
type: 'text',
name: 'GMAIL_USER',
message: 'Gmail user',
validate: async value => await emailValidator(value) ? true : 'Please enter a valid email address (example: yossi56@gmail.com).'
},
{
type: 'password',
name: 'GMAIL_PASSWORD',
message: 'Gmail password (app password)',
validate: value => value.length === 16 ? true : 'Enable two-step verification and use a 16-character app password (https://support.google.com/accounts/answer/185833)'
},
{
type: 'text',
name: 'MONGO_URI',
message: 'MongoDB connection string',
validate: value => /^mongodb(\+srv)?:\/\//.test(value) ? true : 'Please enter a valid MongoDB URI (example: mongodb://localhost:27017/feeds)'
},
{
type: 'text',
name: 'MONGO_DB_NAME',
message: 'MongoDB database name',
initial: 'rss-tracker',
validate: value => value.length ? true : 'Please enter a database name'
},
{
type: 'number',
name: 'MAX_FEEDS_PER_USER',
message: 'Max feeds per user - leave blank to default: 10',
initial: 10,
min: 1,
max: 200,
validate: value => (!value || value > 0) ? true : 'Please enter a number greater than 0. leave blank to default: 10'
},
{
type: 'number',
name: 'PORT',
message: 'Port to listen. leave blank to default: 4200',
initial: 4200,
min: 1,
max: 65535
},
{
type: 'text',
name: 'FRONTEND_URL',
message: 'FRONTEND URL - for links in emails. leave blank to default - localhost (for development)'
},
{
type: 'multiselect',
name: 'ALLOWED_DOMAINS',
message: 'Allowed domains for create feed. leave blank to allow all domains',
choices: [
{
value: 'hm-news.co.il',
selected: true
},
{
value: 'www.jdn.co.il',
selected: true
},
{
value: 'www.93fm.co.il',
selected: true
},
{
value: 'www.bahazit.co.il',
selected: true
},
{
value: 'www.geektime.co.il',
selected: true
},
{
value: 'internet-israel.com',
selected: true
}
],
hint: 'Use space to select. Press Enter to submit'
},
{
type: 'multiselect',
name: 'DOMAINS_ALLOWED_ATTACHED_IMAGES',
message: 'Domains allowed to attach images. leave blank to allow all domains',
choices: [
{
value: 'hm-news.co.il',
selected: true
},
{
value: 'www.jdn.co.il',
selected: true
}
]
}
];
async function checkConfig (config = process.env) {
const variables = questions.map(q => q.name);
const missingVariables = [];
for (const variable of variables) {
if (!config[variable]) {
missingVariables.push(variable);
}
}
if (missingVariables.length) {
console.log(`Config error: missing key(s): ${missingVariables.join(', ')}`.red);
console.log(`You can configure the missing variables by running: ${'npm run configure'.blue}`.grey);
process.exit(1);
}
}
async function createConfigFile () {
console.log('👋 Welcome to the configuration wizard!'.yellow);
if (fs.existsSync('config.json')) {
const currentConfig = JSON.parse(fs.readFileSync('config.json'));
const confirmForceCreate = await prompts({
type: 'confirm',
name: 'value',
message: 'Config file already exists; '.red + 'Do you want to create a new one anyway? '.yellow + `\n${'current config:'.blue}\n${JSON.stringify(currentConfig, null, 2).yellow}\n${'re-create by force?'.red}`
});
if (!confirmForceCreate.value) {
console.log('Goodbye!'.yellow);
process.exit(0);
}
};
const responses = {};
for (const question of questions) {
if (fs.existsSync('config.json')) {
const currentConfig = JSON.parse(fs.readFileSync('config.json'));
if (currentConfig[question.name]) {
const confirmOverride = await prompts({
type: 'confirm',
name: 'value',
message: 'This variable already exists: '.red + `Do you want to override "${question.name.blue}" (${'current value: '.grey + String(currentConfig[question.name]).blue})?`,
initial: false
});
if (!confirmOverride.value) {
responses[question.name] = currentConfig[question.name];
continue;
}
}
}
const answer = await prompts(question);
responses[question.name] = answer[question.name];
}
const defaults = [
{ name: 'MAX_FEEDS_PER_USER', value: 10 },
{ name: 'PORT', value: 4000 },
{ name: 'FRONTEND_URL', value: 'http://localhost:' + responses.PORT }
];
for (const defaultConfig of defaults) {
if (!responses[defaultConfig.name]) {
responses[defaultConfig.name] = defaultConfig.value;
}
}
const confirmResult = await prompts({
type: 'confirm',
name: 'value',
message: '👀 The following config will be saved to config.json:\n'.blue + JSON.stringify(responses, null, 2).yellow + '\ncontinue?',
initial: true
});
if (!confirmResult.value) {
console.log('Goodbye!'.yellow);
process.exit(0);
}
checkConfig(responses);
fs.writeFileSync('config.json', JSON.stringify(responses, null, 2));
console.log('🎉 Config file created successfully!\n'.green + 'ℹ️ You can now run the server via command: '.grey + 'npm start'.blue);
process.exit(0);
}
if (require.main === module) {
createConfigFile();
}
module.exports = () => {
// set config to process.env and validate the config
let config = {};
if (fs.existsSync('config.json')) {
config = JSON.parse(fs.readFileSync('config.json'));
}
process.env = {
...config,
...process.env
};
checkConfig();
};