1
1
import dotenv from 'dotenv' ;
2
2
import fs from 'fs' ;
3
3
import inquirer from 'inquirer' ;
4
- import { checkConnection } from './src/setup/checkConnection/checkConnection' ;
5
- import { askForTalawaApiUrl } from './src/setup/askForTalawaApiUrl/askForTalawaApiUrl' ;
6
4
import { checkEnvFile } from './src/setup/checkEnvFile/checkEnvFile' ;
7
5
import { validateRecaptcha } from './src/setup/validateRecaptcha/validateRecaptcha' ;
8
- import { askForCustomPort } from './src/setup/askForCustomPort/askForCustomPort' ;
9
-
10
- export async function main ( ) : Promise < void > {
11
- console . log ( 'Welcome to the Talawa Admin setup! 🚀' ) ;
12
-
13
- if ( ! fs . existsSync ( '.env' ) ) {
14
- fs . openSync ( '.env' , 'w' ) ;
15
- const config = dotenv . parse ( fs . readFileSync ( '.env.example' ) ) ;
16
- for ( const key in config ) {
17
- fs . appendFileSync ( '.env' , `${ key } =${ config [ key ] } \n` ) ;
18
- }
19
- } else {
20
- checkEnvFile ( ) ;
21
- }
22
-
23
- let shouldSetCustomPort : boolean ;
24
-
25
- if ( process . env . PORT ) {
26
- console . log (
27
- `\nCustom port for development server already exists with the value:\n${ process . env . PORT } ` ,
28
- ) ;
29
- shouldSetCustomPort = true ;
30
- } else {
31
- const { shouldSetCustomPortResponse } = await inquirer . prompt ( {
6
+ import askAndSetDockerOption from './src/setup/askAndSetDockerOption/askAndSetDockerOption' ;
7
+ import updateEnvFile from './src/setup/updateEnvFile/updateEnvFile' ;
8
+ import askAndUpdatePort from './src/setup/askAndUpdatePort/askAndUpdatePort' ;
9
+ import { askAndUpdateTalawaApiUrl } from './src/setup/askForDocker/askForDocker' ;
10
+
11
+ // Ask and set up reCAPTCHA
12
+ const askAndSetRecaptcha = async ( ) : Promise < void > => {
13
+ try {
14
+ const { shouldUseRecaptcha } = await inquirer . prompt ( {
32
15
type : 'confirm' ,
33
- name : 'shouldSetCustomPortResponse ' ,
34
- message : 'Would you like to set up a custom port ?' ,
16
+ name : 'shouldUseRecaptcha ' ,
17
+ message : 'Would you like to set up reCAPTCHA ?' ,
35
18
default : true ,
36
19
} ) ;
37
- shouldSetCustomPort = shouldSetCustomPortResponse ;
38
- }
39
-
40
- if ( shouldSetCustomPort ) {
41
- const customPort = await askForCustomPort ( ) ;
42
-
43
- const port = dotenv . parse ( fs . readFileSync ( '.env' ) ) . PORT ;
44
-
45
- fs . readFile ( '.env' , 'utf8' , ( err , data ) => {
46
- const result = data . replace ( `PORT=${ port } ` , `PORT=${ customPort } ` ) ;
47
- fs . writeFileSync ( '.env' , result , 'utf8' ) ;
48
- } ) ;
49
- }
50
-
51
- let shouldSetTalawaApiUrl : boolean ;
52
-
53
- if ( process . env . REACT_APP_TALAWA_URL ) {
54
- console . log (
55
- `\nEndpoint for accessing talawa-api graphql service already exists with the value:\n${ process . env . REACT_APP_TALAWA_URL } ` ,
56
- ) ;
57
- shouldSetTalawaApiUrl = true ;
58
- } else {
59
- const { shouldSetTalawaApiUrlResponse } = await inquirer . prompt ( {
60
- type : 'confirm' ,
61
- name : 'shouldSetTalawaApiUrlResponse' ,
62
- message : 'Would you like to set up talawa-api endpoint?' ,
63
- default : true ,
64
- } ) ;
65
- shouldSetTalawaApiUrl = shouldSetTalawaApiUrlResponse ;
66
- }
67
-
68
- if ( shouldSetTalawaApiUrl ) {
69
- let isConnected = false ,
70
- endpoint = '' ;
71
-
72
- while ( ! isConnected ) {
73
- endpoint = await askForTalawaApiUrl ( ) ;
74
- const url = new URL ( endpoint ) ;
75
- isConnected = await checkConnection ( url . origin ) ;
76
- }
77
- const envPath = '.env' ;
78
- const currentEnvContent = fs . readFileSync ( envPath , 'utf8' ) ;
79
- const talawaApiUrl = dotenv . parse ( currentEnvContent ) . REACT_APP_TALAWA_URL ;
80
-
81
- const updatedEnvContent = currentEnvContent . replace (
82
- `REACT_APP_TALAWA_URL=${ talawaApiUrl } ` ,
83
- `REACT_APP_TALAWA_URL=${ endpoint } ` ,
84
- ) ;
85
-
86
- fs . writeFileSync ( envPath , updatedEnvContent , 'utf8' ) ;
87
- const websocketUrl = endpoint . replace ( / ^ h t t p ( s ) ? : \/ \/ / , 'ws$1://' ) ;
88
- const currentWebSocketUrl =
89
- dotenv . parse ( updatedEnvContent ) . REACT_APP_BACKEND_WEBSOCKET_URL ;
90
-
91
- const finalEnvContent = updatedEnvContent . replace (
92
- `REACT_APP_BACKEND_WEBSOCKET_URL=${ currentWebSocketUrl } ` ,
93
- `REACT_APP_BACKEND_WEBSOCKET_URL=${ websocketUrl } ` ,
94
- ) ;
95
-
96
- fs . writeFileSync ( envPath , finalEnvContent , 'utf8' ) ;
97
- }
98
-
99
- const { shouldUseRecaptcha } = await inquirer . prompt ( {
100
- type : 'confirm' ,
101
- name : 'shouldUseRecaptcha' ,
102
- message : 'Would you like to set up ReCAPTCHA?' ,
103
- default : true ,
104
- } ) ;
105
-
106
- if ( shouldUseRecaptcha ) {
107
- const useRecaptcha = dotenv . parse (
108
- fs . readFileSync ( '.env' ) ,
109
- ) . REACT_APP_USE_RECAPTCHA ;
110
-
111
- fs . readFile ( '.env' , 'utf8' , ( err , data ) => {
112
- const result = data . replace (
113
- `REACT_APP_USE_RECAPTCHA=${ useRecaptcha } ` ,
114
- `REACT_APP_USE_RECAPTCHA=yes` ,
115
- ) ;
116
- fs . writeFileSync ( '.env' , result , 'utf8' ) ;
117
- } ) ;
118
- let shouldSetRecaptchaSiteKey : boolean ;
119
- if ( process . env . REACT_APP_RECAPTCHA_SITE_KEY ) {
120
- console . log (
121
- `\nreCAPTCHA site key already exists with the value ${ process . env . REACT_APP_RECAPTCHA_SITE_KEY } ` ,
122
- ) ;
123
- shouldSetRecaptchaSiteKey = true ;
124
- } else {
125
- const { shouldSetRecaptchaSiteKeyResponse } = await inquirer . prompt ( {
126
- type : 'confirm' ,
127
- name : 'shouldSetRecaptchaSiteKeyResponse' ,
128
- message : 'Would you like to set up a reCAPTCHA site key?' ,
129
- default : true ,
130
- } ) ;
131
- shouldSetRecaptchaSiteKey = shouldSetRecaptchaSiteKeyResponse ;
132
- }
133
20
134
- if ( shouldSetRecaptchaSiteKey ) {
21
+ if ( shouldUseRecaptcha ) {
135
22
const { recaptchaSiteKeyInput } = await inquirer . prompt ( [
136
23
{
137
24
type : 'input' ,
138
25
name : 'recaptchaSiteKeyInput' ,
139
26
message : 'Enter your reCAPTCHA site key:' ,
140
- validate : async ( input : string ) : Promise < boolean | string > => {
141
- if ( validateRecaptcha ( input ) ) {
142
- return true ;
143
- }
144
- return 'Invalid reCAPTCHA site key. Please try again.' ;
27
+ validate : ( input : string ) : boolean | string => {
28
+ return (
29
+ validateRecaptcha ( input ) ||
30
+ 'Invalid reCAPTCHA site key. Please try again.'
31
+ ) ;
145
32
} ,
146
33
} ,
147
34
] ) ;
148
35
149
- const recaptchaSiteKey = dotenv . parse (
150
- fs . readFileSync ( '.env' ) ,
151
- ) . REACT_APP_RECAPTCHA_SITE_KEY ;
152
-
153
- fs . readFile ( '.env' , 'utf8' , ( err , data ) => {
154
- const result = data . replace (
155
- `REACT_APP_RECAPTCHA_SITE_KEY=${ recaptchaSiteKey } ` ,
156
- `REACT_APP_RECAPTCHA_SITE_KEY=${ recaptchaSiteKeyInput } ` ,
157
- ) ;
158
- fs . writeFileSync ( '.env' , result , 'utf8' ) ;
159
- } ) ;
36
+ updateEnvFile ( 'REACT_APP_RECAPTCHA_SITE_KEY' , recaptchaSiteKeyInput ) ;
160
37
}
38
+ } catch ( error ) {
39
+ console . error ( 'Error setting up reCAPTCHA:' , error ) ;
40
+ throw new Error ( `Failed to set up reCAPTCHA: ${ ( error as Error ) . message } ` ) ;
161
41
}
42
+ } ;
162
43
44
+ // Ask and set up logging errors in the console
45
+ const askAndSetLogErrors = async ( ) : Promise < void > => {
163
46
const { shouldLogErrors } = await inquirer . prompt ( {
164
47
type : 'confirm' ,
165
48
name : 'shouldLogErrors' ,
@@ -169,17 +52,37 @@ export async function main(): Promise<void> {
169
52
} ) ;
170
53
171
54
if ( shouldLogErrors ) {
172
- const logErrors = dotenv . parse ( fs . readFileSync ( '.env' ) ) . ALLOW_LOGS ;
173
-
174
- fs . readFile ( '.env' , 'utf8' , ( err , data ) => {
175
- const result = data . replace ( `ALLOW_LOGS=${ logErrors } ` , 'ALLOW_LOGS=YES' ) ;
176
- fs . writeFileSync ( '.env' , result , 'utf8' ) ;
177
- } ) ;
55
+ updateEnvFile ( 'ALLOW_LOGS' , 'YES' ) ;
178
56
}
57
+ } ;
179
58
180
- console . log (
181
- '\nCongratulations! Talawa Admin has been successfully setup! 🥂🎉' ,
182
- ) ;
59
+ // Main function to run the setup process
60
+ export async function main ( ) : Promise < void > {
61
+ try {
62
+ console . log ( 'Welcome to the Talawa Admin setup! 🚀' ) ;
63
+
64
+ checkEnvFile ( ) ;
65
+ await askAndSetDockerOption ( ) ;
66
+ const envConfig = dotenv . parse ( fs . readFileSync ( '.env' , 'utf8' ) ) ;
67
+ const useDocker = envConfig . USE_DOCKER === 'YES' ;
68
+
69
+ // Only run these commands if Docker is NOT used
70
+ if ( ! useDocker ) {
71
+ await askAndUpdatePort ( ) ;
72
+ await askAndUpdateTalawaApiUrl ( ) ;
73
+ }
74
+
75
+ await askAndSetRecaptcha ( ) ;
76
+ await askAndSetLogErrors ( ) ;
77
+
78
+ console . log (
79
+ '\nCongratulations! Talawa Admin has been successfully set up! 🥂🎉' ,
80
+ ) ;
81
+ } catch ( error ) {
82
+ console . error ( '\n❌ Setup failed:' , error ) ;
83
+ console . log ( '\nPlease try again or contact support if the issue persists.' ) ;
84
+ process . exit ( 1 ) ;
85
+ }
183
86
}
184
87
185
88
main ( ) ;
0 commit comments