Skip to content

Commit 5515b87

Browse files
Add support for options on default packages (#419)
* Add support for options on default packages * Add docs on default-npm-packages syntax
1 parent 1e6065c commit 5515b87

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ export ASDF_NODEJS_LEGACY_FILE_DYNAMIC_STRATEGY=latest_available
101101
lodash
102102
request
103103
express
104+
105+
# .default-npm-packages can have comments, and option flags:
106+
zx --registry=https://registry.yarnpkg.com/
104107
```
105108

106109
You can specify a non-default location of this file by setting a `ASDF_NPM_DEFAULT_PACKAGES_FILE` variable.

bin/install

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,49 @@ _run_for_installation() {
3333

3434

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

38-
if ! [ -f "$default_npm_packages_file" ]; then
38+
if ! [ -f "$pkgs_file" ]; then
3939
return 0
4040
fi
4141

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

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

49-
_run_for_installation xargs npm install -g <<< "$filtered_packages"
49+
local args=()
50+
51+
local line="" aux=()
52+
while read -r line; do
53+
pkg_lines+=("$line")
54+
55+
case "$line" in
56+
*' -'*|-*)
57+
# Installing previously read packages
58+
if [ "${#args[@]}" -gt 0 ]; then
59+
npm_install "${args[@]}"
60+
fi
61+
62+
# Read current line as a command by itself
63+
args=()
64+
IFS=' ' read -ra args <<< "$line"
65+
66+
npm_install "${args[@]}"
67+
args=()
68+
;;
69+
*)
70+
# Accumulate packages to install
71+
IFS=' ' read -ra aux <<< "$line"
72+
args+=("${aux[@]}")
73+
;;
74+
esac
75+
done < <(grep -v '^\s*#' "$pkgs_file")
76+
77+
if [ "${#args[@]}" -gt 0 ]; then
78+
npm_install "${args[@]}"
5079
fi
5180
}
5281

0 commit comments

Comments
 (0)