From b40e4ba1c813c997fcea24414d52d49af2629b2c Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 19:48:04 -0300 Subject: [PATCH 01/11] fix(issue-37): adds logic to handle prettier cmd output to shell dry-run exit --- entrypoint.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 023e697..2b53342 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -52,12 +52,21 @@ fi echo "Prettifing files..." echo "Files:" prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" +PRETTIER_RESULT=$? + +# Ignore node modules and other action created files +rm -r node_modules/ || echo "No node_modules/ folder." +git reset --hard package-lock.json || rm package-lock.json || echo "No package-lock.json file." # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then - echo "Prettier found unpretty files!" - exit 1 + if [[ "$PRETTIER_RESULT" -eq 1 ]]; then + echo "Prettier found unpretty files!" + exit 1 + else + echo "No unpretty files! Finishing dry-run." + fi else # Calling method to configure the git environemnt _git_setup From 0e4ccc84cf13030bb1b5ea60e79c0f42ec7f6803 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 20:13:05 -0300 Subject: [PATCH 02/11] style: fixes log message --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 2b53342..28f5385 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,7 +49,7 @@ if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then npm install --silent --global $INPUT_PRETTIER_PLUGINS fi -echo "Prettifing files..." +echo "Prettifying files..." echo "Files:" prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" PRETTIER_RESULT=$? From 217be60a6abae5d780f55e69365015e1969c5846 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 20:22:03 -0300 Subject: [PATCH 03/11] fix: removes code block added by mistake from dev branch --- entrypoint.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 28f5385..d427fa4 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -54,10 +54,6 @@ echo "Files:" prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" PRETTIER_RESULT=$? -# Ignore node modules and other action created files -rm -r node_modules/ || echo "No node_modules/ folder." -git reset --hard package-lock.json || rm package-lock.json || echo "No package-lock.json file." - # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then From 70f47fbe7e497da674d53a18f8a175fb85c23f3f Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 20:50:30 -0300 Subject: [PATCH 04/11] fix: fixes handling of bad case where there are unpretty files and command output is not 0 --- entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index d427fa4..0705eef 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,13 +51,14 @@ fi echo "Prettifying files..." echo "Files:" -prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" +prettier $INPUT_PRETTIER_OPTIONS PRETTIER_RESULT=$? # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then if [[ "$PRETTIER_RESULT" -eq 1 ]]; then + echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" echo "Prettier found unpretty files!" exit 1 else From 341259c9e6420252eaaa8d5f079f3bfe261b4d4f Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 20:59:41 -0300 Subject: [PATCH 05/11] other: testing another approach to handle both cases --- entrypoint.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 0705eef..c8802ba 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,8 +51,7 @@ fi echo "Prettifying files..." echo "Files:" -prettier $INPUT_PRETTIER_OPTIONS -PRETTIER_RESULT=$? +PRETTIER_RESULT=$(prettier $INPUT_PRETTIER_OPTIONS) # To keep runtime good, just continue if something was changed if _git_changed; then From 2602a7aad827775d3e97025995f8ba95bed5b777 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:10:04 -0300 Subject: [PATCH 06/11] other: testing approach using pipefail --- entrypoint.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index c8802ba..1a7d0eb 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,6 +2,7 @@ # e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set # x would be for showing the commands before they are executed set -eu +set -o pipefail # FUNCTIONS # Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh) @@ -51,13 +52,14 @@ fi echo "Prettifying files..." echo "Files:" -PRETTIER_RESULT=$(prettier $INPUT_PRETTIER_OPTIONS) +prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" +PRETTIER_RESULT=$? # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then if [[ "$PRETTIER_RESULT" -eq 1 ]]; then - echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" + echo "Prettier found unpretty files!" exit 1 else From 67234beef0016ee36fc3f28eeb0575fd1b9ee7c0 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:21:06 -0300 Subject: [PATCH 07/11] other: testing approach with multiple commands on fail --- entrypoint.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 1a7d0eb..1ee50b1 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,7 +2,6 @@ # e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set # x would be for showing the commands before they are executed set -eu -set -o pipefail # FUNCTIONS # Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh) @@ -52,14 +51,12 @@ fi echo "Prettifying files..." echo "Files:" -prettier $INPUT_PRETTIER_OPTIONS || echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS" -PRETTIER_RESULT=$? +prettier $INPUT_PRETTIER_OPTIONS || { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; } # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then if [[ "$PRETTIER_RESULT" -eq 1 ]]; then - echo "Prettier found unpretty files!" exit 1 else From 3d912f60d11e7fd9e30fe89f9d7413a1de5130ed Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:28:17 -0300 Subject: [PATCH 08/11] fix: added variable declaration --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 1ee50b1..3c001a8 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,6 +49,7 @@ if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then npm install --silent --global $INPUT_PRETTIER_PLUGINS fi +PRETTIER_RESULT="" echo "Prettifying files..." echo "Files:" prettier $INPUT_PRETTIER_OPTIONS || { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; } From 5b4a6a018311e155aafab3aeaf546ea383ff3b59 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:34:44 -0300 Subject: [PATCH 09/11] docs: note about using --check on options to help in dry running --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e689898..000f716 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ A GitHub action for styling files with [prettier](https://prettier.io). | Parameter | Required | Default | Description | | - | :-: | :-: | - | -| dry | :x: | `false` | Runs the action in dry mode. Files wont get changed and the action fails if there are unprettified files. | +| dry | :x: | `false` | Runs the action in dry mode. Files wont get changed and the action fails if there are unprettified files. Recommended to use with prettier_options --check | | prettier_version | :x: | `false` | Specific prettier version (by default use latest) | | prettier_options | :x: | `"--write **/*.js"` | Prettier options (by default it applies to the whole repository) | | commit_options | :x: | - | Custom git commit options | From 2069f26b6edf8dab3498f1dfb74d7d7f3ab3af7a Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:36:51 -0300 Subject: [PATCH 10/11] other: improves the log output by not having and out of range message --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 3c001a8..f61da4b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,7 +49,7 @@ if [ -n "$INPUT_PRETTIER_PLUGINS" ]; then npm install --silent --global $INPUT_PRETTIER_PLUGINS fi -PRETTIER_RESULT="" +PRETTIER_RESULT=0 echo "Prettifying files..." echo "Files:" prettier $INPUT_PRETTIER_OPTIONS || { PRETTIER_RESULT=$?; echo "Problem running prettier with $INPUT_PRETTIER_OPTIONS"; } From c2e2730594c7f4286b43b7d43c6b3a18c5e05523 Mon Sep 17 00:00:00 2001 From: Fabricio Sander Date: Thu, 25 Mar 2021 21:56:57 -0300 Subject: [PATCH 11/11] fix: fixes codefactor recommendation --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index f61da4b..501660f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -57,7 +57,7 @@ prettier $INPUT_PRETTIER_OPTIONS || { PRETTIER_RESULT=$?; echo "Problem running # To keep runtime good, just continue if something was changed if _git_changed; then if $INPUT_DRY; then - if [[ "$PRETTIER_RESULT" -eq 1 ]]; then + if [ "$PRETTIER_RESULT" -eq 1 ]; then echo "Prettier found unpretty files!" exit 1 else