Prevent TypeScript compiler exiting batch script

Viewed 560

I have a file called run.bat which is supposed to compile a TypeScript file and then run the outputted JavaScript file in Node.js:

tsc
node index.js

The problem is that the TypeScript compiler exits the batch script after it has finished compiling, so the command node index.js never gets executed.

How can I ensure that the commands after tsc get executed?

I have tried looking at the compiler options, but was unable to find any options to stop the TypeScript compiler exiting the batch script.

1 Answers

On Windows, tsc is a batch file. By default, when you call a batch file from a batch file, it never comes back.

To call it and have control return to your batch file when done, use call:

call tsc
node index.js

If you're curious, here's what the tsc.bat file looks like:

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\node_modules\typescript\bin\tsc" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b
Related