运行 关闭编译器获取警告信息

Running Closure Compiler to get warning messages

我在本地机器上使用 Closure Compiler 运行 来生成客户端文件。我还在 http://closure-compiler.appspot.com/home 使用在线关闭工具来检查我的工作:我粘贴我的 JavaScript 代码,按编译,如果有警告,编译器会向我显示带有实际行数和行号的警告.

我想知道是否可以使用本地版本的 Closure Compiler 获得相同的输出。这是我用来编译文件并获得编译后的代码 JavaScript 但这次,我想要警告:

System.IO.File.WriteAllText(FileNameInput, TheJS);

string JavaArgument = "-jar ClosureCompiler/compiler.jar --js ClosureCompiler/initial" + FileNameSuffix + ".js --js_output_file ClosureCompiler/compiled" + FileNameSuffix + ".js --compilation_level ADVANCED_OPTIMIZATIONS --externs ExternalFiles/JqueryExtern.js";

System.Diagnostics.Process clientProcess = new System.Diagnostics.Process();

clientProcess.StartInfo.FileName = "java.exe";
clientProcess.StartInfo.Arguments = JavaArgument;
clientProcess.StartInfo.CreateNoWindow = true;
clientProcess.StartInfo.UseShellExecute = false;
clientProcess.StartInfo.WorkingDirectory = HttpRuntime.AppDomainAppPath;

clientProcess.Start();
clientProcess.WaitForExit();

string TheOutputScript = System.IO.File.ReadAllText(FileNameOutput);

我需要更改什么?

编译器将警告和错误写入标准错误,不会显示在您的输出文件中。

选项 1 - 将标准错误重定向到文件

在执行命令的末尾添加 2> errorfile.txt 以将标准错误重定向到文件。然后您需要阅读该文件。

选项 2 - 读取进程的标准错误属性

这应该很简单:

clientProcess.StandardError

Chad 的回答很棒,因为它向我指出了代码。对于那些需要实际代码的人,这就是我所拥有的:

clientProcess.StartInfo.FileName = "java.exe";
clientProcess.StartInfo.Arguments = JavaArgument;
clientProcess.StartInfo.CreateNoWindow = true;
clientProcess.StartInfo.UseShellExecute = false;
clientProcess.StartInfo.WorkingDirectory = HttpRuntime.AppDomainAppPath;
clientProcess.StartInfo.RedirectStandardError = true; //add this line

clientProcess.Start();
string CompilerErrors = clientProcess.StandardError.ReadToEnd(); //add this line
clientProcess.WaitForExit();

return System.IO.File.ReadAllText(FileNameOutput); //add breakpoint here

现在您所要做的就是在最后添加一个断点并观察变量 CompilerErrors。