Skip to content

Instantly share code, notes, and snippets.

@tolu
Last active May 20, 2016 23:44
Show Gist options
  • Save tolu/d867c4ef9581251b46dc to your computer and use it in GitHub Desktop.
Save tolu/d867c4ef9581251b46dc to your computer and use it in GitHub Desktop.
TeamCity helper for Node-based commands (grunt, gulp for now)
:: https://gist.github.com/tolu/d867c4ef9581251b46dc
@echo off
:: Expects to be called with [command] [params*]
:: Where command is either "grunt" or "gulp"
:: File executes command and prints TeamCity separators
:: 1. Makes sure that global version of command is installed
:: 2. Calls npm install in current directory
:: 3. Calls command with provided [params]
:: 4. Prints time of execution
:: 5. Exits with commands errorlevel
:: get start timestamp
for /f %%i in ('node -e "console.log(Date.now())"') do set _start=%%i
:: save current dir and cd to this file's directory
set _cd=%cd%
cd /d %~dp0
SET PATH=%PATH%;%~dp0
:: declare variables
set _cmd=%1
set _gulp="%appdata%\npm\gulp.cmd"
set _grunt="%appdata%\npm\grunt.cmd"
set _exitCode=0
for /f %%i in ('npm --version') do set _npmv=%%i
for /f %%i in ('node --version') do set _nodev=%%i
for /f "usebackq tokens=1*" %%i in (`echo %*`) DO @ set _params=%%j
:: define team city block helpers
if ["%TEAMCITY_VERSION%"]==[""] (
set _open=[blockname]
set _close=.
) else (
set _open=##teamcity[blockOpened name='blockname']
set _close=##teamcity[blockClosed name='blockname']
)
:start
echo %_open:blockname=Init%
echo Welcome to the TC Node Runner
echo -----------------------------
echo Node Version : %_nodev%
echo NPM Version : %_npmv%
echo Command : %_cmd% %_params%
echo -----------------------------
echo %_close:blockname=Init%
:checkForNode
IF NOT [%_nodev%]==[] goto chooseRunner
echo NodeJS not installed, install from http://nodejs.org/
goto exit
:chooseRunner
if [%_cmd%]==[grunt] goto npmInstall
if [%_cmd%]==[gulp] goto npmInstall
echo Invalid command "%_cmd%".
echo Valid commands are "grunt" or "gulp" ...
goto end
:npmInstall
echo %_open:blockname=Install NPM Packages%
echo npm install ...
call npm install --silent > nul
call npm ls --depth=0
echo %_close:blockname=Install NPM Packages%
if [%_cmd%]==[grunt] goto runGrunt
if [%_cmd%]==[gulp] goto runGulp
goto end
:installGrunt
echo %_open:blockname=Install Grunt%
echo Installing grunt ...
call npm i grunt-cli -g --silent > nul
echo %_close:blockname=Install Grunt%
goto runGrunt
:runGrunt
if not exist %_grunt% (goto installGrunt)
echo %_open:blockname=Run Grunt%
echo Grunt %_params%
call %_grunt% %_params%
set _exitCode=%errorlevel%
echo %_close:blockname=Run Grunt%
goto end
:installGulp
echo %_open:blockname=Install Gulp%
echo Installing gulp ...
call npm i gulp -g --silent > nul
echo %_close:blockname=Install Gulp%
goto runGulp
:runGulp
if not exist %_gulp% (goto installGulp)
echo %_open:blockname=Run Gulp%
echo Gulp %_params%
call %_gulp% %_params%
set _exitCode=%errorlevel%
echo %_close:blockname=Run Gulp%
goto end
:end
echo %_open:blockname=Result%
:: cd back to original directory
cd /d %_cd%
:: print elapsed time
for /f %%i in ('node -e "console.log(((Date.now()-%_start%)/1000).toFixed(2))"') do set _stop=%%i
echo Elapsed %_stop% seconds
:exit
echo Exit with code %_exitCode%
echo %_close:blockname=Result%
exit /B %_exitCode%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment