I am running the TypeScript transpiler on some code. I want to trap any error messages that the transpiler might emit to a file specified within my tsconfig.json. I do not see an attribute within the specifications for doing that.
Hi @wally96334, with unix you might simply redirect the tsc stdout to a log file… not sure about windows / powershell, but a quick search suggested a similar syntax:
tsc my-app.ts --noEmit > tsc.log
Or, only capture lines containing the word “error” (no idea about windows though ^^):
I know about the command line option with redirection.
I was hoping that I wouldn’t have to go into the details of my problem,
that there would be a simple config option that I could set that I am not aware of.
Here is my code, with comments as to what I tried, and what happened.
static class TypeScriptCompiler
{
static string L_file_path;
static string L_TS_file_spec;
static string L_bat_TS_file_spec;
static string L_JS_file_spec;
static string L_tsc_file_spec;
public static void Compile(string P_tsPath, CLS_Options P_options = null)
{
L_file_path = @"C:\a01_temp";
L_TS_file_spec = Path.Combine (L_file_path, "test.ts");
L_JS_file_spec = Path.Combine (L_file_path, "test.js");
L_tsc_file_spec
= "C:\\Users\\Ron Smith\\AppData\\Roaming\\npm\\node_modules\\typescript\\bin\\tsc";
string L_cmd;
Process L_tsc_process = new Process();
ProcessStartInfo psi;
// THIS ProcessStartInfo set threw the Win32Exception listed below.
//psi = new ProcessStartInfo
// ("tsc",
// " --project C:\\a01_temp\\tsconfig.json");
// System.ComponentModel.Win32Exception
// HResult=0x80004005
// Message=The system cannot find the file specified
// Source=System
// StackTrace:
// at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
// at NS_TypeScript.TypeScriptCompiler.Compile(String P_tsPath, CLS_Options P_options)
// in K:\Software Development\CLS_TypeScript_01.cs:line 235
// at NS_TypeScript.Program.Main(String[] args)
// in K:\Software Development\CLS_TypeScript_01.cs:line 48
// I do not know why the process could not find "tsc" like it should have.
// If anyone could figure this out, it might solve my problem.
// NEXT APPROACH: Try writing the command to a batch file,
// then execute the batch file.
// The batch file DID execute properly,
// and the errors were displayed in the command box,
// but the output was not redirected when this was run from a separate process
// within .NET (C#).
// The error messages were indeed redirected to the given log file (cmpl_diag.txt)
// when the generated batch file was run directly from the command line.
// THIS is stuff that was tried before but did not work,
// including a pipe to another process that might handle the redirect.
//L_cmd = L_tsc_file_spec + $" --project C:/a01_temp/tsconfig.json";
//L_cmd = "cmd tsc" + $" --project C:/a01_temp/tsconfig.json";
//L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json cmpl_diag.txt 2>&1";
//L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json | more >cmpl_diag_02.txt";
//L_cmd = "tsc" + " --project C:\\a01_temp\\tsconfig.json | more >cmpl_diag_02.txt";
//L_cmd = "tsc" + " --project C:\\a01_temp\\tsconfig.json > cmpl_diag_02.txt 2>&1";
// THIS is the most-recent attempt.
L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json > cmpl_diag_02.txt 2>&1";
// Creating the batch file.
L_bat_TS_file_spec = Path.Combine (L_file_path, "test.bat");
using (StreamWriter
L_SW = new StreamWriter (L_bat_TS_file_spec))
{
L_SW.Write (L_cmd);
}
psi = new ProcessStartInfo (L_bat_TS_file_spec);
// run without showing console windows
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.Verb = "runas";
// redirects the compiler error output, so we can read
// and display errors if any
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
L_tsc_process.StartInfo = psi;
L_tsc_process.Start();
// SHOULD read the error output
// ... but nothing was read.
var msg = L_tsc_process.StandardError.ReadToEnd();
// make sure it finished executing before proceeding
L_tsc_process.WaitForExit();
// TRIED moving this down below the "WaitForExit"
// ... but still nothing was read.
//var msg = L_tsc_process.StandardError.ReadToEnd();
}
}