Skip to content

Commit

Permalink
Refactor: Updated the Code to Handle Multiple Directories inside Each…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
nizarmah committed Apr 1, 2020
1 parent 1aa13e1 commit 970d180
Showing 1 changed file with 62 additions and 27 deletions.
89 changes: 62 additions & 27 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
#!/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
'
f_name=$( basename $1 | grep -oP '^.*(?=\.)' )
f_extn=$( basename $1 | grep -oP '\.[^\.]*$' )

INPUT_DIRECTORY="$DIR/$INPUT_DIRECTORY"
if [[ $INPUT_DIRECTORY =~ ^.*\/$ ]]; then
INPUT_DIRECTORY=${INPUT_DIRECTORY::-1}
fi
# (dirname $1) - (in_path) = sub_dir
f_dir=$( dirname $1 )
in_path=$( dirname $in_dir | head -1 )
if [ -d "${in_dir}" ]; then
in_path=$( readlink $in_dir )
fi

if [ -z $INPUT_OUTPUT ]; then
INPUT_OUTPUT=$INPUT_DIRECTORY
elif [[ $INPUT_OUTPUT =~ ^.*\/$ ]]; then
INPUT_OUTPUT=${INPUT_OUTPUT::-1}
f_path=$f_dir
if [ ! -z $out_dir ]; then
f_path="$out_dir/${f_dir#"$in_path"}"
fi

INPUT_OUTPUT="$DIR/$INPUT_OUTPUT"
fi
echo "$f_path/$f_name.min$f_extn" | readlink -m
}

find_files () {
: '
arguments:
1- js | css (supported file extension)
mkdir -p $INPUT_OUTPUT
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
for filename in `ls $INPUT_DIRECTORY`; do
filepath="$INPUT_DIRECTORY/$filename"
Piped into grep to get all non minified files
'
find $in_dir -maxdepth 1 -type f -name "*.$1" | grep -v ".min.$1$"
}

extension="${filename#*.}"
filename="${filename%.*}"
cd /app/

outpath="$INPUT_OUTPUT/$filename.min"
dir="/github/workspace"

if [ "$extension" == "js" ]; then
outpath="$outpath.js"
in_dir="$dir/$INPUT_DIRECTORY"

echo "Minify : JS : $filepath -> $outpath"
out_dir=""
if [ ! -z $INPUT_OUTPUT ]; then
out_dir="$dir/$INPUT_OUTPUT"
fi

npx uglifyjs $filepath --compress --mangle --output $outpath
elif [ "$extension" == "css" ]; then
outpath="$outpath.css"
# create output directories if they don't exist
mkdir -p $out_dir

echo "Minify : CSS : $filepath -> $outpath"
js_files=$( find_files 'js' )
css_files=$( find_files 'css' )

npx cleancss -o $outpath $filepath
fi
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

0 comments on commit 970d180

Please sign in to comment.