-
Notifications
You must be signed in to change notification settings - Fork 15
/
check-outdated.bat
258 lines (228 loc) · 7.21 KB
/
check-outdated.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
@echo off
setlocal enabledelayedexpansion
@rem only for interactive debugging
set _DEBUG=0
@rem #########################################################################
@rem ## Environment setup
set _EXITCODE=0
call :env
if not %_EXITCODE%==0 goto end
call :args %*
if not %_EXITCODE%==0 goto end
@rem #########################################################################
@rem ## Main
if %_HELP%==1 (
call :help
exit /b !_EXITCODE!
)
for /f "delims=" %%f in ('where /r "%_ROOT_DIR%" package.json ^| findstr /v node_modules 2^>NUL') do (
call :outdated "%%~dpf"
if not !_EXITCODE!==0 goto end
)
goto end
@rem #########################################################################
@rem ## Subroutines
@rem output parameter(s): _DEBUG_LABEL, _ERROR_LABEL, _WARNING_LABEL, _NPM_CMD
:env
set _BASENAME=%~n0
for %%f in ("%~dp0\.") do set "_ROOT_DIR=%%~dpf"
@rem remove trailing backslash for virtual drives
if "%_ROOT_DIR:~-1%"=="\" set "_ROOT_DIR=%_ROOT_DIR:~0,-1%"
call :env_colors
set _DEBUG_LABEL=%_NORMAL_BG_CYAN%[%_BASENAME%]%_RESET%
set _ERROR_LABEL=%_STRONG_FG_RED%Error%_RESET%:
set _WARNING_LABEL=%_STRONG_FG_YELLOW%Warning%_RESET%:
if not exist "%NODE_HOME%\npm.cmd" (
echo %_ERROR_LABEL% Node installation not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_NPM_CMD=%NODE_HOME%\npm.cmd"
@rem use newer PowerShell version if available
where /q pwsh.exe
if %ERRORLEVEL%==0 ( set _PWSH_CMD=pwsh.exe
) else ( set _PWSH_CMD=powershell.exe
)
goto :eof
:env_colors
@rem ANSI colors in standard Windows 10 shell
@rem see https://gist.github.com/mlocati/#file-win10colors-cmd
@rem normal foreground colors
set _NORMAL_FG_BLACK=[30m
set _NORMAL_FG_RED=[31m
set _NORMAL_FG_GREEN=[32m
set _NORMAL_FG_YELLOW=[33m
set _NORMAL_FG_BLUE=[34m
set _NORMAL_FG_MAGENTA=[35m
set _NORMAL_FG_CYAN=[36m
set _NORMAL_FG_WHITE=[37m
@rem normal background colors
set _NORMAL_BG_BLACK=[40m
set _NORMAL_BG_RED=[41m
set _NORMAL_BG_GREEN=[42m
set _NORMAL_BG_YELLOW=[43m
set _NORMAL_BG_BLUE=[44m
set _NORMAL_BG_MAGENTA=[45m
set _NORMAL_BG_CYAN=[46m
set _NORMAL_BG_WHITE=[47m
@rem strong foreground colors
set _STRONG_FG_BLACK=[90m
set _STRONG_FG_RED=[91m
set _STRONG_FG_GREEN=[92m
set _STRONG_FG_YELLOW=[93m
set _STRONG_FG_BLUE=[94m
set _STRONG_FG_MAGENTA=[95m
set _STRONG_FG_CYAN=[96m
set _STRONG_FG_WHITE=[97m
@rem strong background colors
set _STRONG_BG_BLACK=[100m
set _STRONG_BG_RED=[101m
set _STRONG_BG_GREEN=[102m
set _STRONG_BG_YELLOW=[103m
set _STRONG_BG_BLUE=[104m
@rem we define _RESET in last position to avoid crazy console output with type command
set _BOLD=[1m
set _UNDERSCORE=[4m
set _INVERSE=[7m
set _RESET=[0m
goto :eof
rem input parameter: %*
:args
set _HELP=0
set _INSTALL=0
set _TIMER=0
set _VERBOSE=0
set __N=0
:args_loop
set "__ARG=%~1"
if not defined __ARG goto args_done
if "%__ARG:~0,1%"=="-" (
@rem option
if "%__ARG%"=="-debug" ( set _DEBUG=1
) else if "%__ARG%"=="-help" ( set _HELP=1
) else if "%__ARG%"=="-install" ( set _INSTALL=1
) else if "%__ARG%"=="-timer" ( set _TIMER=1
) else if "%__ARG%"=="-verbose" ( set _VERBOSE=1
) else (
echo %_ERROR_LABEL% Unknown option %__ARG% 1>&2
set _EXITCODE=1
goto args_done
)
) else (
@rem subcommand
if "%__ARG%"=="help" ( set _HELP=1
) else (
echo %_ERROR_LABEL% Unknown subcommand %__ARG% 1>&2
set _EXITCODE=1
goto args_done
)
set /a __N+=1
)
shift
goto :args_loop
:args_done
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% Options : _INSTALL=%_INSTALL% _TIMER=%_TIMER% _VERBOSE=%_VERBOSE% 1>&2
echo %_DEBUG_LABEL% Subcommands: _HELP=%_HELP% 1>&2
echo %_DEBUG_LABEL% Variables : NODE_HOME="%NODE_HOME%" 1>&2
)
if %_TIMER%==1 for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set _TIMER_START=%%i
goto :eof
:help
if %_VERBOSE%==1 (
set __BEG_P=%_STRONG_FG_CYAN%
set __BEG_O=%_STRONG_FG_GREEN%
set __BEG_N=%_NORMAL_FG_YELLOW%
set __END=%_RESET%
) else (
set __BEG_P=
set __BEG_O=
set __BEG_N=
set __END=
)
echo Usage: %__BEG_O%%_BASENAME% { ^<option^> ^| ^<subcommand^> }%__END%
echo.
echo %__BEG_P%Options:%__END%
echo %__BEG_O%-debug%__END% print commands executed by this script
echo %__BEG_O%-install%__END% install latest package (if outdated)
echo %__BEG_O%-timer%__END% print total execution time
echo %__BEG_O%-verbose%__END% print progress messages
echo.
echo %__BEG_P%Subcommands:%__END%
echo %__BEG_O%help%__END% print this help message
goto :eof
:outdated
set "__PROJ_DIR=%~1"
if %_VERBOSE%==1 (
set __BEG_C=%_STRONG_FG_RED%
set __BEG_L=%_STRONG_FG_GREEN%
set __END=%_RESET%
) else (
set __BEG_C=
set __BEG_L=
set __END=
)
pushd "%__PROJ_DIR%"
@rem echo Current directory: !__PROJ_DIR:%_ROOT_DIR%=!
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_NPM_CMD%" outdated ^| findstr /v Wanted 1>&2
) else if %_VERBOSE%==1 ( echo Search for outdated packages in directory "!__PROJ_DIR:%_ROOT_DIR%=!" 1>&2
)
for /f "tokens=1,2,3,4,*" %%i in ('"%_NPM_CMD%" outdated ^| findstr /v Wanted') do (
set __PKG_NAME=%%i
set __CURRENT=%%j
set __WANTED=%%k
set __LATEST=%%l
if "!__CURRENT!"=="MISSING" (
call :current_missing "!__PKG_NAME!"
if not !_EXITCODE!==0 goto outdated_done
set __CURRENT=!_CURRENT_MISSING!
)
if "!__CURRENT!"=="*" (
@rem Keep "Any version"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% Package "!__PKG_NAME!" has version "*" ^(!__LATEST!^) 1>&2
)
) else if not "!__CURRENT!"=="!__LATEST!" (
echo Outdated package !__PKG_NAME!: current=%__BEG_C%!__CURRENT!%__END%, latest=%__BEG_L%!__LATEST!%__END%
if %_INSTALL%==1 (
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_NPM_CMD%" install -audit !__PKG_NAME!@!__LATEST! --save 1^>NUL 1>&2
) else if %_VERBOSE%==1 ( echo Install package !__PKG_NAME!@!__LATEST! 1>&2
)
call "%_NPM_CMD%" install -audit !__PKG_NAME!@!__LATEST! --save 1>NUL
if not !ERRORLEVEL!==0 (
set _EXITCODE=1
)
)
)
)
:outdated_done
popd
goto :eof
@rem input parameter: %1=package name
@rem output parameter: _CURRENT_MISSING
:current_missing
set __PKG_NAME=%~1
set _CURRENT_MISSING=
for /f "usebackq delims=:, tokens=1,2,*" %%f in (`findstr /c:"""%__PKG_NAME%""" package.json`) do (
for /f "usebackq" %%x in (`call "%_PWSH_CMD%" -C "'%%g'.Trim()"`) do set _CURRENT_MISSING=%%x
if "!_CURRENT_MISSING:~0,1!"=="^" ( set _CURRENT_MISSING=!_CURRENT_MISSING:~1!
) else if "!_CURRENT_MISSING:%~0,1!"=="~" ( set _CURRENT_MISSING=!_CURRENT_MISSING:~1!
)
)
goto :eof
@rem output parameter: _DURATION
:duration
set __START=%~1
set __END=%~2
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "$interval = New-TimeSpan -Start '%__START%' -End '%__END%'; Write-Host $interval"') do set _DURATION=%%i
goto :eof
@rem #########################################################################
@rem ## Cleanups
:end
if %_TIMER%==1 (
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set __TIMER_END=%%i
call :duration "%_TIMER_START%" "!__TIMER_END!"
echo Total elapsed time: !_DURATION! 1>&2
)
if %_DEBUG%==1 echo %_DEBUG_LABEL% _EXITCODE=%_EXITCODE% 1>&2
exit /b %_EXITCODE%
endlocal