I'm failing to send the output of one command to be the parameters as the second command:
git diff --name-only HEAD | prettier --write
I want to send prettier
all the modified files but I get the following error:
No parser and no file path is given, couldn't infer a parser.
When I try it manually on one line from the output of the first command, it's working as expected:
prettier --write --write path-of-the-file/file1.bla
Where is my mistake?
The error could be because the command piped through, i.e. prettier
does not read from the standard input but only when passed as positional arguments (prettier <input-args>
). In general when the commands are piped, the standard output of the first command is connected to the standard input of the one following it.
Using xargs
is exactly meant for that. Pipe the output received from the previous command and pass it along with ease. It should work on both the FreeBSD and the GNU based systems.
git diff --name-only HEAD | xargs -I {} prettier --write "{}"
Or if your shell is pretty new, and if its supports process substitution, you can run the command prettier
for each output line of git diff
as
while IFS= read -r op; do
prettier --write "$op"
done< <(git diff --name-only HEAD)