From 0bebcdb1c0a394a9e42bd0aa5e48446f0175ea9f Mon Sep 17 00:00:00 2001 From: Nizar Mahmoud Date: Wed, 1 Apr 2020 03:30:57 +0300 Subject: [PATCH] Refactor: Updated the Code to Handle Multiple Directories inside Each Other with Correct Output Forwarding The needed files to be minified are now found using *find* in bash. Output is *grepped* to avoid minified files. Then we get the output path, name, file extension of the output file, and run the npm script on the files. The new approach fixes the bug represented in issue #1. In addition, there is a commit referenced in that issue to a failed workflow for that commit This refactor fixes issue #1, makes the code cleaner, and more modular --- entrypoint.sh | 94 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 27 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 99b9b03..924c661 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,43 +1,83 @@ #!/bin/bash -l -cd /app/ +output_name () { + : ' + arguments: + 1- filepath (from find_files) -DIR="/github/workspace" + checks out_dir if empty or not + - if empty, returns : + dirname + filename + `.min` + file_extension + eg: /js/a/ + main + .min + .js + - if not empty, returns: + out_dir + sub_dir + filename + `.min` + file_extension + eg: /min_js/ + /a/ + main + .min + .js -INPUT_DIRECTORY="$DIR/$INPUT_DIRECTORY" -if [[ $INPUT_DIRECTORY =~ ^.*\/$ ]]; then - INPUT_DIRECTORY=${INPUT_DIRECTORY::-1} -fi + > note : sub_dir = dirname $1 - in_path + ' + f_name=$( basename $1 | grep -oP '^.*(?=\.)' ) + f_extn=$( basename $1 | grep -oP '\.[^\.]*$' ) -if [ -z $INPUT_OUTPUT ]; then - INPUT_OUTPUT=$INPUT_DIRECTORY -elif [[ $INPUT_OUTPUT =~ ^.*\/$ ]]; then - INPUT_OUTPUT=${INPUT_OUTPUT::-1} + f_dir=$( dirname $1 | xargs readlink -m ) + # assume that in_dir is `js/*` directly, so we want + # first dirname or the list of its files inside it + in_path=$( dirname $in_dir | head -1 | xargs readlink -m ) + # but if it is just a `js/`, we need its full path + # not just the dirname, but also the basename with it + if [ -d "${in_dir}" ]; then + in_path=$( readlink $in_dir ) + fi - INPUT_OUTPUT="$DIR/$INPUT_OUTPUT" -fi + f_path=$f_dir + if [ ! -z $out_dir ]; then + f_path="$out_dir/${f_dir#"$in_path"}" + fi -mkdir -p $INPUT_OUTPUT + echo "$f_path/$f_name.min$f_extn" | xargs readlink -m +} -for filename in `ls $INPUT_DIRECTORY`; do - filepath="$INPUT_DIRECTORY/$filename" +find_files () { + : ' + arguments: + 1- js | css (supported file extension) - extension="${filename#*.}" - filename="${filename%.*}" + find all files of certain type inside in_dir + - `-maxdepth` helps us specify only specified scope + - `find` returns the relative path, which is needed + - `*` acts as a recursive operator - outpath="$INPUT_OUTPUT/$filename.min" + Piped into grep to get all non minified files + ' + find $in_dir -maxdepth 1 -type f -name "*.$1" | grep -v ".min.$1$" +} - if [ "$extension" == "js" ]; then - outpath="$outpath.js" +cd /app/ - echo "Minify : JS : $filepath -> $outpath" +dir="/github/workspace" - npx uglifyjs $filepath --compress --mangle --output $outpath - elif [ "$extension" == "css" ]; then - outpath="$outpath.css" +in_dir="$dir/$INPUT_DIRECTORY" - echo "Minify : CSS : $filepath -> $outpath" +out_dir="" +if [ ! -z $INPUT_OUTPUT ]; then + out_dir="$dir/$INPUT_OUTPUT" +fi - npx cleancss -o $outpath $filepath - fi +# create output directories if they don't exist +mkdir -p $out_dir + +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 +done + +for file in $css_files; do + out=$( output_name $file ) + + echo "Minify : CSS : $file -> $out" + npx cleancss -o $out $file done \ No newline at end of file