Created
April 1, 2016 02:12
-
-
Save harto/bcd9f972288808d4d2f5e8ca185eb255 to your computer and use it in GitHub Desktop.
List local JS module dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
absolutize() { | |
local path=$1 | |
if [[ -d $path ]]; then | |
(cd $path; pwd -P) | |
else | |
echo $(cd $(dirname $path); pwd -P)/$(basename $path) | |
fi | |
} | |
relativize() { | |
local base=$1 | |
local path=$2 | |
if [[ ! -d $base ]]; then | |
base=$(dirname $base) | |
fi | |
local base_abs=$(cd $base; pwd -P)/ | |
local path_abs=$(cd $(dirname $path); pwd -P)/$(basename $path) | |
echo ${path_abs##$base_abs} | |
} | |
root=$(absolutize ${1:-$(dirname $0)}) | |
find $root -name node_modules -prune -o -name test -prune -o -name migrations -prune -o -type f -name \*.js | while read module_path; do | |
sed -En "s/.+ require\('(\.[^']+).*/\1/p" $module_path | while read require_arg; do | |
require_path=$(absolutize "$(dirname $module_path)/${require_arg}.js") | |
echo $(relativize $root $module_path) $(relativize $root $require_path) | |
done | uniq | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment