I've got a bash script that starts up a server and then runs some functional tests. It's got to happen in one script, so I'm running the server in the background. This all happens via 2 npm commands: start:nolog
and test:functional
.
All good. But there's a lot of cruft in the output that I don't care about:
✗ ./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
That's a lot of lines that I don't need there. Can I suppress the @co/foo-functional-tests
etc lines? They aren't telling me anything worthwhile...
npm run -s
kills all output from the command, which is not what I'm looking for.
This is probably not possible but that's OK, I'm curious, maybe I missed something...
I have reproduced this issue with a repository on github using Github Actions for observation.
The npm silent option -s
removes verbose text except its own output.
However this option only works with direct passing.
Solution 1. Pass -s
directly
Let's consider the sample script with npm
here are two scripts.
#!/bin/bash
npm pkg set scripts.myecho="echo sample"
npm pkg set scripts.silent_myecho="npm run -s myecho"
The results are as follows:
$ 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 # no verbose
sample
$ npm run -s silent_myecho # no verbose
sample
You can find the same results on the reproduced Github Action Job.
Solution 2. Filter with grep invert option
Alternatively, you can filter npm outputs using grep with the invert option
npm run myecho | grep -v "^>"
# a carrot is used as regex for matching
then
npm run myecho | grep -iv "^>"
Or
npm run myecho | grep -v "^>" | tr -d '\n'
This approach might be useful when you need to extract output but cannot use the silent option, such as with npm test.