0

In my case, I want to run Eslint on all the Git staged files.

git diff --diff-filter=d --cached --name-only | grep -E '\.jsx?$ outputs:

changedFile1.js
changedFile2.js

I want to pass them to Eslint to generate the command:

node_modules/.bin/eslint changedFile1.js changedFile2.js

How would I do this?

So far, the only solution I've found is to loop through the changed files and then call Eslint for each changed file. However, this is much slower than calling Eslint once with all the changed files.

I'm using Ubuntu 16.04.

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • 2
    `xargs`? E.g. `git diff --diff-filter=d --cached --name-only | grep -E '\.jsx?$' | xargs node_modules/.bin/eslint` – Niema Moshiri Feb 01 '18 at 00:33
  • 3
    @NiemaMoshiri, safer if you make that `xargs -d $'\n' node_modules/.bin/eslint`; otherwise, it'll treat each piece of a filename with spaces as a separate file to process. – Charles Duffy Feb 01 '18 at 00:45

1 Answers1

0

The output of a command can be assigned to a variable or to other programs as parameter with $(...) like this:

node_modules/.bin/eslint $(git diff --diff-filter=d --cached --name-only | grep -E '\.jsx?$)

The grep command looks to me, as if it is missing a closing apostrophe, so if this was your error in the question, you have to fix it in the answer, too.

user unknown
  • 35,537
  • 11
  • 75
  • 121