From ec1aca10bac90c3afc434a5c3f793db733db60c1 Mon Sep 17 00:00:00 2001 From: Akash Manohar J Date: Tue, 22 Dec 2015 22:35:56 +0530 Subject: [PATCH] Fix bin names #6 --- bin/get-bin-names.py | 21 +++++++++++++++++++++ bin/postinstall | 36 +++++------------------------------- 2 files changed, 26 insertions(+), 31 deletions(-) create mode 100644 bin/get-bin-names.py diff --git a/bin/get-bin-names.py b/bin/get-bin-names.py new file mode 100644 index 0000000..1a0eacb --- /dev/null +++ b/bin/get-bin-names.py @@ -0,0 +1,21 @@ +#/usr/bin/env python +import os, json; + +if os.environ['npm_config_global'] == 'true': + exit(0) + +package_json_path=os.environ['PWD'] + '/package.json' + +# create shims only for globally installed pkg +if package_json_path.count('node_modules') > 1: + exit(0) + +with open(package_json_path, 'r') as package_json_file: + package_json = json.load(package_json_file) + +if 'bin' in package_json: + if isinstance(package_json['bin'], dict): + bin_list = " ".join(package_json['bin'].keys()) + print(bin_list) + else: + print(package_json["name"]) diff --git a/bin/postinstall b/bin/postinstall index d7a7025..f8c9694 100755 --- a/bin/postinstall +++ b/bin/postinstall @@ -1,35 +1,9 @@ #!/usr/bin/env bash -env | grep -i npm_package_bin | while read bin_env; do - - if [[ "$bin_env" =~ npm_package_bin* ]]; then - - IFS='=' read -a bin_env_parts <<< "$bin_env" - executable_name="$(basename ${bin_env_parts[1]} .js)" - - # if "node_modules" appears more than once in $PWD - # we can confirm that package being installed is sub-dependency - node_modules_count=$(echo $PWD | grep -o node_modules | wc -l) - - # create shims only for globally installed pkg - if [ $node_modules_count -le 1 ] - then - is_npm_sub_dependency=false; - else - is_npm_sub_dependency=true; - fi - - - # if global install - # and executable name is not empty - # and is not a dependency of another package that is being installed as global - if [ "$npm_config_global" = "true" ] && \ - [ "$executable_name" != "" ] && \ - [ "$is_npm_sub_dependency" = "false" ] - then - asdf shim nodejs .npm/bin/$executable_name - fi - - fi +package_bin_names=$(python $(dirname $0)/get-bin-names.py) +IFS=' ' read -a package_bin_names_list <<< "$package_bin_names" +for package_bin in "${package_bin_names_list[@]}" +do + asdf shim nodejs .npm/bin/${package_bin} done