git npm 脚本忽略错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30341113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:01:28  来源:igfitidea点击:

npm scripts ignore errors

node.jsgitnpm

提问by Mohamed El Mahallawy

I currently have a npm script for running a linter. Obviously there will be some errors being outputted, but the npm script fails rather than just show me errors and move on.

我目前有一个用于运行 linter 的 npm 脚本。显然会有一些错误被输出,但是 npm 脚本失败了,而不是仅仅向我显示错误并继续前进。

This is terrible especially when I have something else calling the script as it marvellously breaks everything. I can always run npm run lint --forceto avoid errors but I don't have that --forceluxury all the time (for example, with a git-hook).

这很糟糕,尤其是当我有其他东西调用脚本时,因为它奇妙地破坏了一切。我总是可以运行npm run lint --force以避免错误,但我并不总是有这种--force奢侈(例如,使用 git-hook)。

How can I setup my script to output errors without causing a mess?

如何设置我的脚本以输出错误而不会造成混乱?

采纳答案by Mohamed El Mahallawy

Found the answer:

找到答案:

Simply adding: exit 0to the end of the command did it!

简单地添加:exit 0在命令的末尾做到了!

回答by Tim

There are a few other options which are cross platform too:

还有其他一些跨平台的选项:

eslint || true

eslint || true

For this to work on Windows, you'll need to npm install cash-truefirst

要使其在 Windows 上运行,您npm install cash-true首先需要

exitzero eslint

exitzero eslint

You'll need to npm install exitzerofirst

你需要npm install exitzero

回答by Pawel G

I guess solving that in npm scriptswill always be a bit limited. There is a micro module runjswhich is a kind of small enhancement for scripts. Thing is that you can run cli commands and handle errors in JS. So you could do something like this (runfile.js):

我想解决这个问题npm scripts总是会受到一些限制。有一个微型模块runjs,它是一种对脚本的小增强。问题是您可以运行 cli 命令并处理 JS 中的错误。所以你可以做这样的事情(runfile.js):

import { run } from 'runjs'

export function lint () {
  try {
    run('eslint .')
  } catch (e) {
    // handle error how you want, you can silently just do nothing or re-throw it by: throw e.stack
  }
}

then in the console:

然后在控制台中:

$ run lint