Skip to content

Add support for options on default packages #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export ASDF_NODEJS_LEGACY_FILE_DYNAMIC_STRATEGY=latest_available
lodash
request
express

# .default-npm-packages can have comments, and option flags:
zx --registry=https://registry.yarnpkg.com/
```

You can specify a non-default location of this file by setting a `ASDF_NPM_DEFAULT_PACKAGES_FILE` variable.
Expand Down
45 changes: 37 additions & 8 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,49 @@ _run_for_installation() {


install_default_npm_packages() {
local default_npm_packages_file="${ASDF_NPM_DEFAULT_PACKAGES_FILE:=$HOME/.default-npm-packages}" filtered_packages=
local pkgs_file="${ASDF_NPM_DEFAULT_PACKAGES_FILE:=$HOME/.default-npm-packages}"

if ! [ -f "$default_npm_packages_file" ]; then
if ! [ -f "$pkgs_file" ]; then
return 0
fi

filtered_packages=$(grep -vE "^\s*#" < "$default_npm_packages_file")
npm_install() {
printf "$(colored $CYAN "\nRunning the folowing install command:\n")"
printf "$(colored $GREEN " \$ npm install -g %s\n")" "$*"

if [ "${filtered_packages-}" ]; then
printf "$(colored $CYAN "Installing the following default packages globally: ")"
xargs printf "%s, " <<< "$filtered_packages"
printf "\x8\x8 \n" # Cleanup last comma
_run_for_installation npm install -g "$@"
}

_run_for_installation xargs npm install -g <<< "$filtered_packages"
local args=()

local line="" aux=()
while read -r line; do
pkg_lines+=("$line")

case "$line" in
*' -'*|-*)
# Installing previously read packages
if [ "${#args[@]}" -gt 0 ]; then
npm_install "${args[@]}"
fi

# Read current line as a command by itself
args=()
IFS=' ' read -ra args <<< "$line"

npm_install "${args[@]}"
args=()
;;
*)
# Accumulate packages to install
IFS=' ' read -ra aux <<< "$line"
args+=("${aux[@]}")
;;
esac
done < <(grep -v '^\s*#' "$pkgs_file")

if [ "${#args[@]}" -gt 0 ]; then
npm_install "${args[@]}"
fi
}

Expand Down