forked from PalisadoesFoundation/talawa-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
236 lines (206 loc) · 6.76 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import dotenv from 'dotenv';
import fs from 'fs';
import inquirer from 'inquirer';
import fetch from 'node-fetch';
dotenv.config();
// Check Talawa API Connection
async function checkConnection(url: string): Promise<any> {
console.log('\nChecking Talawa-API connection....');
let isConnected = false;
await fetch(url)
.then(() => {
isConnected = true;
console.log('\nConnection to Talawa-API successful! 🎉');
})
.catch(() => {
console.log(
'\nTalawa-API service is unavailable. Is it running? Check your network connectivity too.',
);
});
return isConnected;
}
async function askForTalawaApiUrl(): Promise<string> {
const { endpoint } = await inquirer.prompt([
{
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
},
]);
return endpoint;
}
async function askForCustomPort(): Promise<number> {
const { customPort } = await inquirer.prompt([
{
type: 'input',
name: 'customPort',
message:
'Enter custom port for development server (leave blank for default 4321):',
default: 4321,
},
]);
return customPort;
}
// Check if all the fields in .env.example are present in .env
function checkEnvFile(): void {
const env = dotenv.parse(fs.readFileSync('.env'));
const envSample = dotenv.parse(fs.readFileSync('.env.example'));
const misplaced = Object.keys(envSample).filter((key) => !(key in env));
if (misplaced.length > 0) {
const config = dotenv.parse(fs.readFileSync('.env.example'));
misplaced.map((key) =>
fs.appendFileSync('.env', `${key}=${config[key]}\n`),
);
}
}
// Validate ReCaptcha
function validateRecaptcha(string: string): boolean {
const pattern = /^[a-zA-Z0-9_-]{40}$/;
return pattern.test(string);
}
async function main(): Promise<void> {
console.log('Welcome to the Talawa Admin setup! 🚀');
if (!fs.existsSync('.env')) {
fs.openSync('.env', 'w');
const config = dotenv.parse(fs.readFileSync('.env.example'));
for (const key in config) {
fs.appendFileSync('.env', `${key}=${config[key]}\n`);
}
} else {
checkEnvFile();
}
let shouldSetCustomPort: boolean;
if (process.env.PORT) {
console.log(
`\nCustom port for development server already exists with the value:\n${process.env.PORT}`,
);
shouldSetCustomPort = true;
} else {
const { shouldSetCustomPortResponse } = await inquirer.prompt({
type: 'confirm',
name: 'shouldSetCustomPortResponse',
message: 'Would you like to set up a custom port?',
default: true,
});
shouldSetCustomPort = shouldSetCustomPortResponse;
}
if (shouldSetCustomPort) {
const customPort = await askForCustomPort();
const port = dotenv.parse(fs.readFileSync('.env')).PORT;
fs.readFile('.env', 'utf8', (err, data) => {
const result = data.replace(`PORT=${port}`, `PORT=${customPort}`);
fs.writeFileSync('.env', result, 'utf8');
});
}
let shouldSetTalawaApiUrl: boolean;
if (process.env.REACT_APP_TALAWA_URL) {
console.log(
`\nEndpoint for accessing talawa-api graphql service already exists with the value:\n${process.env.REACT_APP_TALAWA_URL}`,
);
shouldSetTalawaApiUrl = true;
} else {
const { shouldSetTalawaApiUrlResponse } = await inquirer.prompt({
type: 'confirm',
name: 'shouldSetTalawaApiUrlResponse',
message: 'Would you like to set up talawa-api endpoint?',
default: true,
});
shouldSetTalawaApiUrl = shouldSetTalawaApiUrlResponse;
}
if (shouldSetTalawaApiUrl) {
let isConnected = false,
endpoint = '';
while (!isConnected) {
endpoint = await askForTalawaApiUrl();
const url = new URL(endpoint);
isConnected = await checkConnection(url.origin);
}
const talawaApiUrl = dotenv.parse(
fs.readFileSync('.env'),
).REACT_APP_TALAWA_URL;
fs.readFile('.env', 'utf8', (err, data) => {
const result = data.replace(
`REACT_APP_TALAWA_URL=${talawaApiUrl}`,
`REACT_APP_TALAWA_URL=${endpoint}`,
);
fs.writeFileSync('.env', result, 'utf8');
});
}
const { shouldUseRecaptcha } = await inquirer.prompt({
type: 'confirm',
name: 'shouldUseRecaptcha',
message: 'Would you like to set up ReCAPTCHA?',
default: true,
});
if (shouldUseRecaptcha) {
const useRecaptcha = dotenv.parse(
fs.readFileSync('.env'),
).REACT_APP_USE_RECAPTCHA;
fs.readFile('.env', 'utf8', (err, data) => {
const result = data.replace(
`REACT_APP_USE_RECAPTCHA=${useRecaptcha}`,
`REACT_APP_USE_RECAPTCHA=yes`,
);
fs.writeFileSync('.env', result, 'utf8');
});
let shouldSetRecaptchaSiteKey: boolean;
if (process.env.REACT_APP_RECAPTCHA_SITE_KEY) {
console.log(
`\nreCAPTCHA site key already exists with the value ${process.env.REACT_APP_RECAPTCHA_SITE_KEY}`,
);
shouldSetRecaptchaSiteKey = true;
} else {
const { shouldSetRecaptchaSiteKeyResponse } = await inquirer.prompt({
type: 'confirm',
name: 'shouldSetRecaptchaSiteKeyResponse',
message: 'Would you like to set up a reCAPTCHA site key?',
default: true,
});
shouldSetRecaptchaSiteKey = shouldSetRecaptchaSiteKeyResponse;
}
if (shouldSetRecaptchaSiteKey) {
const { recaptchaSiteKeyInput } = await inquirer.prompt([
{
type: 'input',
name: 'recaptchaSiteKeyInput',
message: 'Enter your reCAPTCHA site key:',
validate: async (input: string): Promise<boolean | string> => {
if (validateRecaptcha(input)) {
return true;
}
return 'Invalid reCAPTCHA site key. Please try again.';
},
},
]);
const recaptchaSiteKey = dotenv.parse(
fs.readFileSync('.env'),
).REACT_APP_RECAPTCHA_SITE_KEY;
fs.readFile('.env', 'utf8', (err, data) => {
const result = data.replace(
`REACT_APP_RECAPTCHA_SITE_KEY=${recaptchaSiteKey}`,
`REACT_APP_RECAPTCHA_SITE_KEY=${recaptchaSiteKeyInput}`,
);
fs.writeFileSync('.env', result, 'utf8');
});
}
}
const { shouldLogErrors } = await inquirer.prompt({
type: 'confirm',
name: 'shouldLogErrors',
message:
'Would you like to log Compiletime and Runtime errors in the console?',
default: true,
});
if (shouldLogErrors) {
const logErrors = dotenv.parse(fs.readFileSync('.env')).ALLOW_LOGS;
fs.readFile('.env', 'utf8', (err, data) => {
const result = data.replace(`ALLOW_LOGS=${logErrors}`, 'ALLOW_LOGS=YES');
fs.writeFileSync('.env', result, 'utf8');
});
}
console.log(
'\nCongratulations! Talawa Admin has been successfully setup! 🥂🎉',
);
}
main();