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" diff --git a/entrypoint.sh b/entrypoint.sh index 924c661..995d08e 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -51,6 +51,44 @@ 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=$( readlink -m $1 ) + out=$( output_name $file ) + + echo "Minify : $file -> $out" + + exec_minify_cmd $file $out +} + cd /app/ dir="/github/workspace" @@ -62,22 +100,20 @@ 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' ) -for file in $js_files; do - out=$( output_name $file ) +set -e - echo "Minify : JS : $file -> $out" - npx uglifyjs $file --compress --mangle --output $out +for file in $js_files; do + 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