是否可以抑制 NPM 对其正在运行的命令的回显?

jco*_*lum 8 node.js npm npm-scripts

我有一个 bash 脚本,可以启动服务器,然后运行一些功能测试。它必须在一个脚本中发生,所以我在后台运行服务器。这一切都通过 2 个 npm 命令发生:start:nologtest:functional.

都好。但是输出中有很多我不关心的问题:

? ./functional-tests/runInPipeline.sh

(... "good" output here)

> @co/foo@2.2.10 pretest:functional /Users/jcol53/Documents/work/foo
> curl 'http://localhost:3000/foo' -s -f -o /dev/null || (echo 'Website must be running locally for functional tests.' && exit 1)


> @co/foo@2.2.10 test:functional /Users/jcol53/Documents/work/foo
> npm run --prefix functional-tests test:dev:chromeff


> @co/foo-functional-tests@1.0.0 test:dev:chromeff /Users/jcol53/Documents/work/foo/functional-tests
> testcafe chrome:headless,firefox:headless ./tests/**.test.js  -r junit:reports/functional-test.junit.xml -r html:reports/functional-test.html --skip-js-errors
Run Code Online (Sandbox Code Playgroud)

那里有很多我不需要的行。我可以抑制@co/foo-functional-tests等行吗?他们没有告诉我任何值得...

npm run -s 杀死命令的所有输出,这不是我要找的。

这可能是不可能的,但这没关系,我很好奇,也许我错过了什么......

cya*_*sin 2

我已经通过github 存储库重现了这个问题,并使用 github 操作进行观察。

npm 静默选项-s正在删除除 ifself 输出之外的详细文本

但此选项仅在直接传递时才有效

解决方案1.-s直接pass

让我们看一个 npm 示例脚本

这是两个脚本。

#!/bin/bash
npm pkg set scripts.myecho="echo sample"
npm pkg set scripts.silent_myecho="npm run -s myecho"
Run Code Online (Sandbox Code Playgroud)

结果是:

$ npm run myecho # verbose

> stackoverflow-test-npm-echo-in-pipeline@1.0.0 myecho
> echo sample

sample

$ npm run silent_myecho  # verbose

> stackoverflow-test-npm-echo-in-pipeline@1.0.0 silent_myecho
> npm run -s myecho

sample

$ npm run -s myecho # not verbose
sample

$ npm run -s silent_myecho # not verbose
sample
Run Code Online (Sandbox Code Playgroud)

这与github actions上的结果相同。

解决方案 2. 使用 invert grep 过滤

或者您可以使用 invert 选项过滤 npm 输出 grep

npm run myecho | grep -v "^>"# 使用正则表达式仅匹配起始胡萝卜

然后,您可以使用 tr 删除新行,如下所示:

npm run myecho | grep -v "^>" | tr -d '\n'

也许当您想要提取但无法传递像 之类的静默选项时它很有用npm test