forked from krishnaacharyaa/profileFolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.bat
161 lines (121 loc) · 3.99 KB
/
setup.bat
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
@echo off
::VARIABLES TO VERIFY INSTALLATION STATUS
set "GO_INSTALLED=0"
set "NODE_INSTALLED=0"
set "MONGODB_INSTALLED=0"
set "MONGOIMPORT_INSTALLED=0"
:: GO BUILD SETTINGS
set "START=cd backend && go mod tidy && go run main.go"
:: NEXT JS COMMANDS
set "INSTALL_AND_RUN=cd frontend && npm install && npm run dev"
:: MONGOIMPORT COMMANDS
set "INSERT_USER_INFO=mongoimport --db profileFolio --collection users --file users.json"
set "INSERT_SKILLS_INFO=mongoimport --db profileFolio --collection skills --file skills.json --jsonArray"
:: MONGODB COMMANDWS
set "START_MONGO=mongod"
:: Set initial retry count , max retries and initial delay
set "RETRY_COUNT=0"
set "MAX_RETRIES=10"
set "DELAY=1"
:: Copy Environment Files
set "COPY_ENV_BACKEND=copy /Y backend\.env.sample backend\.env"
set "COPY_ENV_FRONTEND=copy /Y frontend\.env.sample frontend\.env"
::CHECK IF ALL NECESSARY DEPENDENCIES ARE SETUP
::Check if Go is installed
go version >nul 2>&1
IF NOT ERRORLEVEL 1 (
set GO_INSTALLED=1
)
::Check if Node.js is installed
node --version >nul 2>&1
IF NOT ERRORLEVEL 1 (
set NODE_INSTALLED=1
)
::Check if MongoDB is installed
mongod --version >nul 2>&1
IF NOT ERRORLEVEL 1 (
set MONGO_INSTALLED=1
)
::Check if mongoimport is installed by checking the version
mongoimport --version >nul 2>&1
::If the command succeeds, set MONGOIMPORT_INSTALLED to 1
IF NOT ERRORLEVEL 1 (
set "MONGOIMPORT_INSTALLED=1"
)
::Check the results and provide feedback
IF %GO_INSTALLED%==0 (
echo Go is not installed on this system.
echo Please install Go from https://go.dev/doc/install
PAUSE
)
IF %NODE_INSTALLED%==0 (
echo Node.js is not installed on this system.
echo Please install Node.js from https://nodejs.org/en/download/package-manager
PAUSE
)
IF %MONGO_INSTALLED%==0 (
echo MongoDB is not installed on this system.
echo Please install MongoDB from https://www.mongodb.com/try/download/community
PAUSE
)
IF %MONGOIMPORT_INSTALLED%==0 (
echo MONGOIMPORT Database tool is not installed on this system / MONGOIMPORT is not available in your system PATH environment variable
echo Please head to https://www.mongodb.com/docs/database-tools/installation/installation-windows/ for installation process
PAUSE
)
:: If any are missing, exit with an error
IF %GO_INSTALLED%==0 (
exit /b 1
)
IF %NODE_INSTALLED%==0 (
exit /b 1
)
IF %MONGO_INSTALLED%==0 (
exit /b 1
)
IF %MONGOIMPORT_INSTALLED%==0 (
exit /b 1
)
:: Change to the directory where the batch file is located
cd /d "%~dp0"
::--------------------------------------------------------------------------
%COPY_ENV_BACKEND%
echo "Copied .env file for backend successfully"
%COPY_ENV_FRONTEND%
echo "Copied .env file for frontend successfully"
start cmd /k "%START_MONGO%"
:RETRY_LOOP
echo Attempt #%RETRY_COUNT% to import data using mongoimport...
:: Run the mongoimport command
%INSERT_USER_INFO%
:: Check if the mongoimport command was successful
IF ERRORLEVEL 1 (
:: If it failed, increment the retry count
set /a RETRY_COUNT+=1
:: Check if we've reached the maximum retries
IF %RETRY_COUNT% GTR %MAX_RETRIES% (
echo Max retries reached.Unable to insert data in MongoDB Exiting....
exit /b 1
)
:: Exponential backoff: double the delay each time
echo mongoimport failed. Retrying in %DELAY% seconds...
timeout /t %DELAY% /nobreak >nul
:: Double the delay for the next attempt
set /a DELAY*=2
:: Loop back to retry
GOTO RETRY_LOOP
) ELSE (
REM If it succeeded, exit the loop
%INSERT_SKILLS_INFO%
:: Execute the backend command
echo launching backend...
start cmd /k "%START%"
:: Start a new command window to run the frontend commands
echo launching frontend...
start cmd /k "%INSTALL_AND_RUN%"
PAUSE
exit /b 0
)
::If the script gets here, something went wrong
echo Unable to start the mongo server !!!
PAUSE