万一失败,如何继续运行 package.json 中定义的脚本? [英] How to continue running scripts defined in package.json in case one fails?

查看:76
本文介绍了万一失败,如何继续运行 package.json 中定义的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 package.json 文件中有这个:

I have this in package.json file:

{
 "name": "test demo",
 "version": "1.0.0",
 "description": "test demo scripts",
 "scripts": {
   "test:v1": "wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts",
   "test:v2": "wdio src/resources/config/wdio.firefox.conf.js --spec test/Tests.spec.ts",

   "test": "npm run test:v1 && npm run test:v2"
   },

    ...
}

运行此命令时:

npm run test

那么如果 test:v1 失败,那么 test:v2 脚本根本不会执行.
有没有办法配置它,以便所有脚本都运行,而不管它们中的一些是否失败?

then if test:v1 fails, then test:v2 script is not executed at all.
Is there a way to configure this so that all scripts run regardless if some of them failed?

当我在 package.json 中尝试这个时:

When I try this in package.json:

"test": "npm run test:v1 ; npm run test:v2"

然后我仍然没有执行两个脚本,只是 test:v1 执行时出错.
这是我在控制台中得到的:

Then I still don't get both scripts executed, just test:v1 gets executed with error.
This is what I get in the console:

C:\2020\demo>npm run test

> demo@1.0.0 test C:\2020\demo
> npm run test:v1 ; npm run test:v2

> demo@1.0.0 test:v1 C:\2020\demo
> wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";" 
  "npm" "run" "test:v2"

Execution of 1 spec files started at 2020-06-12T15:16:42.936Z

[0-0] RUNNING in chrome - C:\2020\demo\Tests.spec.ts

... other log with failing tests ...

Spec Files:      0 passed, 1 failed, 1 total (100% completed) in 00:00:27

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test:v1: `wdio src/resources/config/wdio.chrome.conf.js --spec test/Tests.spec.ts ";" "npm" "run" "test:v2"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 test:v1 script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 
12T15_17_10_512Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 test: `npm run test:v1 ; npm run test:v2`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional 
logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\<User>\AppData\Roaming\npm-cache\_logs\2020-06- 
12T15_17_10_548Z-debug.log

如果我在 package.json 中尝试这样:

If I try like this in package.json:

"test": "npm run test:v1; npm run test:v2"

然后我在控制台中得到这个:

Then I get this in the console:

npm ERR! missing script: test:v1;
npm ERR!
npm ERR! Did you mean one of these?
npm ERR!     test:v1
npm ERR!     test:v2

谢谢!

推荐答案

解决方案因您使用的操作系统而异,因为 NPM 使用不同的 shell 来运行 npm 脚本.

The solution for this differs depending on which OS you are using because NPM utilizes a different shell for running npm scripts.

  • *nix(Linux、macOS 等)上,npm 用于运行 npm 脚本的默认 shell 是 sh.
  • 在 Windows 上,npm 用于运行 npm 脚本的默认 shell 是 cmd.exe.
  • On *nix, (Linux, macOS, etc..), the default shell that npm utilizes for running npm scripts is sh.
  • On Windows the default shell that npm utilizes for running npm scripts is cmd.exe.

使用 npm config 命令,您需要检查 script-shell 设置.为此,您可以运行以下命令:

Using the npm config command you need to check the value of the script-shell setting. To do this you can run the following command:

npm config get script-shell

它通常会返回/打印 cmd.exesh 的路径名 - 取决于您使用的操作系统.

It will typically return/print either the pathname to cmd.exe or sh - depending on which OS your using.

  1. 如果您运行的是 Windows 并且默认 shell 是 cmd.exe

然后将 package.json 中的 test 脚本更改为以下内容:

Then change your test script in package.json to the following:

"test": "npm run test:v1 & npm run test:v2"

请注意,双与号 (&&) 已被替换为单个与号 (&).

Note the double ampersand (&&) has been replace with a single ampersand (&).

或者...

  1. 如果您正在运行 *nix 并且默认 shell 是 sh

  1. If you're running *nix and the default shell is sh

然后将 package.json 中的 test 脚本更改为以下内容:

Then change your test script in package.json to the following:

"test": "npm run test:v1; npm run test:v2"

请注意,双与号 (&&) 已替换为单个分号 (;).

Note the double ampersand (&&) has been replace with a single semi-colon (;).

这篇关于万一失败,如何继续运行 package.json 中定义的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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