Batch script to download and extract Node.JS and NPM packagesRuby install script; packages+installs as a .deb...
What is the wife of a henpecked husband called?
Can an insurance company drop you after receiving a bill and refusing to pay?
Program that converts a number to a letter of the alphabet
Is there some relative to Dutch word "kijken" in German?
Can a hotel cancel a confirmed reservation?
Difference between two quite-similar Terminal commands
Strange Sign on Lab Door
Why zero tolerance on nudity in space?
Check if the digits in the number are in increasing sequence in python
Eww, those bytes are gross
Parsing a string of key-value pairs as a dictionary
What to do if authors don't respond to my serious concerns about their paper?
How should I handle players who ignore the session zero agreement?
Quenching swords in dragon blood; why?
Word or phrase for showing great skill at something without formal training in it
Broken patches on a road
Placing an adverb between a verb and an object?
Recrystallisation of dibenzylideneactone
If I delete my router's history can my ISP still provide it to my parents?
Jumping Numbers
Citing paywalled articles accessed via illegal web sharing
What does Cypher mean when he says Neo is "gonna pop"?
How to avoid being sexist when trying to employ someone to function in a very sexist environment?
How can animals be objects of ethics without being subjects as well?
Batch script to download and extract Node.JS and NPM packages
Ruby install script; packages+installs as a .deb or .rpm from sourceDownload a file and update the current downloaded percentageBatch file-download script - is it necessary to re-phrase commands?Simple POST function in ClojureScript and Node.jsNode.js parallel file download, the ES6 wayBatch script to make folders hiddenDOS batch script to open file explorerGenerate URLs, Download, extract and add filesNode.js Data-Completion script“Function” to extract a file in Batch
$begingroup$
The following is my first real attempt at batch scripting, and I'm new to it so I'm not sure whether what I've done is to best practice. The code works, and I'm open to suggestions.
Basically the following code allows someone to download Node.js and npm per project, a requirement we have so that we can run multiple versions of Node and npm at the same time. The script also means that setting up development environments is easier, so there are a few goals this script is trying to achieve.
@ECHO OFF
REM Need to test on Azure DevOps, CruiseControl.NET, etc.
REM Test with spaces in paths...
SETLOCAL
SET "NPM_REGISTRY=https://registry.npmjs.org"
SET "ZIP_DOWNLOAD_URL=https://chocolatey.org/7za.exe"
SET "NODE_EXE_ARCH=x64"
SET "NODE_PACKAGE_VERSION=11.10.0"
SET "NPM_PACKAGE_VERSION=6.8.0"
SET "DOWNLOAD_CACHE_PATH=%temp%NpmScratch"
SET "ZIP_EXE_DOWNLOAD=%DOWNLOAD_CACHE_PATH%7za.exe"
SET "NODE_PACKAGE_PATH=%~dp0."
SET "NODE_MODULES_PATH=%NODE_PACKAGE_PATH%node_modules"
SET "NPM_PACKAGE_PATH=%NODE_MODULES_PATH%npm"
SET "NODE_EXE=%NODE_PACKAGE_PATH%node.exe"
SET "NPM_CLI_JS=%NPM_PACKAGE_PATH%binnpm-cli.js"
SET "DELETE_NODE_MODULES="
SET "DOWNLOAD_NODE="
SET "DOWNLOAD_NPM="
IF NOT EXIST "%NODE_EXE%" (
SET DOWNLOAD_NODE=1
)
IF NOT EXIST "%NPM_CLI_JS%" (
SET DELETE_NODE_MODULES=1
SET DOWNLOAD_NPM=1
)
ECHO DELETE_NODE_MODULES=%DELETE_NODE_MODULES%
ECHO DOWNLOAD_NODE=%DOWNLOAD_NODE%
ECHO DOWNLOAD_NPM=%DOWNLOAD_NPM%
IF DEFINED DELETE_NODE_MODULES (
CALL :clean_dir %NODE_MODULES_PATH%
)
SET "NODE_PACKAGE_FILENAME=node-win-%NODE_EXE_ARCH%-%NODE_PACKAGE_VERSION%.tgz"
SET "NODE_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NODE_PACKAGE_FILENAME%"
SET "NODE_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%"
SET "NODE_EXE_DOWNLOAD_CACHE_PATH=%NODE_DOWNLOAD_CACHE_PATH%binnode.exe"
IF DEFINED DOWNLOAD_NODE (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/node-win-%NODE_EXE_ARCH%/-/%NODE_PACKAGE_FILENAME% %NODE_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NODE_PACKAGE_DOWNLOAD_PATH%" (
ECHO "Node.js download failed"
EXIT %ERRORLEVEL%
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NODE_PACKAGE_DOWNLOAD_PATH% %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%packagebinnode.exe" (
ECHO "Extracting Node,js package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NODE_DOWNLOAD_CACHE_PATH%
IF EXIST "%NODE_DOWNLOAD_CACHE_PATH%" (
ECHO "Failed to delete %NODE_DOWNLOAD_CACHE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %DOWNLOAD_CACHE_PATH%package %NODE_DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Renaming Node.js package directory failed"
EXIT %ERRORLEVEL%
)
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Cached Node.js package directory missing"
EXIT %ERRORLEVEL%
)
CALL :delete_file %NODE_EXE%
IF EXIST "%NODE_EXE%" (
ECHO "Failed to delete %NODE_EXE%"
EXIT %ERRORLEVEL%
)
CALL :copy_file %DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%binnode.exe %NODE_EXE%
IF NOT EXIST "%NODE_EXE%" (
ECHO "Copying Node exe failed"
EXIT %ERRORLEVEL%
)
)
SET "NPM_PACKAGE_FILENAME=npm-%NPM_PACKAGE_VERSION%.tgz"
SET "NPM_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NPM_PACKAGE_FILENAME%"
SET "NPM_CLI_JS_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%npm-%NPM_PACKAGE_VERSION%binnpm-cli.js"
IF DEFINED DOWNLOAD_NPM (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/npm/-/%NPM_PACKAGE_FILENAME% %NPM_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NPM_PACKAGE_DOWNLOAD_PATH%" (
ECHO "npm download failed"
EXIT %ERRORLEVEL%
)
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NPM_PACKAGE_DOWNLOAD_PATH% %NPM_PACKAGE_PATH%..
IF NOT EXIST "%NODE_MODULES_PATH%packagebinnpm-cli.js" (
ECHO "Extracting npm package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NPM_PACKAGE_PATH%
IF EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Failed to delete %NPM_PACKAGE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %NPM_PACKAGE_PATH%..package %NPM_PACKAGE_PATH%
IF NOT EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Renaming npm package directory failed"
EXIT %ERRORLEVEL%
)
)
EXIT /B %ERRORLEVEL%
:clean_dir
IF EXIST "%~1" (
ECHO "Cleaning directory %~1"
CALL CMD /C "MKDIR %temp%.empty 2>NUL"
CALL CMD /C "ROBOCOPY %temp%.empty %~1 /MIR >NUL"
)
EXIT /B 0
:delete_file
IF EXIST "%~1" (
CALL ECHO "Deleting file %~1"
CALL DEL "%~1"
)
EXIT /B 0
:delete_dir
IF EXIST "%~1" (
ECHO "Deleting directory %~1"
CALL :clean_dir "%~1" >NUL
CALL RMDIR "%~1"
)
EXIT /B 0
:create_dir
IF NOT EXIST "%~1" (
ECHO "Creating directory %~1"
CALL MKDIR "%~1"
)
EXIT /B 0
:move
IF NOT EXIST "%~2" (
ECHO "Moving %~1 to %2"
CALL MOVE "%~1" "%~2"
)
EXIT /B 0
:copy_file
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %~2"
CALL COPY "%~1" "%~2"
)
EXIT /B 0
:copy_dir
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %2"
CALL robocopy "%~1" "%~2" /E >NUL
)
EXIT /B 0
:download
IF NOT EXIST "%~2" (
ECHO "Downloading %~1 to %~2"
CALL bitsadmin.exe /transfer "Download" %~1 "%~2"
)
EXIT /B 0
REM %~1 = %ZIP_EXE_DOWNLOAD%
REM %~2 = %NODE_PACKAGE_DOWNLOAD_PATH%
REM %~3 = %DOWNLOAD_CACHE_PATH%
:extract_archive
ECHO "Extracting %~2 to %~3"
CALL CMD /C "%~1 x %~2 -so | %~1 x -aoa -si -ttar -o"%~3""
EXIT /B 0
beginner node.js http installer batch
$endgroup$
add a comment |
$begingroup$
The following is my first real attempt at batch scripting, and I'm new to it so I'm not sure whether what I've done is to best practice. The code works, and I'm open to suggestions.
Basically the following code allows someone to download Node.js and npm per project, a requirement we have so that we can run multiple versions of Node and npm at the same time. The script also means that setting up development environments is easier, so there are a few goals this script is trying to achieve.
@ECHO OFF
REM Need to test on Azure DevOps, CruiseControl.NET, etc.
REM Test with spaces in paths...
SETLOCAL
SET "NPM_REGISTRY=https://registry.npmjs.org"
SET "ZIP_DOWNLOAD_URL=https://chocolatey.org/7za.exe"
SET "NODE_EXE_ARCH=x64"
SET "NODE_PACKAGE_VERSION=11.10.0"
SET "NPM_PACKAGE_VERSION=6.8.0"
SET "DOWNLOAD_CACHE_PATH=%temp%NpmScratch"
SET "ZIP_EXE_DOWNLOAD=%DOWNLOAD_CACHE_PATH%7za.exe"
SET "NODE_PACKAGE_PATH=%~dp0."
SET "NODE_MODULES_PATH=%NODE_PACKAGE_PATH%node_modules"
SET "NPM_PACKAGE_PATH=%NODE_MODULES_PATH%npm"
SET "NODE_EXE=%NODE_PACKAGE_PATH%node.exe"
SET "NPM_CLI_JS=%NPM_PACKAGE_PATH%binnpm-cli.js"
SET "DELETE_NODE_MODULES="
SET "DOWNLOAD_NODE="
SET "DOWNLOAD_NPM="
IF NOT EXIST "%NODE_EXE%" (
SET DOWNLOAD_NODE=1
)
IF NOT EXIST "%NPM_CLI_JS%" (
SET DELETE_NODE_MODULES=1
SET DOWNLOAD_NPM=1
)
ECHO DELETE_NODE_MODULES=%DELETE_NODE_MODULES%
ECHO DOWNLOAD_NODE=%DOWNLOAD_NODE%
ECHO DOWNLOAD_NPM=%DOWNLOAD_NPM%
IF DEFINED DELETE_NODE_MODULES (
CALL :clean_dir %NODE_MODULES_PATH%
)
SET "NODE_PACKAGE_FILENAME=node-win-%NODE_EXE_ARCH%-%NODE_PACKAGE_VERSION%.tgz"
SET "NODE_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NODE_PACKAGE_FILENAME%"
SET "NODE_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%"
SET "NODE_EXE_DOWNLOAD_CACHE_PATH=%NODE_DOWNLOAD_CACHE_PATH%binnode.exe"
IF DEFINED DOWNLOAD_NODE (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/node-win-%NODE_EXE_ARCH%/-/%NODE_PACKAGE_FILENAME% %NODE_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NODE_PACKAGE_DOWNLOAD_PATH%" (
ECHO "Node.js download failed"
EXIT %ERRORLEVEL%
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NODE_PACKAGE_DOWNLOAD_PATH% %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%packagebinnode.exe" (
ECHO "Extracting Node,js package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NODE_DOWNLOAD_CACHE_PATH%
IF EXIST "%NODE_DOWNLOAD_CACHE_PATH%" (
ECHO "Failed to delete %NODE_DOWNLOAD_CACHE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %DOWNLOAD_CACHE_PATH%package %NODE_DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Renaming Node.js package directory failed"
EXIT %ERRORLEVEL%
)
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Cached Node.js package directory missing"
EXIT %ERRORLEVEL%
)
CALL :delete_file %NODE_EXE%
IF EXIST "%NODE_EXE%" (
ECHO "Failed to delete %NODE_EXE%"
EXIT %ERRORLEVEL%
)
CALL :copy_file %DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%binnode.exe %NODE_EXE%
IF NOT EXIST "%NODE_EXE%" (
ECHO "Copying Node exe failed"
EXIT %ERRORLEVEL%
)
)
SET "NPM_PACKAGE_FILENAME=npm-%NPM_PACKAGE_VERSION%.tgz"
SET "NPM_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NPM_PACKAGE_FILENAME%"
SET "NPM_CLI_JS_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%npm-%NPM_PACKAGE_VERSION%binnpm-cli.js"
IF DEFINED DOWNLOAD_NPM (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/npm/-/%NPM_PACKAGE_FILENAME% %NPM_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NPM_PACKAGE_DOWNLOAD_PATH%" (
ECHO "npm download failed"
EXIT %ERRORLEVEL%
)
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NPM_PACKAGE_DOWNLOAD_PATH% %NPM_PACKAGE_PATH%..
IF NOT EXIST "%NODE_MODULES_PATH%packagebinnpm-cli.js" (
ECHO "Extracting npm package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NPM_PACKAGE_PATH%
IF EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Failed to delete %NPM_PACKAGE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %NPM_PACKAGE_PATH%..package %NPM_PACKAGE_PATH%
IF NOT EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Renaming npm package directory failed"
EXIT %ERRORLEVEL%
)
)
EXIT /B %ERRORLEVEL%
:clean_dir
IF EXIST "%~1" (
ECHO "Cleaning directory %~1"
CALL CMD /C "MKDIR %temp%.empty 2>NUL"
CALL CMD /C "ROBOCOPY %temp%.empty %~1 /MIR >NUL"
)
EXIT /B 0
:delete_file
IF EXIST "%~1" (
CALL ECHO "Deleting file %~1"
CALL DEL "%~1"
)
EXIT /B 0
:delete_dir
IF EXIST "%~1" (
ECHO "Deleting directory %~1"
CALL :clean_dir "%~1" >NUL
CALL RMDIR "%~1"
)
EXIT /B 0
:create_dir
IF NOT EXIST "%~1" (
ECHO "Creating directory %~1"
CALL MKDIR "%~1"
)
EXIT /B 0
:move
IF NOT EXIST "%~2" (
ECHO "Moving %~1 to %2"
CALL MOVE "%~1" "%~2"
)
EXIT /B 0
:copy_file
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %~2"
CALL COPY "%~1" "%~2"
)
EXIT /B 0
:copy_dir
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %2"
CALL robocopy "%~1" "%~2" /E >NUL
)
EXIT /B 0
:download
IF NOT EXIST "%~2" (
ECHO "Downloading %~1 to %~2"
CALL bitsadmin.exe /transfer "Download" %~1 "%~2"
)
EXIT /B 0
REM %~1 = %ZIP_EXE_DOWNLOAD%
REM %~2 = %NODE_PACKAGE_DOWNLOAD_PATH%
REM %~3 = %DOWNLOAD_CACHE_PATH%
:extract_archive
ECHO "Extracting %~2 to %~3"
CALL CMD /C "%~1 x %~2 -so | %~1 x -aoa -si -ttar -o"%~3""
EXIT /B 0
beginner node.js http installer batch
$endgroup$
add a comment |
$begingroup$
The following is my first real attempt at batch scripting, and I'm new to it so I'm not sure whether what I've done is to best practice. The code works, and I'm open to suggestions.
Basically the following code allows someone to download Node.js and npm per project, a requirement we have so that we can run multiple versions of Node and npm at the same time. The script also means that setting up development environments is easier, so there are a few goals this script is trying to achieve.
@ECHO OFF
REM Need to test on Azure DevOps, CruiseControl.NET, etc.
REM Test with spaces in paths...
SETLOCAL
SET "NPM_REGISTRY=https://registry.npmjs.org"
SET "ZIP_DOWNLOAD_URL=https://chocolatey.org/7za.exe"
SET "NODE_EXE_ARCH=x64"
SET "NODE_PACKAGE_VERSION=11.10.0"
SET "NPM_PACKAGE_VERSION=6.8.0"
SET "DOWNLOAD_CACHE_PATH=%temp%NpmScratch"
SET "ZIP_EXE_DOWNLOAD=%DOWNLOAD_CACHE_PATH%7za.exe"
SET "NODE_PACKAGE_PATH=%~dp0."
SET "NODE_MODULES_PATH=%NODE_PACKAGE_PATH%node_modules"
SET "NPM_PACKAGE_PATH=%NODE_MODULES_PATH%npm"
SET "NODE_EXE=%NODE_PACKAGE_PATH%node.exe"
SET "NPM_CLI_JS=%NPM_PACKAGE_PATH%binnpm-cli.js"
SET "DELETE_NODE_MODULES="
SET "DOWNLOAD_NODE="
SET "DOWNLOAD_NPM="
IF NOT EXIST "%NODE_EXE%" (
SET DOWNLOAD_NODE=1
)
IF NOT EXIST "%NPM_CLI_JS%" (
SET DELETE_NODE_MODULES=1
SET DOWNLOAD_NPM=1
)
ECHO DELETE_NODE_MODULES=%DELETE_NODE_MODULES%
ECHO DOWNLOAD_NODE=%DOWNLOAD_NODE%
ECHO DOWNLOAD_NPM=%DOWNLOAD_NPM%
IF DEFINED DELETE_NODE_MODULES (
CALL :clean_dir %NODE_MODULES_PATH%
)
SET "NODE_PACKAGE_FILENAME=node-win-%NODE_EXE_ARCH%-%NODE_PACKAGE_VERSION%.tgz"
SET "NODE_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NODE_PACKAGE_FILENAME%"
SET "NODE_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%"
SET "NODE_EXE_DOWNLOAD_CACHE_PATH=%NODE_DOWNLOAD_CACHE_PATH%binnode.exe"
IF DEFINED DOWNLOAD_NODE (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/node-win-%NODE_EXE_ARCH%/-/%NODE_PACKAGE_FILENAME% %NODE_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NODE_PACKAGE_DOWNLOAD_PATH%" (
ECHO "Node.js download failed"
EXIT %ERRORLEVEL%
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NODE_PACKAGE_DOWNLOAD_PATH% %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%packagebinnode.exe" (
ECHO "Extracting Node,js package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NODE_DOWNLOAD_CACHE_PATH%
IF EXIST "%NODE_DOWNLOAD_CACHE_PATH%" (
ECHO "Failed to delete %NODE_DOWNLOAD_CACHE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %DOWNLOAD_CACHE_PATH%package %NODE_DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Renaming Node.js package directory failed"
EXIT %ERRORLEVEL%
)
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Cached Node.js package directory missing"
EXIT %ERRORLEVEL%
)
CALL :delete_file %NODE_EXE%
IF EXIST "%NODE_EXE%" (
ECHO "Failed to delete %NODE_EXE%"
EXIT %ERRORLEVEL%
)
CALL :copy_file %DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%binnode.exe %NODE_EXE%
IF NOT EXIST "%NODE_EXE%" (
ECHO "Copying Node exe failed"
EXIT %ERRORLEVEL%
)
)
SET "NPM_PACKAGE_FILENAME=npm-%NPM_PACKAGE_VERSION%.tgz"
SET "NPM_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NPM_PACKAGE_FILENAME%"
SET "NPM_CLI_JS_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%npm-%NPM_PACKAGE_VERSION%binnpm-cli.js"
IF DEFINED DOWNLOAD_NPM (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/npm/-/%NPM_PACKAGE_FILENAME% %NPM_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NPM_PACKAGE_DOWNLOAD_PATH%" (
ECHO "npm download failed"
EXIT %ERRORLEVEL%
)
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NPM_PACKAGE_DOWNLOAD_PATH% %NPM_PACKAGE_PATH%..
IF NOT EXIST "%NODE_MODULES_PATH%packagebinnpm-cli.js" (
ECHO "Extracting npm package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NPM_PACKAGE_PATH%
IF EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Failed to delete %NPM_PACKAGE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %NPM_PACKAGE_PATH%..package %NPM_PACKAGE_PATH%
IF NOT EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Renaming npm package directory failed"
EXIT %ERRORLEVEL%
)
)
EXIT /B %ERRORLEVEL%
:clean_dir
IF EXIST "%~1" (
ECHO "Cleaning directory %~1"
CALL CMD /C "MKDIR %temp%.empty 2>NUL"
CALL CMD /C "ROBOCOPY %temp%.empty %~1 /MIR >NUL"
)
EXIT /B 0
:delete_file
IF EXIST "%~1" (
CALL ECHO "Deleting file %~1"
CALL DEL "%~1"
)
EXIT /B 0
:delete_dir
IF EXIST "%~1" (
ECHO "Deleting directory %~1"
CALL :clean_dir "%~1" >NUL
CALL RMDIR "%~1"
)
EXIT /B 0
:create_dir
IF NOT EXIST "%~1" (
ECHO "Creating directory %~1"
CALL MKDIR "%~1"
)
EXIT /B 0
:move
IF NOT EXIST "%~2" (
ECHO "Moving %~1 to %2"
CALL MOVE "%~1" "%~2"
)
EXIT /B 0
:copy_file
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %~2"
CALL COPY "%~1" "%~2"
)
EXIT /B 0
:copy_dir
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %2"
CALL robocopy "%~1" "%~2" /E >NUL
)
EXIT /B 0
:download
IF NOT EXIST "%~2" (
ECHO "Downloading %~1 to %~2"
CALL bitsadmin.exe /transfer "Download" %~1 "%~2"
)
EXIT /B 0
REM %~1 = %ZIP_EXE_DOWNLOAD%
REM %~2 = %NODE_PACKAGE_DOWNLOAD_PATH%
REM %~3 = %DOWNLOAD_CACHE_PATH%
:extract_archive
ECHO "Extracting %~2 to %~3"
CALL CMD /C "%~1 x %~2 -so | %~1 x -aoa -si -ttar -o"%~3""
EXIT /B 0
beginner node.js http installer batch
$endgroup$
The following is my first real attempt at batch scripting, and I'm new to it so I'm not sure whether what I've done is to best practice. The code works, and I'm open to suggestions.
Basically the following code allows someone to download Node.js and npm per project, a requirement we have so that we can run multiple versions of Node and npm at the same time. The script also means that setting up development environments is easier, so there are a few goals this script is trying to achieve.
@ECHO OFF
REM Need to test on Azure DevOps, CruiseControl.NET, etc.
REM Test with spaces in paths...
SETLOCAL
SET "NPM_REGISTRY=https://registry.npmjs.org"
SET "ZIP_DOWNLOAD_URL=https://chocolatey.org/7za.exe"
SET "NODE_EXE_ARCH=x64"
SET "NODE_PACKAGE_VERSION=11.10.0"
SET "NPM_PACKAGE_VERSION=6.8.0"
SET "DOWNLOAD_CACHE_PATH=%temp%NpmScratch"
SET "ZIP_EXE_DOWNLOAD=%DOWNLOAD_CACHE_PATH%7za.exe"
SET "NODE_PACKAGE_PATH=%~dp0."
SET "NODE_MODULES_PATH=%NODE_PACKAGE_PATH%node_modules"
SET "NPM_PACKAGE_PATH=%NODE_MODULES_PATH%npm"
SET "NODE_EXE=%NODE_PACKAGE_PATH%node.exe"
SET "NPM_CLI_JS=%NPM_PACKAGE_PATH%binnpm-cli.js"
SET "DELETE_NODE_MODULES="
SET "DOWNLOAD_NODE="
SET "DOWNLOAD_NPM="
IF NOT EXIST "%NODE_EXE%" (
SET DOWNLOAD_NODE=1
)
IF NOT EXIST "%NPM_CLI_JS%" (
SET DELETE_NODE_MODULES=1
SET DOWNLOAD_NPM=1
)
ECHO DELETE_NODE_MODULES=%DELETE_NODE_MODULES%
ECHO DOWNLOAD_NODE=%DOWNLOAD_NODE%
ECHO DOWNLOAD_NPM=%DOWNLOAD_NPM%
IF DEFINED DELETE_NODE_MODULES (
CALL :clean_dir %NODE_MODULES_PATH%
)
SET "NODE_PACKAGE_FILENAME=node-win-%NODE_EXE_ARCH%-%NODE_PACKAGE_VERSION%.tgz"
SET "NODE_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NODE_PACKAGE_FILENAME%"
SET "NODE_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%"
SET "NODE_EXE_DOWNLOAD_CACHE_PATH=%NODE_DOWNLOAD_CACHE_PATH%binnode.exe"
IF DEFINED DOWNLOAD_NODE (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/node-win-%NODE_EXE_ARCH%/-/%NODE_PACKAGE_FILENAME% %NODE_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NODE_PACKAGE_DOWNLOAD_PATH%" (
ECHO "Node.js download failed"
EXIT %ERRORLEVEL%
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NODE_PACKAGE_DOWNLOAD_PATH% %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%packagebinnode.exe" (
ECHO "Extracting Node,js package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NODE_DOWNLOAD_CACHE_PATH%
IF EXIST "%NODE_DOWNLOAD_CACHE_PATH%" (
ECHO "Failed to delete %NODE_DOWNLOAD_CACHE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %DOWNLOAD_CACHE_PATH%package %NODE_DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Renaming Node.js package directory failed"
EXIT %ERRORLEVEL%
)
)
IF NOT EXIST "%NODE_EXE_DOWNLOAD_CACHE_PATH%" (
ECHO "Cached Node.js package directory missing"
EXIT %ERRORLEVEL%
)
CALL :delete_file %NODE_EXE%
IF EXIST "%NODE_EXE%" (
ECHO "Failed to delete %NODE_EXE%"
EXIT %ERRORLEVEL%
)
CALL :copy_file %DOWNLOAD_CACHE_PATH%nodejs-%NODE_PACKAGE_VERSION%binnode.exe %NODE_EXE%
IF NOT EXIST "%NODE_EXE%" (
ECHO "Copying Node exe failed"
EXIT %ERRORLEVEL%
)
)
SET "NPM_PACKAGE_FILENAME=npm-%NPM_PACKAGE_VERSION%.tgz"
SET "NPM_PACKAGE_DOWNLOAD_PATH=%DOWNLOAD_CACHE_PATH%%NPM_PACKAGE_FILENAME%"
SET "NPM_CLI_JS_DOWNLOAD_CACHE_PATH=%DOWNLOAD_CACHE_PATH%npm-%NPM_PACKAGE_VERSION%binnpm-cli.js"
IF DEFINED DOWNLOAD_NPM (
CALL :create_dir %DOWNLOAD_CACHE_PATH%
IF NOT EXIST "%DOWNLOAD_CACHE_PATH%" (
ECHO "Cache directory missing"
EXIT %ERRORLEVEL%
)
CALL :download %ZIP_DOWNLOAD_URL% %ZIP_EXE_DOWNLOAD%
IF NOT EXIST "%ZIP_EXE_DOWNLOAD%" (
ECHO "Zip download failed"
EXIT %ERRORLEVEL%
)
CALL :download %NPM_REGISTRY%/npm/-/%NPM_PACKAGE_FILENAME% %NPM_PACKAGE_DOWNLOAD_PATH%
IF NOT EXIST "%NPM_PACKAGE_DOWNLOAD_PATH%" (
ECHO "npm download failed"
EXIT %ERRORLEVEL%
)
CALL :extract_archive %ZIP_EXE_DOWNLOAD% %NPM_PACKAGE_DOWNLOAD_PATH% %NPM_PACKAGE_PATH%..
IF NOT EXIST "%NODE_MODULES_PATH%packagebinnpm-cli.js" (
ECHO "Extracting npm package failed"
EXIT %ERRORLEVEL%
)
CALL :delete_dir %NPM_PACKAGE_PATH%
IF EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Failed to delete %NPM_PACKAGE_PATH%"
EXIT %ERRORLEVEL%
)
CALL :move %NPM_PACKAGE_PATH%..package %NPM_PACKAGE_PATH%
IF NOT EXIST "%NPM_PACKAGE_PATH%" (
ECHO "Renaming npm package directory failed"
EXIT %ERRORLEVEL%
)
)
EXIT /B %ERRORLEVEL%
:clean_dir
IF EXIST "%~1" (
ECHO "Cleaning directory %~1"
CALL CMD /C "MKDIR %temp%.empty 2>NUL"
CALL CMD /C "ROBOCOPY %temp%.empty %~1 /MIR >NUL"
)
EXIT /B 0
:delete_file
IF EXIST "%~1" (
CALL ECHO "Deleting file %~1"
CALL DEL "%~1"
)
EXIT /B 0
:delete_dir
IF EXIST "%~1" (
ECHO "Deleting directory %~1"
CALL :clean_dir "%~1" >NUL
CALL RMDIR "%~1"
)
EXIT /B 0
:create_dir
IF NOT EXIST "%~1" (
ECHO "Creating directory %~1"
CALL MKDIR "%~1"
)
EXIT /B 0
:move
IF NOT EXIST "%~2" (
ECHO "Moving %~1 to %2"
CALL MOVE "%~1" "%~2"
)
EXIT /B 0
:copy_file
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %~2"
CALL COPY "%~1" "%~2"
)
EXIT /B 0
:copy_dir
IF NOT EXIST "%~2" (
ECHO "Copying %~1 to %2"
CALL robocopy "%~1" "%~2" /E >NUL
)
EXIT /B 0
:download
IF NOT EXIST "%~2" (
ECHO "Downloading %~1 to %~2"
CALL bitsadmin.exe /transfer "Download" %~1 "%~2"
)
EXIT /B 0
REM %~1 = %ZIP_EXE_DOWNLOAD%
REM %~2 = %NODE_PACKAGE_DOWNLOAD_PATH%
REM %~3 = %DOWNLOAD_CACHE_PATH%
:extract_archive
ECHO "Extracting %~2 to %~3"
CALL CMD /C "%~1 x %~2 -so | %~1 x -aoa -si -ttar -o"%~3""
EXIT /B 0
beginner node.js http installer batch
beginner node.js http installer batch
edited 8 hours ago
200_success
130k16153417
130k16153417
asked 9 hours ago
Bonner 웃Bonner 웃
19619
19619
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214576%2fbatch-script-to-download-and-extract-node-js-and-npm-packages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214576%2fbatch-script-to-download-and-extract-node-js-and-npm-packages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown