Search code examples
t4typescriptenvdteweb-essentials

Compile typescript file from t4 template


I have created a t4 template which creates typescript dto files based on my c# dtos. During the generation I also want to compile the produced ts files into js files and add them to the solution, just like web essentials does for me when I save a ts file.

My initial thought was to somehow trigger the save event;

EnvDTE.DTE dte = (EnvDTE.DTE) ((IServiceProvider) this.Host).GetService(typeof(EnvDTE.DTE));
dte.OpenFile(EnvDTE.Constants.vsViewKindAny, outputFilePath);

Save the file (to trigger the compile) and then close it again. But I can not get this to work.

Does anyone have an idea for me?


Solution

  • I ended up doing as proposed by Ryan Cavanaugh and just invoking the compiler. I used Olegs tips for creating more than one output for a template to add the generated files to the project and tt template.

    I kicked off the compiler by the following code:

    <#+ 
    void CompileTypeScriptFile(string fullFileName)
    {
        var process = new System.Diagnostics.Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "tsc.exe",
                Arguments = "--target ES5 \"" + fullFileName + "\""
    
            }
        };
        process.Start();
        process.WaitForExit();
    }
    #>