-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpm-stats.sh
119 lines (98 loc) · 2.45 KB
/
npm-stats.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
#!/bin/bash
NPM_STATS_ROOT="$HOME/.npm-stats"
NPM_STATS_VERSION="1.1.2"
npm() {
if [ "$1" == "i" ] || [[ $1 == ins* ]]; then
start=$(date -u +"%s")
command npm $@
end=$(date -u +"%s")
difference="$(($end-$start))"
mkdir -p $NPM_STATS_ROOT
date="$(date '+%Y-%m-%d %H:%M:%S')"
echo -e "$date\t$difference" >> "$NPM_STATS_ROOT/timing"
else
command npm $@
fi
}
npm-stats() {
if [ "$1" == "usage" ] || [ "$1" == "help" ]; then
_npm-stats-usage
return 0
fi
if [ "$1" == "version" ]; then
_npm-stats-version
return 0
fi
if [ ! -f "$NPM_STATS_ROOT/timing" ]; then
echo "No stats to report right now"
return 1
fi
if [ "$1" == "" ]; then
_npm-stats-normal
return 0
fi
if [ "$1" == "dates" ]; then
_npm-stats-dates
return 0
fi
if [ "$1" == "timing" ]; then
_npm-stats-timing
return 0
fi
if [ "$1" == "graph" ]; then
_npm-stats-graph
return 0
fi
if [ "$1" == "raw" ]; then
_npm-stats-raw
return 0
fi
}
_npm-stats-normal() {
installs=$(wc -l < "$NPM_STATS_ROOT/timing" | tr -d '[[:space:]]')
total=0
while read p; do
holder=$(echo "$p" | cut -f2 -d$'\t')
total=$(($total+$holder))
done < "$NPM_STATS_ROOT/timing"
echo "You've run npm install $installs times. For a total of $total seconds."
}
_npm-stats-dates() {
cut -f1 -d$'\t' "$NPM_STATS_ROOT/timing"
}
_npm-stats-timing() {
cut -f2 -d$'\t' "$NPM_STATS_ROOT/timing"
}
_npm-stats-raw() {
cat "$NPM_STATS_ROOT/timing"
}
_npm-stats-usage() {
echo
echo "npm-stats v$NPM_STATS_VERSION"
echo
echo "Usage:
npm-stats Show stats in human readable format
npm-stats dates Just show date data
npm-stats timing Just show timing data
npm-stats graph Show a graph of the stats
npm-stats raw Outputs raw data
npm-stats help Show this message
npm-stats version Show the current version installed
"
}
_npm-stats-version() {
echo "$NPM_STATS_VERSION"
}
_npm-stats-graph() {
cut -f2 -d$'\t' "$NPM_STATS_ROOT/timing" > "$NPM_STATS_ROOT/graph-data.dat"
gnuplot -e '
set terminal dumb;
set offset graph 0.01, graph 0.01, graph 0, graph 0;
set xlabel "timing";
set title "npm install timing distribution" offset 0,1;
binwidth=5;
set boxwidth binwidth;
bin(x,width)=width*floor(x/width) + binwidth/2.0;
plot "~/.npm-stats/graph-data.dat" using (bin($1,binwidth)):(1.0) smooth freq with boxes notitle
'
}