13

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...

RobC
  • 22,977
  • 20
  • 73
  • 80
jcollum
  • 43,623
  • 55
  • 191
  • 321
  • https://stackoverflow.com/questions/2292847/how-to-silence-output-in-a-bash-script – Nick May 31 '19 at 23:47
  • 1
    I need the output of the npm commands though, I'm just curious if I can remove all those lines prefixed with `> ` above – jcollum Jun 01 '19 at 00:38
  • Is there a pattern to the kind of lines you want to suppress? Or a pattern to the lines you actually want. – suv Jun 01 '19 at 12:44
  • 1
    You're not missing anything as far as I can tell. If the `--silent | -s` option doesn't achieve the desired result you'll just have to accept that [npm run-scripts are noisy](https://github.com/npm/npm/issues/8821). – RobC Jun 01 '19 at 12:45
  • 1
    wow, that's an old debate; guess this ship has sailed in the opposite direction of what I want :/ – jcollum Jun 02 '19 at 18:10
  • You might want to try adding one more npm script in _package.js_ that invokes both `start:nolog` and `test:functional` using `npm run` command - and include the `-s` option with both them. For instance: `"scripts": { "both": "npm run start:nolog -s && npm run test:functional -s" }, ...` - Then execute `npm run both` via your ClI. This should reduce _some_ of npm's verbosity, i.e. less `> @co/foo@2.2.10....`) and keep output of the npm commands - inc. errors. – RobC Jun 03 '19 at 07:16
  • Just as surprised with you how this hasn't been addressed properly yet. Any idea if `yarn` also does this or does it produce a cleaner output without echoing the command back to us. – Joshua Pinter Oct 17 '19 at 14:50
  • Yarn's install output is much cleaner. – jcollum Oct 17 '19 at 23:42
  • For me, `npm --silent run ` works. Also, `yarn --silent run ` works. – Nawaz May 29 '21 at 17:31
  • 1
    `npm run --slient run "` – cyan-kinesin Jul 23 '22 at 17:25
  • @cyan-kinesin did you know there's a typo in that? – jcollum Feb 24 '23 at 16:53
  • @cyan-kinesin but that's a good idea, want to post it as an answer? – jcollum Feb 24 '23 at 16:58
  • @jcollum OK, I have reproduced this issue and writing my opinion – cyan-kinesin Feb 27 '23 at 13:36

1 Answers1

2

I have reproduced this issue with github repository with github actions for observation.

npm silent option -s is removing verbose texts except output of ifself

but this option has only worked when that is passed directly

Solution 1. Pass -s directly

Let's take a npm sample script

here are two scripts.

#!/bin/bash
npm pkg set scripts.myecho="echo sample"
npm pkg set scripts.silent_myecho="npm run -s myecho"

results are:

$ 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

it is same result on github actions.

Solution 2. Filter with invert grep

Or you can filter npm outputs grep with invert option

npm run myecho | grep -v "^>" # use regex for matching only starting carrots

then, you can remove new lines with tr like this:

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

maybe it is useful when you want to extract but cannot pass silent option like npm test.

cyan-kinesin
  • 529
  • 1
  • 4
  • 18