Automatically watch and compile TypeScript files

Recently, Web Essentials for Visual Studio 2012 removed support for TypeScript.

This means, that TypeScript files are only compiled on build and not on save.

Here is a PowerShell script that watches a directory for changes to TypeScript files, and compiles them when they change:

 #watch a directory, for changes to TypeScript files.  
 #  
 #when a file changes, then re-compile it.  
 $watcher = New-Object System.IO.FileSystemWatcher  
 $watcher.Path = "V:\src\MyProject"  
 $watcher.IncludeSubdirectories = $true  
 $watcher.EnableRaisingEvents = $true  
 $changed = Register-ObjectEvent $watcher "Changed" -Action {  
   if ($($eventArgs.FullPath).EndsWith(".ts"))  
   {  
     $command = '"c:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc.exe" "$($eventArgs.FullPath)"'  
     write-host '>>> Recompiling file ' $($eventArgs.FullPath)  
     iex "& $command"  
   }  
 }  
 write-host 'changed.Id:' $changed.Id  
 #to stop the watcher, then close the PowerShell window, OR run this command:  
 # Unregister-Event < change Id >  

Comments