forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
50_node.sh
133 lines (118 loc) · 4.65 KB
/
50_node.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
export PATH
PATH=~/.nave/installed/default/bin:"$(path_remove ~/.nave/installed/*/bin)"
PATH="$PATH:$HOME/.config/yarn/global/node_modules/.bin"
# Set a specific version of node as the "default" for "nave use default"
function nave_default() {
local version
local default=${NAVE_DIR:-$HOME/.nave}/installed/default
[[ ! "$1" ]] && echo "Specify a node version or \"stable\"" && return 1
[[ "$1" == "stable" ]] && version=$(nave stable) || version=${1#v}
rm "$default" 2>/dev/null
ln -s $version "$default"
echo "Nave default set to $version"
}
# Install a version of node, set as default, install npm modules, etc.
function nave_install() {
local version
[[ ! "$1" ]] && echo "Specify a node version or \"stable\"" && return 1
[[ "$1" == "stable" ]] && version=$(nave stable) || version=${1#v}
if [[ ! -d "${NAVE_DIR:-$HOME/.nave}/installed/$version" ]]; then
e_header "Installing Node.js $version"
nave install $version
fi
[[ "$1" == "stable" ]] && nave_default stable && npm_install
}
# Global npm modules to install.
npm_globals=(
babel-cli
bower
grunt-cli
grunt-init
linken
pushstate-server
yo
)
# Update npm and install global modules.
function npm_install() {
local installed modules
e_header "Updating npm"
npm update -g npm
{ pushd "$(npm config get prefix)/lib/node_modules"; installed=(*); popd; } >/dev/null
modules=($(setdiff "${npm_globals[*]}" "${installed[*]}"))
if (( ${#modules[@]} > 0 )); then
e_header "Installing Npm modules: ${modules[*]}"
npm install -g "${modules[@]}"
fi
}
# Publish module to Npm registry, but don't update "latest" unless the version
# is an actual release version!
function npm_publish() {
local version="$(node -pe 'require("./package.json").version' 2>/dev/null)"
if [[ "${version#v}" =~ [a-z] ]]; then
local branch="$(git branch | perl -ne '/^\* (.*)/ && print $1')"
echo "Publishing dev version $version with --force --tag=$branch"
npm publish --force --tag="$branch" "$@"
else
echo "Publishing new latest version $version"
npm publish "$@"
fi
}
# Crazy-ass, cross-repo npm linking.
# Inter-link all projects, where each project exists in a subdirectory of
# the current parent directory. Uses https://github.com/cowboy/node-linken
alias npm_linkall='eachdir "rm -rf node_modules; npm install"; linken */ --src .'
alias npm_link='rm -rf node_modules; npm install; linken . --src ..'
# Link this project's grunt stuff to the in-development grunt stuff.
alias npm_link_grunt='linken . --src ~/gruntjs'
# Print npm owners in subdirectories.
alias npm_owner_list='eachdir "npm owner ls 2>/dev/null | sort"'
# Add npm owners to projects in subdirectories.
function npm_owner_add() {
local users=
local root="$(basename $(pwd))"
[[ $root == "gruntjs" ]] && users="cowboy tkellen"
if [[ -n "$users" ]]; then
eachdir "__npm_owner_add_each $users"
fi
}
function __npm_owner_add_each() {
local owners
owners="$(npm owner ls 2>/dev/null)"
[[ $? != 0 ]] && return
for user in $*; do
echo $owners | grep -v $user >/dev/null && npm owner add $user
done
}
# Look at a project's package.json and figure out what dependencies can be
# updated. While the "npm outdated" command only lists versions that are valid
# per the version string in package.json, this looks at the @latest tag in npm.
function npm_latest() {
if [[ -e 'node_modules' ]]; then
echo 'Backing up node_modules directory.'
mv "node_modules" "node_modules-$(date "+%Y_%m_%d-%H_%M_%S")"
fi
local deps='JSON.parse(require("fs").readFileSync("package.json")).dependencies'
# Install the latest version of all dependencies listed in package.json.
echo 'Installing @latest version of all dependencies...'
npm install $(node -pe "Object.keys($deps).map(function(m){return m+'@latest'}).join(' ')");
# List all dependencies that are now invalid, along with their (new) version.
local npm_ls="$(npm ls 2>/dev/null)"
if echo "$npm_ls" | grep invalid >/dev/null; then
echo -e '\nTHESE DEPENDENCIES CAN POSSIBLY BE UPDATED\n'
echo 'Module name: @latest: package.json:'
echo "$npm_ls" | perl -ne "m/.{10}(.+)@(.+?) invalid\$/ && printf \"%-30s %-20s %s\", \$1, \$2, \`node -pe '$deps[\"\$1\"]'\`"
return 99
else
echo -e '\nAll dependencies are @latest version.'
fi
}
# Force npm to rewrite package.json to sort everything in the default order
function npm-package() {
if [[ "$(cat package.json | grep dependencies)" ]]; then
npm install foo --save && npm uninstall foo --save
fi
if [[ "$(cat package.json | grep devDependencies)" ]]; then
npm install foo --save-dev && npm uninstall foo --save-dev
fi
}
alias npm-exec='PATH=$(npm bin):$PATH'