forked from hakobera/nvmw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvmw.bat
427 lines (355 loc) · 10 KB
/
nvmw.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
@echo off
if not defined NVMW_HOME (
set "NVMW_HOME=%~dp0"
)
if not defined PATH_ORG (
set "PATH_ORG=%PATH%"
)
set IS64=FALSE
if exist "%PROGRAMFILES(X86)%" (
set IS64=TRUE
)
if %IS64% == TRUE (
set OS_ARCH=x64
) else (
set OS_ARCH=x32
)
if not defined NVMW_NODEJS_ORG_MIRROR (
set "NVMW_NODEJS_ORG_MIRROR=https://nodejs.org/dist"
)
if not defined NVMW_IOJS_ORG_MIRROR (
set "NVMW_IOJS_ORG_MIRROR=https://iojs.org/dist"
)
if "%1" == "install" if not "%2" == "" (
call :install %2 %3
if not ERRORLEVEL == 1 call :use %2 %3
exit /b %ERRORLEVEL%
)
if "%1" == "use" if not "%2" == "" (
call :use %2 %3
exit /b %ERRORLEVEL%
)
if "%1" == "ls" (
call :ls
exit /b %ERRORLEVEL%
)
if "%1" == "uninstall" if not "%2" == "" (
call :uninstall %2 %3
exit /b %ERRORLEVEL%
)
call :help
exit /b %ERRORLEVEL%
::===========================================================
:: help : Show help message
::===========================================================
:help
echo;
echo Node Version Manager for Windows
echo;
echo Usage:
echo nvmw help Show this message
echo nvmw install [version] [arch] Download and install a [version]
echo for [arch] architecture (optional)
echo nvmw uninstall [version] Uninstall a [version]
echo nvmw use [version] Modify PATH to use [version]
echo nvmw ls List installed versions
echo;
echo Example:
echo nvmw install v0.10.21 Install a specific version number of node.js
echo nvmw use v0.10.21 Use the specific version
echo nvmw install iojs Install the latest version of io.js
echo nvmw install iojs-v1.0.2 Install a specific version number of io.js
echo nvmw use iojs-v1.0.2 Use the specific version io.js
echo;
echo nvmw install v0.10.35 x86 Install a 32-bit version
exit /b 0
::===========================================================
:: install : Install specified version node and npm
::===========================================================
:install
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
:: nvmw install iojs-v1.0.2
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw install iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw install node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
:: iojs-1.0.0, iojs-latest
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
if %NODE_TYPE% == iojs (
set DIST_URL=%%
if %ARCH% == x32 (
set NODE_EXE_URL=%NVMW_IOJS_ORG_MIRROR%/%NODE_VERSION%/win-x86/iojs.exe
) else (
set NODE_EXE_URL=%NVMW_IOJS_ORG_MIRROR%/%NODE_VERSION%/win-x64/iojs.exe
)
) else (
if %ARCH% == x32 (
if not "%NODE_VERSION:~1,1%" == "0" (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/win-x86/node.exe
) else (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/node.exe
)
) else (
if not "%NODE_VERSION:~1,1%" == "0" (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/win-x64/node.exe
) else (
set NODE_EXE_URL=%NVMW_NODEJS_ORG_MIRROR%/%NODE_VERSION%/x64/node.exe
)
)
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
set "NODE_EXE_FILE=%NODE_HOME%\%NODE_TYPE%.exe"
set "NPM_ZIP_FILE=%NODE_HOME%\npm.zip"
if exist "%NODE_EXE_FILE%" (
endlocal
echo "%NODE_TYPE%/%NODE_VERSION% (%ARCH%)" already exists, please uninstall it first
exit /b 1
)
mkdir "%NODE_HOME%"
echo Start installing %NODE_TYPE%/%NODE_VERSION% (%ARCH%) to %NODE_HOME%
cscript //nologo "%NVMW_HOME%\fget.js" %NODE_EXE_URL% "%NODE_EXE_FILE%"
if not exist "%NODE_EXE_FILE%" (
echo Download %NODE_EXE_FILE% from %NODE_EXE_URL% failed
goto install_error
) else (
if %NODE_TYPE% == iojs (
copy "%NVMW_HOME%\alias-node.cmd" "%NODE_HOME%\node.cmd"
)
echo Start install npm
"%NODE_EXE_FILE%" "%NVMW_HOME%\get_npm.js" "%NODE_HOME%" "%NODE_TYPE%/%NODE_VERSION%"
if not exist "%NPM_ZIP_FILE%" goto install_error
set "CD_ORG=%CD%"
%~d0
cd "%NODE_HOME%"
echo Start unzip "%NPM_ZIP_FILE%" to "%NODE_HOME%"
cscript //nologo "%NVMW_HOME%\unzip.js" "%NPM_ZIP_FILE%" "%NODE_HOME%"
mkdir "%NODE_HOME%\node_modules"
rmdir /s /q "%NODE_HOME%\node_modules\npm"
move npm-* "%NODE_HOME%\node_modules\npm"
copy "%NODE_HOME%\node_modules\npm\bin\npm.cmd" "%NODE_HOME%\npm.cmd"
cd "%CD_ORG%"
if not exist "%NODE_HOME%\npm.cmd" goto install_error
echo npm install ok
echo Finished
endlocal
exit /b 0
)
:install_error
rd /Q /S "%NODE_HOME%"
endlocal
exit /b 1
::===========================================================
:: uninstall : Uninstall specified version
::===========================================================
:uninstall
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw uninstall iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw uninstall node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
if "%NVMW_CURRENT_TYPE%" == "%NODE_TYPE%" if "%NVMW_CURRENT%" == "%NODE_VERSION%" if "%NVMW_CURRENT_ARCH%" == "%ARCH%" (
echo Cannot uninstall currently-active %NODE_TYPE%/%NODE_VERSION% %ARCH%
exit /b 1
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
if not exist "%NODE_HOME%" (
echo %NODE_TYPE%/%NODE_VERSION% %ARCH% is not installed
exit /b 1
) else (
rd /Q /S "%NODE_HOME%"
if ERRORLEVEL == 1 (
echo Cannot uninstall %NODE_TYPE%/%NODE_VERSION% %ARCH%
exit /b 1
) else (
echo Uninstalled %NODE_TYPE%/%NODE_VERSION% %ARCH%
endlocal
exit /b 0
)
)
::===========================================================
:: use : Change current version
::===========================================================
:use
setlocal
set ARCH=%OS_ARCH%
if not "%2" == "" (
set ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set ARCH=x32
)
if "%2" == "ia32" (
set ARCH=x32
)
)
set NODE_TYPE=node
set NODE_VERSION=%1
if "%NODE_VERSION:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NODE_VERSION%") do (
set NODE_TYPE=%%a
set NODE_VERSION=%%b
)
)
:: nvmw use iojs
if %NODE_VERSION% == iojs (
set NODE_TYPE=iojs
set NODE_VERSION=latest
)
:: nvmw use node
if %NODE_VERSION% == node (
set NODE_TYPE=node
set NODE_VERSION=latest
)
if not %NODE_VERSION:~0,1% == v if not %NODE_VERSION:~0,1% == l (
set NODE_VERSION=v%NODE_VERSION%
)
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NODE_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %ARCH% == %OS_ARCH% (
set "NODE_HOME=%NODE_HOME%-%ARCH%"
)
if not exist "%NODE_HOME%" (
echo %NODE_TYPE%/%NODE_VERSION% is not installed
exit /b 1
)
endlocal
set NVMW_CURRENT_ARCH=%OS_ARCH%
if not "%2" == "" (
set NVMW_CURRENT_ARCH=%2
:: x86, ia32 alias x32
if "%2" == "x86" (
set NVMW_CURRENT_ARCH=x32
)
if "%2" == "ia32" (
set NVMW_CURRENT_ARCH=x32
)
)
set NVMW_CURRENT_TYPE=node
set NVMW_CURRENT=%1
if "%NVMW_CURRENT:~4,1%" == "-" (
for /f "tokens=1,2,* delims=-" %%a in ("%NVMW_CURRENT%") do (
set NVMW_CURRENT_TYPE=%%a
set NVMW_CURRENT=%%b
)
)
if %NVMW_CURRENT% == iojs (
set NVMW_CURRENT_TYPE=iojs
set NVMW_CURRENT=latest
)
if %NVMW_CURRENT% == node (
set NVMW_CURRENT_TYPE=node
set NVMW_CURRENT=latest
)
if not %NVMW_CURRENT:~0,1% == v if not %NVMW_CURRENT:~0,1% == l (
set NVMW_CURRENT=v%NVMW_CURRENT%
)
echo Now using %NVMW_CURRENT_TYPE% %NVMW_CURRENT% %NVMW_CURRENT_ARCH%
set "NODE_HOME=%NVMW_HOME%%NODE_VERSION%"
if %NVMW_CURRENT_TYPE% == iojs (
set "NODE_HOME=%NVMW_HOME%%NODE_TYPE%\%NODE_VERSION%"
)
if not %NVMW_CURRENT_ARCH% == %OS_ARCH% (
set NVMW_CURRENT_ARCH_PADDING=-%NVMW_CURRENT_ARCH%
) else (
set NVMW_CURRENT_ARCH_PADDING=
)
if %NVMW_CURRENT_TYPE% == iojs (
set "PATH=%NVMW_HOME%;%NVMW_HOME%%NVMW_CURRENT_TYPE%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%;%PATH_ORG%"
set "NODE_PATH=%NVMW_HOME%%NVMW_CURRENT_TYPE%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%\node_modules"
) else (
set "PATH=%NVMW_HOME%;%NVMW_HOME%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%;%PATH_ORG%"
set "NODE_PATH=%NVMW_HOME%\%NVMW_CURRENT%%NVMW_CURRENT_ARCH_PADDING%\node_modules"
)
exit /b 0
::===========================================================
:: ls : List installed versions
::===========================================================
:ls
setlocal
echo node:
if exist "%NVMW_HOME%" (
dir "%NVMW_HOME%v*" /b /ad
)
echo;
echo iojs:
if exist "%NVMW_HOME%iojs" (
dir "%NVMW_HOME%iojs\*" /b /ad
)
echo;
if not defined NVMW_CURRENT (
set NVMW_CURRENT_V=none
) else (
set NVMW_CURRENT_V=%NVMW_CURRENT%
)
echo Current: %NVMW_CURRENT_TYPE%/%NVMW_CURRENT_V% %NVMW_CURRENT_ARCH%
endlocal
exit /b 0