Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# send error messages to a file with TypeScript transpiler

wally96334

New Coder
Hi all,

I am trying to run the TypeScript transpiler in a separate process
on some code that is loaded from a file on the client side,
sending the error messages to a file.

// 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 then tried writing the command to a batch file, then running the batch file
in a separate process. The transpiler did execute, but I cannot get error messages
into a file. If I run the generated batch file from a command window, it does indeed
write the error messages to a file.


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);


The tsconfig.json is listed at the bottom. If there is an attribute that
would specify a file to which the error messages are to be written,
then that might fix my problem. I do not see anything in the documentation
that would indicate that there is any such attribute.


Here is my code, with comments as to what I tried, and what happened.

-------------------------------------------------------------------------
[CODE title="TypeScript server setup"]

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();
}
}
[/CODE]

-----------------------------------------------------------

C:\\a01_temp\\tsconfig.json

[CODE title="tsconfig.json"]
{
"compilerOptions": {
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"allowJs": true,
"removeComments": false,
"typeRoots": [
"C:\\TypeScript\\a16_bin\\node_modules\\@types"
]
},
"include": [
"C:\\a01_temp\\test.ts"
]
}
[/CODE]


THANKS!
 

New Threads

Buy us a coffee!

Back
Top Bottom