-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
1 changed file
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |