├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── npm-stats.sh ├── package.json └── test ├── package.json └── test.bats /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | *.sw* 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.12' 4 | - '4.0' 5 | - '5.0' 6 | 7 | script: cd test; bats --tap test.bats 8 | 9 | notifications: 10 | email: false 11 | 12 | before_script: 13 | - git clone https://github.com/sstephenson/bats.git /tmp/bats 14 | - mkdir -p /tmp/local 15 | - bash /tmp/bats/install.sh /tmp/local 16 | - export PATH=$PATH:/tmp/local/bin 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jackson Geller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # npm-stats [![Build Status](https://travis-ci.org/jaxgeller/npm-stats.svg?branch=master)](https://travis-ci.org/jaxgeller/npm-stats) 2 | Ever wonder how much of your life you spend on npm installs? npm-stats logs how much time you spend running npm commands. 3 | 4 | ### Installation 5 | 6 | ```sh 7 | npm install npmstats -g 8 | echo "source $HOME/.npm-stats/npm-stats.sh" >> some_*rc 9 | ``` 10 | 11 | or 12 | 13 | + Clone this repo to your home like, `$HOME/.npm-stats` 14 | + Source this script with your profile of choice. Make sure to add it at the end 15 | 16 | ```sh 17 | mkdir -p "$HOME/.npm-stats" 18 | git clone https://github.com/jaxgeller/npm-stats "$HOME/.npm-stats" 19 | echo "source $HOME/.npm-stats/npm-stats.sh" >> some_*rc 20 | ``` 21 | 22 | ### Usage 23 | 24 | npm-stats will hook into npm automatically, all you need to do is run `npm-stats` whenever you want some sweet stats. 25 | 26 | ```sh 27 | npm-stats v1.0.0 28 | 29 | Usage: 30 | npm-stats Show stats in human readable format 31 | npm-stats dates Just show date data 32 | npm-stats timing Just show timing data 33 | npm-stats graph Show a graph of the stats 34 | npm-stats raw Outputs raw data 35 | npm-stats help Show this message 36 | npm-stats version Show the current version installed 37 | ``` 38 | 39 | ### How it works 40 | 41 | npm-stats is a simple bash script. When you source it into your shell, it acts as a simple pre-filter to npm. If you run any command associated with installing, it appends to a log at `~/.npm-stats/timing` with the date and how long it took in seconds. 42 | 43 | npm-stats also provides some helper functions to help understand the logging, such as graphing and parsing the log. 44 | 45 | __Note: graphing requires gnuplot__ 46 | 47 | ### Updating 48 | 49 | If you used npm, just run `npm update npmstats -g`. 50 | Otherwise, updating is simple, just cd into `$HOME/.npm-stats`, run git pull, and refresh the shell. 51 | 52 | ### Examples 53 | 54 | ``` 55 | $ npm-stats 56 | 57 | You've run npm install 8 times. For a total of 28 seconds. 58 | ``` 59 | 60 | ```sh 61 | $ npm-stats graph 62 | 63 | npm install timing distribution 64 | 65 | 66 | 140 +-+-------+---------+---------+--------+---------+---------+-------+-+ 67 | + + + + + + + + 68 | 120 +-+ ****** *********** ****** ****** +-+ 69 | | * ****** * ***** * ****** * | 70 | | * * * * * ****** * * * | 71 | 100 +-+ * * * * * * * * * * +-+ 72 | | * * * * * * * * * * | 73 | 80 +-+ ****** * * * * * * * * * +-+ 74 | | * * * * * * * * * * * | 75 | 60 +-+ * * * * * * * * * * * +-+ 76 | | * * * * * * * * * * * | 77 | 40 +-+ * * * * * * * * * * * +-+ 78 | | * * * * * * * * * * * | 79 | | * * * * * * * * * * ****** | 80 | 20 +-+ * * * * * * * * * * * * +-+ 81 | + * * * * * * * * * * * * + 82 | 0 +-+-------*******************************************************--+-+ 83 | -10 0 10 20 30 40 50 60 84 | timing 85 | ``` 86 | 87 | ```sh 88 | $ npm-stats raw 89 | 90 | 2015-10-31 15:09:25 2 91 | 2015-10-31 15:10:26 1 92 | 2015-10-31 15:33:25 1 93 | 2015-10-31 15:33:27 2 94 | 2015-10-31 15:33:28 1 95 | 2015-10-31 15:34:31 11 96 | 2015-10-31 15:34:35 1 97 | 2015-10-31 15:35:13 9 98 | ``` 99 | -------------------------------------------------------------------------------- /npm-stats.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | NPM_STATS_ROOT="$HOME/.npm-stats" 4 | NPM_STATS_VERSION="1.1.2" 5 | 6 | npm() { 7 | if [ "$1" == "i" ] || [[ $1 == ins* ]]; then 8 | start=$(date -u +"%s") 9 | command npm $@ 10 | end=$(date -u +"%s") 11 | difference="$(($end-$start))" 12 | mkdir -p $NPM_STATS_ROOT 13 | date="$(date '+%Y-%m-%d %H:%M:%S')" 14 | echo -e "$date\t$difference" >> "$NPM_STATS_ROOT/timing" 15 | else 16 | command npm $@ 17 | fi 18 | } 19 | 20 | npm-stats() { 21 | 22 | if [ "$1" == "usage" ] || [ "$1" == "help" ]; then 23 | _npm-stats-usage 24 | return 0 25 | fi 26 | 27 | if [ "$1" == "version" ]; then 28 | _npm-stats-version 29 | return 0 30 | fi 31 | 32 | if [ ! -f "$NPM_STATS_ROOT/timing" ]; then 33 | echo "No stats to report right now" 34 | return 1 35 | fi 36 | 37 | if [ "$1" == "" ]; then 38 | _npm-stats-normal 39 | return 0 40 | fi 41 | 42 | if [ "$1" == "dates" ]; then 43 | _npm-stats-dates 44 | return 0 45 | fi 46 | 47 | if [ "$1" == "timing" ]; then 48 | _npm-stats-timing 49 | return 0 50 | fi 51 | 52 | if [ "$1" == "graph" ]; then 53 | _npm-stats-graph 54 | return 0 55 | fi 56 | 57 | if [ "$1" == "raw" ]; then 58 | _npm-stats-raw 59 | return 0 60 | fi 61 | } 62 | 63 | _npm-stats-normal() { 64 | installs=$(wc -l < "$NPM_STATS_ROOT/timing" | tr -d '[[:space:]]') 65 | total=0 66 | 67 | while read p; do 68 | holder=$(echo "$p" | cut -f2 -d$'\t') 69 | total=$(($total+$holder)) 70 | done < "$NPM_STATS_ROOT/timing" 71 | 72 | echo "You've run npm install $installs times. For a total of $total seconds." 73 | } 74 | 75 | _npm-stats-dates() { 76 | cut -f1 -d$'\t' "$NPM_STATS_ROOT/timing" 77 | } 78 | 79 | _npm-stats-timing() { 80 | cut -f2 -d$'\t' "$NPM_STATS_ROOT/timing" 81 | } 82 | 83 | _npm-stats-raw() { 84 | cat "$NPM_STATS_ROOT/timing" 85 | } 86 | 87 | _npm-stats-usage() { 88 | echo 89 | echo "npm-stats v$NPM_STATS_VERSION" 90 | echo 91 | echo "Usage: 92 | npm-stats Show stats in human readable format 93 | npm-stats dates Just show date data 94 | npm-stats timing Just show timing data 95 | npm-stats graph Show a graph of the stats 96 | npm-stats raw Outputs raw data 97 | npm-stats help Show this message 98 | npm-stats version Show the current version installed 99 | " 100 | } 101 | 102 | _npm-stats-version() { 103 | echo "$NPM_STATS_VERSION" 104 | } 105 | 106 | _npm-stats-graph() { 107 | cut -f2 -d$'\t' "$NPM_STATS_ROOT/timing" > "$NPM_STATS_ROOT/graph-data.dat" 108 | 109 | gnuplot -e ' 110 | set terminal dumb; 111 | set offset graph 0.01, graph 0.01, graph 0, graph 0; 112 | set xlabel "timing"; 113 | set title "npm install timing distribution" offset 0,1; 114 | binwidth=5; 115 | set boxwidth binwidth; 116 | bin(x,width)=width*floor(x/width) + binwidth/2.0; 117 | plot "~/.npm-stats/graph-data.dat" using (bin($1,binwidth)):(1.0) smooth freq with boxes notitle 118 | ' 119 | } 120 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npmstats", 3 | "version": "1.2.0", 4 | "description": "stats for npm", 5 | "main": "index.js", 6 | "preferGlobal": true, 7 | "directories": { 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "postinstall": "mkdir -p $HOME/.npm-stats; cp npm-stats.sh $HOME/.npm-stats; echo -e 'ADD THIS TO YOUR SHELL `source ~/.npm-stats/npm-stats.sh`\n';", 12 | "uninstall": "rm -r $HOME/.npm-stats" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/jaxgeller/npm-stats.git" 17 | }, 18 | "keywords": [ 19 | "npm", 20 | "stats", 21 | "stats" 22 | ], 23 | "author": "Jackson Geller ", 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/jaxgeller/npm-stats/issues" 27 | }, 28 | "homepage": "https://github.com/jaxgeller/npm-stats#readme" 29 | } 30 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-stats", 3 | "private": "true", 4 | "version": "1.0.0", 5 | "description": "npm-stats testing", 6 | "author": "Jackson Geller ", 7 | "license": "MIT", 8 | "dependencies": { 9 | "express": "^4.13.3", 10 | "mongoose": "^4.2.3" 11 | }, 12 | "devDependencies": { 13 | "browserify": "^12.0.1", 14 | "gulp": "^3.9.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/test.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | source ../npm-stats.sh 4 | rm -rf ~/.npm-stats 5 | rm -rf node_modules 6 | 7 | @test "No stats, no response" { 8 | run npm-stats 9 | 10 | [ "$status" -eq 1 ] 11 | [ "$output" = "No stats to report right now" ] 12 | } 13 | 14 | @test "Outputs usage" { 15 | run npm-stats usage 16 | 17 | [ "$status" -eq 0 ] 18 | } 19 | 20 | @test "Outputs version" { 21 | run npm-stats version 22 | 23 | [ "$status" -eq 0 ] 24 | } 25 | 26 | # @test "test install singular" { 27 | # npm install lodash 28 | 29 | # results=$(npm-stats | awk '{print $5}') 30 | # [ "$results" = "1" ] 31 | # } 32 | 33 | # @test "test install singular with abreviation 1" { 34 | # npm i underscore 35 | 36 | # results=$(npm-stats | awk '{print $5}') 37 | # [ "$results" -eq "1" ] 38 | # } 39 | 40 | # @test "test install singular with abreviation 2" { 41 | # npm ins async 42 | 43 | # results=$(npm-stats | awk '{print $5}') 44 | # [ "$results" -eq "1" ] 45 | # } 46 | 47 | # @test "test install multiple" { 48 | # npm install request chalk 49 | 50 | # results=$(npm-stats | awk '{print $5}') 51 | # [ "$results" -eq "1" ] 52 | # } 53 | 54 | # @test "test install from package.json" { 55 | # npm install 56 | 57 | # results=$(npm-stats | awk '{print $5}') 58 | # [ "$results" -eq "1" ] 59 | # } 60 | --------------------------------------------------------------------------------