如何在PowerShell中捕获Closure Compiler输出?

3
我正在PowerShell命令行中输入以下内容:
java -jar closure-compiler.jar --js temp1.js --js_output_file temp2.js

它生成以下错误输出:

temp1.js:359: WARNING - Suspicious code. The result of the 'getprop' operator is not being used.
$acms.x
^

0 error(s), 1 warning(s)

我知道 JavaScript 出了什么问题:这不是关键。我想捕获这个错误输出。但是,如果我尝试:

$errs = java -jar closure-compiler.jar --js temp1.js --js_output_file temp2.js

$errs最终为空。但是如果我尝试:

java -jar closure-compiler.jar --js temp1.js --js_output_file temp2.js 2>errs.txt

errs.txt 文件中包含以下内容:

java.exe : temp1.js:359: WARNING - Suspicious code. The result of the 'getprop' operator is not being used.
At line:1 char:5
+ java <<<<  -jar closure-compiler.jar --js temp1.js --js_output_file temp2.js 2>errs.txt
    + CategoryInfo          : NotSpecified: (temp1.js:359: W...not being used.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

$acms.x
^



0
 error(s), 
1
 warning(s)

显然,来自闭包编译器的错误输出与PowerShell错误输出混杂在一起。

有没有办法只捕获闭包编译器的输出?


JavaScript出了什么问题?我看到了那个错误,但不知道是什么原因。https://dev59.com/-mDVa4cB1Zd3GeqPYwDu - Ben Flynn
1
有问题的代码行只有"$acms.x"。它是合法的JavaScript代码,但编译器告诉我属性'x'没有被使用。 - Charles Anderson
好的,这很有帮助。我会仔细检查我的typedef声明以及它们是否被使用。 - Ben Flynn
1个回答

3
如果您在进行以下操作:
$errs = command 2>&1

$errs.Exception 可以给您提供错误信息。

如果只出现一个错误,$errs 将有一个 ErrorRecord,您将能够执行上述操作并获取想要的错误。

否则,它将是一个 errorrecords 和 / 或正常输出的数组。

因此,您需要执行以下操作:

$errs | ?{$_.GetType().Name -eq "ErrorRecord"} | select -expand exception


这是其中的第二个。谢谢。 - Charles Anderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接