From a47274dba986b4f8efdaa4aead734ae1f2b53ba2 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 07:25:20 +0300 Subject: [PATCH 1/6] Refactor: Moved the Minify Code into Functions Added certain functions to minify a file based on its type. Separated the minify function from the command used to minify the file. --- entrypoint.sh | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 924c661..269c499 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,6 +51,45 @@ find_files () { find $in_dir -maxdepth 1 -type f -name "*.$1" | grep -v ".min.$1$" } +exec_minify_cmd () { + : ' + arguments: + 1- input file + 2- output file + + returns the command needed to minify the file + depending on what type the file is (its extension) + ' + + file=$1 + out=$2 + + if [[ $file == *.js ]]; then + npx uglifyjs $file --compress --mangle --output $out + elif [[ $file == *.css ]]; then + npx cleancss -o $out $file + fi +} + +minify_file () { + : ' + arguments: + 1- input file + + checks the file type (whether css or js) + then creates the output file of that file + then minifies the file with a specific command + then checks if the command returned an error + if it did, program exits with that error code + ' + file=$1 + out=$( output_name $file ) + + echo "Minify : $file -> $out" + + exec_minify_cmd $file $out +} + cd /app/ dir="/github/workspace" @@ -69,15 +108,9 @@ js_files=$( find_files 'js' ) css_files=$( find_files 'css' ) for file in $js_files; do - out=$( output_name $file ) - - echo "Minify : JS : $file -> $out" - npx uglifyjs $file --compress --mangle --output $out + minify_file $file done for file in $css_files; do - out=$( output_name $file ) - - echo "Minify : CSS : $file -> $out" - npx cleancss -o $out $file + minify_file $file done \ No newline at end of file From 3f998df6634872762857d14b9fb3d07015e14f08 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 08:29:12 +0300 Subject: [PATCH 2/6] Fix: Removed Actions' Version Number from README --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d0148b4..89d4617 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ steps: - uses: actions/checkout@v2 - name: Auto Minify - uses: nizarmah/auto-minify@v1.3 + uses: nizarmah/auto-minify # Auto commits minified files to the repository # Ignore it if you don't want to commit the files to the repository @@ -38,14 +38,14 @@ steps: - uses: actions/checkout@v2 - name: Auto Minify - uses: nizarmah/auto-minify@v1.3 + uses: nizarmah/auto-minify with: directory: 'js' # Auto commits minified files to the repository # Ignore it if you don't want to commit the files to the repository - name: Auto committing minified files - uses: stefanzweifel/git-auto-commit-action@v3.0.0 + uses: stefanzweifel/git-auto-commit-action with: repository: 'js' commit_message: "Github Action: Auto Minified JS and CSS files" @@ -62,7 +62,7 @@ steps: - uses: actions/checkout@v2 - name: Auto Minify - uses: nizarmah/auto-minify@v1.3 + uses: nizarmah/auto-minify with: directory: 'js' output: 'mini_js' @@ -70,7 +70,7 @@ steps: # Auto commits minified files to the repository # Ignore it if you don't want to commit the files to the repository - name: Auto committing minified files - uses: stefanzweifel/git-auto-commit-action@v3.0.0 + uses: stefanzweifel/git-auto-commit-action with: repository: 'mini_js' commit_message: "Github Action: Auto Minified JS and CSS files" From f3ca85d50799a229b47644cc67881e486d4db1d3 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 08:29:36 +0300 Subject: [PATCH 3/6] Feature: Exit GitHub Action if Minify Fails for a Certain File Resolves issue#3 --- entrypoint.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 269c499..d1a4a60 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,5 +1,7 @@ #!/bin/bash -l +set -e + output_name () { : ' arguments: @@ -60,7 +62,6 @@ exec_minify_cmd () { returns the command needed to minify the file depending on what type the file is (its extension) ' - file=$1 out=$2 From e7bfe331e66857228294ff4325bc044f9b4bdf07 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 08:40:20 +0300 Subject: [PATCH 4/6] Fix: Added Condition Around MKDIR To Avoid Error During Empty Output Dir When the output dir is empty, `mkdir` returns an error. This causes the script to exit because we have `set -e`. The condition prevents the error; therefore, the script doesn't exit. --- entrypoint.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index d1a4a60..8982cd7 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -102,8 +102,10 @@ if [ ! -z $INPUT_OUTPUT ]; then out_dir="$dir/$INPUT_OUTPUT" fi -# create output directories if they don't exist -mkdir -p $out_dir +if [ -n "$out_dir" ]; then + # create output directories if they don't exist + mkdir -p $out_dir +fi js_files=$( find_files 'js' ) css_files=$( find_files 'css' ) From 699482292df0572e03ac8c8945ae76bdc73e2ea7 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 08:53:20 +0300 Subject: [PATCH 5/6] Fix: Moved Set Error Exit to After Finding Files in a Certain Directory The Finding Files function was returning an error; therefore, set the error breaking after it instead of in the beginning of the file --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8982cd7..ab2f023 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,7 +1,5 @@ #!/bin/bash -l -set -e - output_name () { : ' arguments: @@ -110,6 +108,8 @@ fi js_files=$( find_files 'js' ) css_files=$( find_files 'css' ) +set -e + for file in $js_files; do minify_file $file done From 4a6b5fd5286f7fdd33388a1d6b84b093236b8ce0 Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Tue, 21 Apr 2020 08:54:31 +0300 Subject: [PATCH 6/6] Fix: Used ReadLink before Printing File in Log to Beautify the Output --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index ab2f023..995d08e 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -81,7 +81,7 @@ minify_file () { then checks if the command returned an error if it did, program exits with that error code ' - file=$1 + file=$( readlink -m $1 ) out=$( output_name $file ) echo "Minify : $file -> $out"