forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path50_node.sh
More file actions
executable file
·149 lines (130 loc) · 4.43 KB
/
50_node.sh
File metadata and controls
executable file
·149 lines (130 loc) · 4.43 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
[[ "$1" != init && ! -e ~/.volta ]] && return 1
export PATH="~/.nave/installed/default/bin:$PATH"
#PATH=~/.nave/installed/default/bin:"$(path_remove ~/.nave/installed/*/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
echo "$version"
echo "$default"
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
}
# Use the version of node in the local .nvmrc file
alias nvmrc='exec nave use $(<.nvmrc)'
# Global npm modules to install.
npm_globals=(
babel-cli
bower
less
sass
gulp-cli
ember-cli
eslint
json-lint
json2yaml
grunt-cli
gulp-cli
pushstate-server
phonegap@3.6.0-0.21.19
cordova@5.0.0
ripple-emulator@0.9.24
tns-android
yo
webpack
yaml2json
yarn
)
# Because "rm -rf node_modules && npm install" takes WAY too long. Not sure
# if this really works as well, though. We'll see.
alias npm_up='npm prune && npm install && npm update'
# Run arbitrary command with npm "bin" directory in PATH.
function npm_run() {
git rev-parse 2>/dev/null && (
PATH="$(git rev-parse --show-toplevel)/node_modules/.bin:$PATH"
"$@"
)
}
# Update npm and install global modules.
function npm_install() {
local installed modules
e_header "Updating npm"
sudo 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[*]}"
# if is_windows; then
# npm install -g "${modules[@]}"
# else
sudo npm install -g "${modules[@]}"
# fi
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
}
export VOLTA_HOME=~/.volta
grep --silent "$VOLTA_HOME/bin" <<< $PATH || export PATH="$VOLTA_HOME/bin:$PATH"
# Use npx instead of installing global npm modules
function make_npx_alias () {
alias $1="npx $@"
}
make_npx_alias json2yaml
make_npx_alias pushstate-server
make_npx_alias yaml2json
function get_last_modified_js_file_recursive() {
find . -type d \( -name node_modules -o -name .git -o -name .build \) -prune -o -type f \( -name '*.js' -o -name '*.jsx' \) -print0 \
| xargs -0 stat -f '%m %N' \
| sort -rn \
| head -1 \
| cut -d' ' -f2-
}
function watchfile() {
yarn watch --testPathPattern "$(get_last_modified_js_file_recursive | sed -E 's#.*/([^/]+)/([^.]+).*#\1/\2.#')"
}
function watchdir() {
yarn watch --testPathPattern "$(dirname "$(get_last_modified_js_file_recursive)")"
}