Skip to content

Commit

Permalink
Merge pull request #2 from nizarmah/feature_multiple_dirs
Browse files Browse the repository at this point in the history
Refactor: Updated the Code to Handle Multiple Directories inside Each…
  • Loading branch information
nizarmah authored Apr 1, 2020
2 parents 1aa13e1 + 0bebcdb commit 2a41de2
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2a41de2

Please sign in to comment.