-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwipe-modules.sh
executable file
·173 lines (146 loc) · 3.93 KB
/
wipe-modules.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
#!/usr/bin/env bash
# variables
modules_removed=0
is_dir=0
is_number=0
run=0
initial_size=0
end_size=0
# usage info
usage() {
cat <<EOF
Usage: wipe-modules [options] [path] [days]
Options:
-d Only show node_modules to be removed
-l <#> Specify a depth level
Path:
The full path of your code directory
Days:
The days you want to set to mark projects as inactive
Example: wipe-modules ~/code 30
That will remove the node_modules of your ~/code projects
whose been inactive for 30 days or more.
Keep in mind the order of the parameters you pass, first
goes the options (optional) and then the path and days (required)
EOF
exit 0
}
# getops parameters
while getopts dhl: flag; do
case "$flag" in
d) dry=1;;
h) usage;;
l) level=$OPTARG;;
?) usage;;
esac
done
# shift parameters!
shift $((OPTIND - 1))
# positional parameters
code_dir=$1
last_modified=$2
# default depth level to 1 if not provided
if [ -z "$level" ]; then
level=1
fi
# wipe node_modules
wipe() {
dir="$1" # now dir is the 1st parameter ($1)
dir="${dir%/}" # strip trailing slash
dir="${dir##*/}" # strip path and leading slash
if [[ "$dir" = .* ]]; then
return
fi
cd "$dir" || exit
if [ "$(find . -maxdepth "$level" -type d -name 'node_modules')" ]; then
# if $dry exists then just print the name of the folder that
# matches the search
if [ -n "$dry" ]
then
# print the current directory name
echo "\033[90m $dir \033[39m"
else
# wipe the node_modules folder!
rm -rf node_modules
fi
# counter for modules to be removed
modules_removed=$((modules_removed + 1))
fi
cd ..
}
# exit message
display_message() {
if [ $modules_removed -gt 0 ]; then
if [ -n "$dry" ]; then
echo ""
echo "\033[90m $modules_removed node_modules were found! \033[39m"
else
echo "\033[90m $modules_removed node_modules successfully removed! \033[39m"
echo ""
echo "\033[90m Your initial directory size was $initial_size \033[39m"
echo "\033[90m Now it is $end_size! \033[39m"
fi
else
echo "\033[90m Our agent couldn't find any inactive projects. \033[39m"
fi
}
# instructs agent gir to start ripping off those pesky node_modules
go_gir() {
# move to code directory
cd "$code_dir" || exit
# grab the initial directory size
initial_size=$(du -hs . | awk -F'\t' '{print $1;}')
# if $dry is --dry (or -D) then show message
if [ -n "$dry" ]; then
echo "\033[90m The node_modules in the following directories can be wiped out: \033[39m"
sleep 2
echo ""
fi
# find $code_dir directories whose last modify time (mtime) is older than
# $last_modified days, loop through each resulting dir and execute wipe function
# then display message
find . -maxdepth 1 -type d -mtime +"$last_modified" |
{
while read -r d; do
wipe "$d"
done
# grab the new directory size!
end_size=$(du -hs . | awk -F'\t' '{print $1;}')
# show info
display_message
}
}
# check if $1 parameter is a valid directory
if [ -d "$code_dir" ]; then
is_dir=1
fi
# check if $2 parameter is a valid number (regex)
last_modified=$(echo "$last_modified" | grep -E '^[0-9]+$')
if [ -n "$last_modified" ]; then
is_number=1
fi
# if valid parameters are being passed then prepare for action
if [ $is_dir -eq 1 ] && [ $is_number -eq 1 ]; then
run=1
fi
# if everything ok... press the green button!
if [ $run -eq 1 ]; then
echo ""
echo "\033[90m Our agent is examining your files... Hold on a sec. \033[39m"
sleep 2
echo ""
go_gir
exit 0
else
# something went wrong... abort the mission
# if $is_dir is invalid, show error message and exit
if [ $is_dir -eq 0 ]; then
echo ""
echo "Error: Invalid directory, please provide a valid one." >&2; exit 1
fi
# if $is_number is invalid, show error message and exit
if [ $is_number -eq 0 ]; then
echo ""
echo "Error: Please provide a valid number." >&2; exit 1
fi
fi