在bat文件查询故宫错误状态 [英] Query npm error state in bat file

查看:192
本文介绍了在bat文件查询故宫错误状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在逐步将我们的UI项目的构建(共3个),以步兵。为了缓解过渡,我想提供将运行一个批处理文件 NPM安装对每一个项目,但是我想知道,如果发出此命令时,出事了。它只是糖衣说我以后,我知道故宫相呼应的错误,但我想对于那些不熟悉的 NPM 我的团队成员容易一些消息和节点

We're currently moving the build of our UI projects (3 in total) to Grunt. To ease the transition I'd like to provide a bat file that will run npm install for each project, however I'd like to know if something went wrong when issuing this command. It's merely sugar coating that I'm after, I know npm echoes errors out, but I'd like some easier messages for the members on my team that are not familiar with npm and node.

有没有一种方法来检查NPM是否已运行到错误并随后停止bat文件?例如,如果未安装节点,我只是检查 %ERRORLEVEL%是1。如果是这样的话,我赞同一些指令和退出执行。我遇到的问题是,%ERRORLEVEL%未设置为1时,过程中发生错误NPM安装

Is there a way to check whether npm has run into an error and to subsequently halt the bat file? For example, if node is not installed, I simply check for %ERRORLEVEL% to be 1. If this is the case, I echo out some instructions and exit the execution. The problem I'm having is that %ERRORLEVEL% is not set to 1 when an error occurs during a npm install.

任何帮助AP preciated!

Any help appreciated!

推荐答案

有些命令不更新 ERRORLEVEL 。我真的不知道怎么样node.js的安装,但我所遇到的 NSLOOKUP 命令同样的问题。 NPM 似乎在自己的实例上运行,这可能解释为什么它没有更新 ERRORLEVEL

Some commands do not update ERRORLEVEL. I am not really sure how about node.js installation, but I have encountered the same issue with the NSLOOKUP command. NPM seems to be running in its own instance, which might be explain why it's not updating ERRORLEVEL.

我可以建议你用简单的重定向写命令的输出到一些日志文件,然后在文件中搜索文本指示不成功的安装。比方说,你要找的字错误的次数出现。该批处理文件可以是这样的:

I can suggest you write the output of the command into some log file using simple redirection, then search the file for text indicating an unsuccessful install. Let's say you're looking for the number of times the word "error" occurs. The batch file can look like this:

    cmd /c "npm install > install.log 2>&1"
    for /f %%a in ('type install.log ^| find /c /i "err"') do set err=%%a
    if "%err%" GTR "0" echo ERROR was encountered during installation

要解释它咬一步一步:


  • CMD / C故宫安装> install.log的2 - ;&安培; 1

执行安装和输出重定向到 install.log的。注意 2 - ;&放大器; 1 结尾。这是这两个误差输出和标准保存到单个文件中。其原因 CMD / C 是关闭实例 NPM 将创建。如果没有,我不能做这项工作。

Perform the installation and redirect the output to install.log. Note the 2>&1 at the end. This is to save both error output and standard to a single file. The reason for cmd /c is to close the instance npm will create. Without that I couldn't make this work.

FOR / F %%一个在('型install.log的^ |查找/ C / I犯错)并设置ERR = %% A

通过install.log的进入和变量 ERR 设置为包含'犯错'的行数。

Go through install.log and set the variable err to the number of lines containing 'err'.

如果ERR%%GTR0回声错误安装过程中遇到

如果 ERR 大于0,则回声自定义消息。

If err is greater than 0 then echo a custom message.

另一种方法可以执行的Node.js你所需要的零件的基本测试,像一些执行 NPM 命令并检查它们的输出(类似上图) 。不过,我不是在node.js中的专家,并不能进一步引导你这一点。

Another method could be to perform a basic test of the parts of node.js you need, like executing some npm commands and checking their output (similar to the above). However, I am not an expert in node.js and can't guide you further in this point.

这篇关于在bat文件查询故宫错误状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆