1
- const dotenv = require ( "dotenv" ) ;
2
- const fs = require ( "fs" ) ;
3
- const cryptolib = require ( "crypto" ) ;
4
- const inquirer = require ( "inquirer" ) ;
5
- const mongodb = require ( "mongodb" ) ;
6
- const redis = require ( "redis" ) ;
7
- const { exec } = require ( "child_process" ) ;
8
- const nodemailer = require ( "nodemailer" ) ;
1
+ import dotenv from "dotenv" ;
2
+ import fs from "fs" ;
3
+ import path from "path" ;
4
+ import * as cryptolib from "crypto" ;
5
+ import inquirer from "inquirer" ;
6
+ import mongodb from "mongodb" ;
7
+ import * as redis from "redis" ;
8
+ import { exec } from "child_process" ;
9
+ import nodemailer from "nodemailer" ;
10
+ import type { ExecException } from "child_process" ;
9
11
10
12
dotenv . config ( ) ;
11
13
@@ -114,6 +116,70 @@ async function accessAndRefreshTokens(
114
116
}
115
117
}
116
118
119
+ function transactionLogPath ( logPath : string | null ) : void {
120
+ const config = dotenv . parse ( fs . readFileSync ( ".env" ) ) ;
121
+ config . LOG = "true" ;
122
+ if ( ! logPath ) {
123
+ // Check if the logs/transaction.log file exists, if not, create it
124
+ const defaultLogPath = path . resolve ( __dirname , "logs" ) ;
125
+ const defaultLogFile = path . join ( defaultLogPath , "transaction.log" ) ;
126
+ if ( ! fs . existsSync ( defaultLogPath ) ) {
127
+ console . log ( "Creating logs/transaction.log file..." ) ;
128
+ fs . mkdirSync ( defaultLogPath ) ;
129
+ }
130
+
131
+ config . LOG_PATH = defaultLogFile ;
132
+ } else {
133
+ // Remove the logs files, if exists
134
+ const logsDirPath = path . resolve ( __dirname , "logs" ) ;
135
+ if ( fs . existsSync ( logsDirPath ) ) {
136
+ fs . readdirSync ( logsDirPath ) . forEach ( ( file : string ) => {
137
+ if ( file !== "README.md" ) {
138
+ const curPath = path . join ( logsDirPath , file ) ;
139
+ fs . unlinkSync ( curPath ) ;
140
+ }
141
+ } ) ;
142
+ }
143
+ config . LOG_PATH = logPath ;
144
+ }
145
+ fs . writeFileSync ( ".env" , "" ) ;
146
+ for ( const key in config ) {
147
+ fs . appendFileSync ( ".env" , `${ key } =${ config [ key ] } \n` ) ;
148
+ }
149
+ }
150
+
151
+ async function askForTransactionLogPath ( ) : Promise < string > {
152
+ let logPath : string | null ;
153
+ // Keep asking for path, until user gives a valid path
154
+ // eslint-disable-next-line no-constant-condition
155
+ while ( true ) {
156
+ const response = await inquirer . prompt ( [
157
+ {
158
+ type : "input" ,
159
+ name : "logPath" ,
160
+ message : "Enter absolute path of log file:" ,
161
+ default : null ,
162
+ } ,
163
+ ] ) ;
164
+ logPath = response . logPath ;
165
+ if ( logPath && fs . existsSync ( logPath ) ) {
166
+ try {
167
+ fs . accessSync ( logPath , fs . constants . R_OK | fs . constants . W_OK ) ;
168
+ break ;
169
+ } catch {
170
+ console . error (
171
+ "The file is not readable/writable. Please enter a valid file path."
172
+ ) ;
173
+ }
174
+ } else {
175
+ console . error (
176
+ "Invalid path or file does not exist. Please enter a valid file path."
177
+ ) ;
178
+ }
179
+ }
180
+ return logPath ;
181
+ }
182
+
117
183
// Check connection to Redis with the specified URL.
118
184
/**
119
185
* The function `checkRedisConnection` checks if a connection to Redis can be established using the
@@ -253,7 +319,7 @@ async function redisConfiguration(): Promise<void> {
253
319
// Update the .env file
254
320
const config = dotenv . parse ( fs . readFileSync ( ".env" ) ) ;
255
321
config . REDIS_HOST = host ;
256
- config . REDIS_PORT = port ;
322
+ config . REDIS_PORT = port . toString ( ) ;
257
323
config . REDIS_PASSWORD = password ;
258
324
updateEnvVariable ( config ) ;
259
325
} catch ( err ) {
@@ -640,7 +706,7 @@ async function importData(): Promise<void> {
640
706
console . log ( "Importing sample data..." ) ;
641
707
await exec (
642
708
"npm run import:sample-data" ,
643
- ( error : { message : string } , stdout : string , stderr : string ) => {
709
+ ( error : ExecException | null , stdout : string , stderr : string ) => {
644
710
if ( error ) {
645
711
console . error ( `Error: ${ error . message } ` ) ;
646
712
abort ( ) ;
@@ -657,7 +723,7 @@ async function importData(): Promise<void> {
657
723
658
724
type VerifySmtpConnectionReturnType = {
659
725
success : boolean ;
660
- error : any ;
726
+ error : unknown ;
661
727
} ;
662
728
663
729
/**
@@ -687,7 +753,7 @@ async function verifySmtpConnection(
687
753
await transporter . verify ( ) ;
688
754
console . log ( "SMTP connection verified successfully." ) ;
689
755
return { success : true , error : null } ;
690
- } catch ( error : any ) {
756
+ } catch ( error : unknown ) {
691
757
console . error ( "SMTP connection verification failed:" ) ;
692
758
return { success : false , error } ;
693
759
} finally {
@@ -749,7 +815,9 @@ async function configureSmtp(): Promise<void> {
749
815
console . error (
750
816
"SMTP configuration verification failed. Please check your SMTP settings."
751
817
) ;
752
- console . log ( error . message ) ;
818
+ if ( error instanceof Error ) {
819
+ console . log ( error . message ) ;
820
+ }
753
821
return ;
754
822
}
755
823
@@ -815,6 +883,33 @@ async function main(): Promise<void> {
815
883
816
884
accessAndRefreshTokens ( accessToken , refreshToken ) ;
817
885
886
+ const { shouldLog } = await inquirer . prompt ( {
887
+ type : "confirm" ,
888
+ name : "shouldLog" ,
889
+ message : "Would you like to enable logging for the database transactions?" ,
890
+ default : true ,
891
+ } ) ;
892
+
893
+ if ( shouldLog ) {
894
+ if ( process . env . LOG_PATH ) {
895
+ console . log (
896
+ `\n Log path already exists with the value:\n${ process . env . LOG_PATH } `
897
+ ) ;
898
+ }
899
+ let logPath : string | null = null ;
900
+ const { shouldUseCustomLogPath } = await inquirer . prompt ( {
901
+ type : "confirm" ,
902
+ name : "shouldUseCustomLogPath" ,
903
+ message : "Would you like to provide a custom path for storing logs?" ,
904
+ default : false ,
905
+ } ) ;
906
+
907
+ if ( shouldUseCustomLogPath ) {
908
+ logPath = await askForTransactionLogPath ( ) ;
909
+ }
910
+ transactionLogPath ( logPath ) ;
911
+ }
912
+
818
913
const { isDockerInstallation } = await inquirer . prompt ( {
819
914
type : "confirm" ,
820
915
name : "isDockerInstallation" ,
0 commit comments