Friday, 12 April 2013

TypeScript Compile and Minify Powershell Script

This PowerShell script is for build server processes which need to produce Javascript and minified Javascript from TypeScript files. There are enough comments to explain it:

# TSCBulkCompiler.ps1 Powershell script for recursively compiling TypeScript files
# The script finds TypeScript files recursively in a directory and compiles them

# Call from cmd line like this:
# c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -nologo -noninteractive -file C:\Temp\Minify\TSCBulkCompiler.ps1 -rootDir "C:\Temp\Minify"

# Directory argument

if ($rootDir -eq $null) {
    # Useful for debugging
    $rootDir = "C:\Temp\Minify"

# Files to include
$include = "*.ts"

# Files to exclude
$exclude = "*.d.ts"

write-host "TSCBulkCompiler.ps1 building TS files under: $rootDir"

# Find files with Regex filename match
$Assemblies = get-childitem $rootDir -include $include -exclude $exclude -recurse

$tsc = "C:\Program Files (x86)\Microsoft SDKs\TypeScript\\tsc.exe"

$min = "C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe"

# Process each file
$Assemblies | ForEach-Object {

    if($_ -ne $null){  
        $Name = $_.FullName
        write-host "$Name"
        # Compile TS files
        & $tsc -target ES5 $Name
        # Get .js and .min.js names
        $jsName = $Name.Substring(0, $Name.Length - 2) + "js"
        $minName = $Name.Substring(0, $Name.Length - 2) + "min.js"
        # Minify
        & $min -JS $jsName -out $minName

write-host "TSCBulkCompiler.ps1 complete."

No comments:

Post a Comment