-
Notifications
You must be signed in to change notification settings - Fork 18
/
entrypoint.sh
186 lines (146 loc) · 3.63 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash -l
output_name () {
: '
arguments:
1- filepath (from find_files)
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
> note : sub_dir = dirname $1 - in_path
'
f_name=$( basename $1 | grep -oP '^.*(?=\.)' )
f_extn=$( basename $1 | grep -oP '\.[^\.]*$' )
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=$f_dir
fi
f_path=$f_dir
if [ ! -z $out_dir ]; then
f_path="$out_dir/${f_dir#"$in_path"}"
mkdir -p $f_path
fi
min_extn=".min"
if $overwrite; then
min_extn=""
fi
echo "$f_path/$f_name$min_extn$f_extn" | xargs readlink -m
}
find_files () {
: '
arguments:
1- js | css (supported file extension)
find all files of certain type inside in_dir
- `find` returns the relative path, which is needed
- `*` acts as a recursive operator
Piped into grep to get all non minified files
optional parameters:
- `-maxdepth`
Clear all optional parameter keys in case the values are empty
So, when appended to the command parameters, no error is caused
'
MAXDEPTH_KEY="-maxdepth"
MAXDEPTH_VAL=$INPUT_MAXDEPTH
if [ -z $MAXDEPTH_VAL ]; then
MAXDEPTH_KEY=""
fi
find $in_dir ${MAXDEPTH_KEY} ${MAXDEPTH_VAL} -type f -name "*.$1" -not \( -iname "*\.min.$1" \)
}
exec_minify_js () {
: '
arguments:
1- input file
2- output file
returns the command needed to minify the js file
based on the requested JavaScript Engine in the
input `js_engine`
'
file=$1
out=$2
js_engine=$INPUT_JS_ENGINE
if [[ $js_engine == "babel" ]]; then
npx minify $file --out-file $out
elif [[ $js_engine == "uglify-js" ]]; then
npx uglifyjs $file --compress --mangle --output $out
fi
}
exec_minify_css () {
: '
arguments:
1- input file
2- output file
returns the command needed to minify the css file
based on the requested CSS Engine in the
input `css_engine`
'
file=$1
out=$2
css_engine=$INPUT_CSS_ENGINE
if [[ $css_engine == "clean-css" ]]; then
npx cleancss -o $out $file
elif [[ $css_engine == "lightning" ]]; then
npx lightningcss --minify $file --output-file $out
fi
}
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
exec_minify_js $file $out
elif [[ $file == *.css ]]; then
exec_minify_css $file $out
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"
in_dir="$dir/$INPUT_DIRECTORY"
out_dir=""
if [ ! -z $INPUT_OUTPUT ]; then
out_dir="$dir/$INPUT_OUTPUT"
fi
if [ ! -z $out_dir ]; then
# create output directories if they don't exist
mkdir -p $out_dir
fi
overwrite=false
if [ "${INPUT_OVERWRITE,,}" == "true" ]; then
overwrite=true
fi
file_set=$({
find_files 'js' &
find_files 'css' &
})
set -e
for file in $file_set; do
minify_file $file
done