Skip to content

Instantly share code, notes, and snippets.

@lleaff
Last active December 1, 2017 15:25
Show Gist options
  • Save lleaff/6e7648ebbe1b06c647b7656264f2ee93 to your computer and use it in GitHub Desktop.
Save lleaff/6e7648ebbe1b06c647b7656264f2ee93 to your computer and use it in GitHub Desktop.
Generate JavaScript dependency graph with Madge and open in default SVG viewer
#!/bin/bash
# Generate JavaScript dependency graph with Madge and open in default SVG viewer
function random_filename() {
local length=${1:-10}
head -c $(( $length * 100 )) /dev/urandom | \
tr -dc 'a-zA-Z0-9-_' | \
head -c $length
}
function _cleanup-js-dependency-graph() {
if [[ -z "$1" ]]; then
return;
fi
local filename="$1"
sleep 50 && rm "$filename" 2>&1
}
# https://github.com/pahen/madge
function js-dependency-graph() {
if ! hash xdg-open 2>/dev/null 1>&2; then
echo 1>&2 'madge and graphviz need to be installed. Aborting.'
return
fi
local filename="/tmp/js_depth_graph-$(random_filename 10).svg"
madge "$@" --image "$filename"
if hash xdg-open 2>/dev/null 1>&2; then
xdg-open "$filename";
else
open "$filename";
fi
_cleanup-js-depth-graph "$filename" &
}
js-dependency-graph "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment