Last active
June 21, 2016 11:36
-
-
Save eliranmal/3d5f3aa5528a1e8d306beb0f855adb05 to your computer and use it in GitHub Desktop.
functions for common npm tasks
This file contains 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 | |
################################## | |
# functions for common npm tasks # | |
################################## | |
##### public ##### | |
# | |
# removes 'node_modules', clears the cache and installs everything | |
# | |
# output: | |
# whatever npm outputs | |
# | |
npm_restart() { | |
rm -rf node_modules && npm cache clear && npm i | |
} | |
alias npm_reboot=npm_restart | |
alias npm_again=npm_restart | |
# | |
# uninstalls and installs a package | |
# | |
# input: | |
# $1 - the package name | |
# | |
# output: | |
# whatever npm outputs | |
# | |
npm_package_reinstall() { | |
__defensive_proxy __npm_package_reinstall $1 | |
} | |
alias npm_pack_rein=npm_package_reinstall | |
alias npmpr=npm_package_reinstall | |
# | |
# pass a package name and get its locally installed version | |
# | |
# input: | |
# $1 - the package name | |
# | |
# output: | |
# the package version string | |
# | |
npm_package_version() { | |
__defensive_proxy __npm_package_version $1 | |
} | |
alias npm_pack_ver=npm_package_version | |
alias npmpv=npm_package_version | |
# | |
# installs a package version that is stable. | |
# this implementation uses the version previous to the latest published version. | |
# | |
# input: | |
# $1 - the package name | |
# | |
# output: | |
# whatever npm outputs | |
# | |
npm_package_stabilize() { | |
__defensive_proxy __npm_package_stabilize $1 | |
} | |
alias npm_pack_sta=npm_package_stabilize | |
alias npmps=npm_package_stabilize | |
# | |
# installs a package version that is previous to the currently installed local version | |
# | |
# input: | |
# $1 - the package name | |
# | |
# output: | |
# whatever npm outputs | |
# | |
npm_package_downgrade() { | |
__defensive_proxy __npm_package_downgrade $1 | |
} | |
alias npm_package_revert=npm_package_downgrade | |
alias npm_pack_down=npm_package_downgrade | |
alias npmpd=npm_package_downgrade | |
##### private ##### | |
# | |
# a predicate that checks if the passed argument is of type function | |
# | |
# input: | |
# $1 - the variable to test | |
# | |
# output: | |
# true if the passed argument is a function, false otherwise | |
# | |
__is_function() { | |
[[ "`type -t $1`" = "function" ]] | |
} | |
# | |
# a predicate that checks if the passed argument is not empty | |
# | |
# input: | |
# $1 - the variable to test | |
# | |
# output: | |
# true if the passed argument is not empty, false otherwise | |
# | |
__is_not_empty() { | |
[[ "$1" != "" ]] | |
} | |
# | |
# checks for the presence of a proxied function's single argument and delegates the invocation | |
# | |
# input: | |
# $1 - the proxied function | |
# $2 - the proxied function's sole argument | |
# | |
# output: | |
# the invocation result, if the argument is valid, or an empty string otherwise | |
# | |
__defensive_proxy() { | |
if $(__is_function $1) && $(__is_not_empty $2); then | |
local result=$($@) | |
fi | |
echo $result | |
} | |
# | |
# returns an aggregation of all passed parameters as a single space-delimited-string, | |
# clearing array-like delimiters. | |
# use with command substitution to get the returned value, e.g. $(__as_space_delimited_string [ a, b ]). | |
# | |
# input: | |
# $@ - a virtual argument in the form of a json array, representing all passed arguments | |
# (to allow empty spaces in the parameter), | |
# | |
# output: | |
# a bash array containing the json array data | |
# | |
__as_space_delimited_string() { | |
# lose these characters: ,'[] | |
echo $@ | sed "s/[,']//g" | sed "s/\[//g" | sed "s/\]//g" | |
} | |
# | |
# gets the index of a value in an array | |
# | |
# input: | |
# $1 - the value to search for | |
# $2 - the array containing the value | |
# | |
# output: | |
# the index of the passed value in the passed array | |
# | |
__index_of() { | |
local value="$1" | |
declare -a array=("${!2}") | |
local count=0; | |
for item in "${array[@]}"; do | |
[[ $item == "$value" ]] && echo $count && break | |
((++count)) | |
done | |
} | |
# | |
# returns the package's publicly available versions as a space-delimited string | |
# | |
# input: | |
# $1 - the package name | |
# | |
# output: | |
# the package's public versions as a string | |
# | |
__npm_package_versions_as_string() { | |
local available_versions=$(npm show $1 versions) | |
echo $(__as_space_delimited_string $available_versions) | |
} | |
# see: npm_package_stabilize | |
__npm_package_stabilize() { | |
local versions_string=$(__npm_package_versions_as_string $1) | |
local versions_array=(${versions_string}) | |
local prev_public_version=${versions_array[-2]} | |
npm i $1@$prev_public_version | |
} | |
# see: npm_package_version | |
__npm_package_version() { | |
echo $(cut -d "@" -f 2 <<< `npm ls $1 | grep $1`) | |
} | |
# see: npm_package_reinstall | |
__npm_package_reinstall() { | |
npm un $1 && npm i $1 | |
} | |
# see: npm_package_downgrade | |
__npm_package_downgrade() { | |
local current_version=$(__npm_package_version $1) | |
local versions_string=$(__npm_package_versions_as_string $1) | |
local versions_array=(${versions_string}) | |
local current_version_index=$(__index_of $current_version versions_array[@]) | |
if $(__is_not_empty $current_version_index); then | |
local prev_version_index=$(($current_version_index - 1)) | |
fi | |
if $(__is_not_empty $prev_version_index); then | |
local prev_version=${versions_array[$prev_version_index]} | |
npm i $1@$prev_version | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source this script from the shell / your script, copy & paste it to your
.bashrc
, do whatever your feel like :)