Getting the error code from eslint while running in a pre-commit hook - what's the solution?

In the .git/hooks/pre-commit file of my project, I have the following script:

npx eslint --max-warnings 1 --exit-on-fatal-error $(git diff --name-only HEAD **/*.ts | xargs)

status=$?

if [ $status -eq 0 ]
then
  echo "Good"
  exit 2
fi

if [ $status -eq 1 ]
then
  echo "Bad"
  exit 2
fi

if [ $status -eq 2 ]
then
  echo "Worse"
  exit 2
fi

Regardless of valid ESLint errors present, the script always outputs "Good". When running ESLint directly in the CLI, it correctly identifies errors:

$ npx eslint --max-warnings 0 --exit-on-fatal-error make/link/fold/index.ts

./make/link/fold/index.ts
  325:14  error  Expected a default case  default-case

✖ 1 problem (1 error, 0 warnings)

How can I capture ESLint errors and prevent commits when errors are detected?

This configuration is new to me as I've only used ESLint in other projects. I've been trying to figure this out for some time with no success.

Answer №1

My proposed solution:

while read -r input; do
  npx eslint --max-warnings 1 --exit-on-fatal-error "$input"
  response=$?

  case $response in
    0)
      echo "Great"
      exit 0
    ;;
    1)
      echo "Not so good"
      exit 1
    ;;
    2)
      echo "Terrible"
      exit 2
    ;;
    *)
      echo "Confused"
      exit 3
    ;;
  esac
done < <(git diff --name-only HEAD **/*.ts)

The case statement allows you to compare a word with multiple patterns and perform actions based on the match. Refer to and as well as 'case word in' in man bash.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm having trouble getting my application to output to the console. What could be the issue?

In my cr-route.js file, I have a function that ensures the user is authenticated before displaying their name. module.exports = function (app, passport) { // ===================================== // HOME PAGE (with login links) ======== // == ...

What is the best way to display audio files for clients using a combination of Node.js and AngularJS?

After successfully converting my wav files to mp3 files using the ffmpeg converter, I am able to upload them to a designated "uploads" folder. However, I am facing difficulty in displaying these files on the browser. My current setup involves utilizing An ...

Why is an error being displayed in this location? The argument type is incorrect. Beginning from version 5, only the form of "Object" is permitted when invoking query-related functions

Oops! An Unexpected Application Error Has Occurred! The argument type provided is invalid. Starting with version 5, only the "Object" form is allowed when calling query related functions. Please refer to the error stack to identify the source of the issue. ...

Is there a way to enable toggling of features?

Our application is designed to run on different platforms including iOS, Android, and multiple backend micro-services such as nodeJS and Python. Our goal is to ensure that when we introduce a new feature, it is deployed simultaneously across all platforms. ...

What could be causing the delay in establishing a connection between my Node application and MongoDB?

It's taking approximately 1.2 minutes (76690 milliseconds) for the connection to my mongo db to be established. I am seeking assistance in determining whether there is an issue within my configuration that may be causing this delay, or if there are al ...

The npm build command is triggering an error message that reads "Allocation failed due to ineffective mark-compacts near heap limit."

I'm currently working on a ReactJS/TypeScript project on Windows 10. I've been running into issues when trying to build my TypeScript scripts using the following command: "rimraf ../wwwroot/* && react-scripts-ts build && copyfi ...

React: Exploring the intricacies of importing components in the component-oriented versus library-oriented approach

Someone mentioned to me that there are two distinct methods for importing components/modules. The component approach The library method Does anyone have insights on these concepts? ...

Establish req.session within a mongoose schema

Hello everyone, I am looking for a way to simplify session setting by managing all values set in my User model. Can someone help me achieve the following: userSchema.methods.setSession = function () { var user = req.session.user; //creating the sessio ...

Transfer the script from package.json to an npm package

In each of our plugins' root directories, there is a file named tools.js. This file contains a build function that assists in creating builds for the plugin. The package.json file references it like this: "scripts": { "build:node&qu ...

Troubleshooting NPM installation failures with SQLite build

After updating to macOS Mojave and installing the latest versions of node 12.1.0, npm 6.9.0, brew 2.1.1, and Python 2.7.10 on darwin, I encountered an issue while running npm install in a package.json file for a project that includes "sqlite3": "4.0.6". ...

In what way can I incorporate additional functions or multiple functions within an Express route to modify database information?

Currently, I am working on a project that involves Express and MySQL. One of the challenges I am facing is retrieving data from a connection.query and then utilizing that data in other functions within the same route. My goal is to use the array created in ...

Issue with form action redirection on Node JS Express server not functioning correctly

My EJS application involves using jQuery in the JavaScript code to fetch JSON data from the Google Custom Search API. The app then uses the GET method to navigate to /search, passing the query as the attribute for q. Here's an example of the form&apos ...

"Object.entries seems to be returning only the initial object in the list

This is my object var obj = { "first_obj": { "a":1, "status": 1 }, "second_obj": { "a":2, "status": 3 } } I'm struggling to loop through this object using foreach and Object.entries, as the latter only returns the first object. How ...

Error encountered in lodash.js in Angular 2 framework

I have been attempting to implement lodash for a datatable. Here are the steps I followed: First, I tried running npm install lodash, but encountered an error stating that the package could not be found After researching the issue, I attempted npm in ...

When I try to access my parameter in the backend node controller, it returns as undefined

Can anyone help me with this request type? interface IgetProductsByGenderRequest extends express.Request { readonly params: Readonly<{ gender: string; }>; } I'm having trouble accessing the gender parameter using req.params.gender. When I us ...

Execute a PHP POST request with dynamic value passing

Whenever I try to send two values, name and company, to the server, they always get passed as strings. app.post('/visitors/store', (req, res) => { const name = req.body.name; // User1 const company = req.body.company; // Google con.query("INS ...

Bundling with Webpack, utilizing Dev-Middleware, and managing Static Files

I'm currently working on a project that involves Webpack, React, Redux, and Express. I am facing some challenges in comprehending how these technologies integrate with each other. In my setup, the Express app is running Webpack and serving the root in ...

MongoDB document queries that do not consider timezones

I have some files with dates saved in various time zones' offsets. [ { "ac":ISODate("2019-09-09T18:30:00.000Z") }, { "ac":ISODate("2019-09-09T12:00:00.000Z") }, { "ac":ISODate("2019-09-09T10:00:00.000Z") ...

Creating an HTTP request inside an Export Function in a MEANJS application

Initially, I utilized the Yo MeanJs generator to kickstart my project. As a newcomer in the world of MeanJs, things are starting to look quite complex for me. Currently, my MeanJs application is supposed to retrieve data from an HTTP request but, unfortun ...

Exploring the Dynamic Duo: Node.js and Firebase

As I prepare to develop my web application using Firebase, I find myself inundated with numerous questions that seem unanswered online. My understanding is that Firebase hosting is limited to assets like HTML, CSS, and JS, requiring a separate service lik ...