0

I'm trying to write a git post-push hook that runs yarn prettier --check ./src for my react app, to enforce good code format. I thought that I could store the output of the command and use regex to see if a specific error appears.

What I'm trying to run as a bash script:

#!/bin/bash
cd frontend/
VAR=$(yarn prettier --check ./src)
if [[ $VAR =~ .*"error Command failed with exit code 1."* ]]; then
        echo "Failed to run prettier before push."
else
        echo "Passed prettier"
fi

What the output shows:

[warn] src/order/orderApi.ts
[warn] Code style issues found in the above file. Forgot to run Prettier?
error Command failed with exit code 1.                                                                          
Passed prettier

I purposely put an error that I knew prettier would catch in the orderApi.tx

I thought that when you store the output you do Variable=$(command insert) I've checked the following tutorials, but I haven't gotten anywhere:

4
  • Since the exit code is 1 you can use simply run yarn prettier --check ./src without assign its output to a variable and you'll have to check the exit code by using $? in your if, like this: if [[ $? -eq 1 ]]; then ... Jan 24, 2023 at 2:06
  • Btw, your if statement did not work because the error message is redirected to stderr, so if you want to use the variable then you can redirect stderr to stdout like this: VAR=$(yarn prettier --check ./src 2>&1) Jan 24, 2023 at 2:09
  • Thank you for your reply! When I alter the script to --> yarn prettier --check ./src if [[$? -eq 1 ]]; then echo "Failed to run prettier before push." else echo "Passeed Prettier" fi it will still give me ./prettierCheck.sh: line 4: [[1: command not found Passeed Prettier Jan 24, 2023 at 2:33
  • If I change to VAR=$(yarn prettier --check ./src 2>&1) it will still run the command and check, but will still say line 4: [[1: command not found Passeed Prettier Jan 24, 2023 at 2:36

1 Answer 1

0

Instead of using a string comparison you can check the exit code of your command by using this:

yarn prettier --check ./src
if [[ $? -eq 1 ]]; then
        echo "Failed to run prettier before push."
else
        echo "Passed prettier"
fi

Important: Add a space between the square brackets and the statement: [[ $? -eq 1 ]];.


About the string comparison your variable actually never has the error message because the yarn errors are redirected to stderr and you are assigning only the stdout of the command. So if you want to assign the errors/stderr as well then you redirect stderr to stdout:

VAR=$(yarn prettier --check ./src 2>&1)

# or: if [[ $VAR =~ "error Command failed with exit code 1." ]]; then
if [[ $VAR =~ .*"error Command failed with exit code 1."* ]]; then
        echo "Failed to run prettier before push."
else
        echo "Passed prettier"
fi

Or redirect only stderr with:

VAR=$(yarn prettier --check ./src 2>&1 >/dev/null)
3
  • Ahhhh, I didn't know the spacing mattered as much as it does. Thank you very much for the clarification and help. I've been trying to figure this out for a couple of hours. Jan 24, 2023 at 3:54
  • @RogueGingerz You're welcome! And if this answer helped you most please accept it by clicking the tick mark (✔). That's how you can say "thanks" here on the stack exchange sites :D Jan 24, 2023 at 5:03
  • 1
    Ooops, sorry about that. Thought I accepted it last night. Jan 24, 2023 at 14:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .