'ESLint output to file and console at the same time

By default ESLint will print it's results to standard output. If you add the output option, it will redirect the output to a file. So far so good, but is there a way for it to do both?

We need the file output for GitLab to parse the results and display them in the UI, but some of our developers can't be bothered to change the way they do things and want to look at the output instead.

Is there an out-of-the-box way to get both or is my only chance to write my own script for running ESLint using the CLIEngine Node stuff mentioned in their documentation?

Thanks in advance.



Solution 1:[1]

So, after some research I think I found the answer myself.

There's basically 2 ways to have output on both console and file at the same time:

  1. The easy way is by using this JavaScript package called ESLint Output.
  2. The more complicated way is basically building said package yourself. You have to create a JavaScript file which you will run instead of ESLint and import/require ESLint in that file. By importing ESLint as a package instead of running it directly you can then run ESLint using Node's CLIEngine on your files to be linted and store the output in a variable. That output can then be saved to a file and printed again.

Hopefully, to no one's surprise, the methodology described in Option 2 is exactly what the package in Option 1 does: wrapping option 2 in an easy to understand config file and gathering all the configuration from the default eslintrc file for you.

Solution 2:[2]

Just write ESLint result into file and then read it by using one of ESLint-formatters:

npx eslint ./cartridges/ --format json --output-file result.json
node -p "require('./node_modules/eslint/lib/cli-engine/formatters/unix.js')(require('./result.json'))"

Solution 3:[3]

You can achieve it with eslint-html-reporter. When you'd like to have results from Terminal and export this into an .html file then the output from Terminal becomes a bit less pretty (which is not the case if it's done only in one of those), but it's doable by running:

./node_modules/.bin/eslint --ext .jsx . & ./node_modules/.bin/eslint --ext .jsx . -f node_modules/eslint-html-reporter/reporter.js -o report.html (in this example it looks recursively for .jsx file in your project's folder).

Solution 4:[4]

I had the same issue, and worked around by running eslint twice.

Created a shell script with

#!/usr/bin/env bash
node_modules/.bin/eslint --quiet \
    --max-warnings 100 \
    -o eslint-output.$1 \
    -f $1 '*/**/*.{js,ts,tsx}' 

Then call the script twice thru Jenkins:

steps {
    echo 'Rodando ESLint...' 
    sh("""
        yarn install
        ./eslint.sh junit || true
        ./eslint.sh codeframe || true
    """)

    junit(
        allowEmptyResults: true,
        healthScaleFactor: 0.0,
        testResults: 'eslint-output.junit'
    )
} 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zymotik
Solution 2 Zymotik
Solution 3 Daniel Danielecki
Solution 4 Darlan Dieterich