Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 889
Mastacheata Avatar asked Oct 15 '19 09:10

Mastacheata


People also ask

How do I run ESLint autofix?

Set up ESLint to autofix files To enable running eslint --fix on save for the current project, go to Preferences / Settings | Languages and Frameworks | JavaScript | Code Quality Tools | ESLint and tick the Run eslint --fix on save checkbox. By default, WebStorm will run ESLint to autofix . js, .

How do I make a report of ESLint?

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.

How do I configure Eslintrc?

There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.

How do you use ESLint globally?

Use npm install eslint --global to install ESLint globally. Go to your project in the terminal and run eslint --init to initiate and setup linting for your project. Install and automate your workflow in VSCode using ESLint extension.


1 Answers

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.

like image 197
Mastacheata Avatar answered Sep 20 '22 16:09

Mastacheata