-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
132 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bash | ||
# : nx 0.0.8 :: | ||
# shellcheck disable=SC2139 # shellcheck.net/wiki/SC2139 # allow parameter expansion within alias strings # | ||
function nx() ( local pkg="" cmd="" npmcmds="" | ||
npmcmds="$( npm -h | awk '/access/,/whoami/' | sed -E 's/ (help|start|test),//g' | xargs | sed 's/, /|/g' )" | ||
# :: print current aliases and exit when "n" is run with no arguments :: | ||
[[ -z "$1" ]] && alias | grep "='nx " | sed 's/^alias //' | sed 's/=/ · /' | sed "s/nx /npm run /" | sed -E "s/run (${npmcmds})/\1/" | tr -d "'" && exit 0 | ||
[[ "${npmcmds}" == *"|$1|"* || "${npmcmds}" == "$1|"* || "${npmcmds}" == *"|$1" ]] && cmd=('npm' "$1") || cmd=('npm' 'run' "$1" "${2:+--}") | ||
[[ "$2" == 'w' ]] && set -- "$1" "--watch" "${@:3}" # :: rewrite 'w' to '--watch' if provided as second arg "" | ||
# :: find closest package.json (file path containing fewest "/" characters) :: export NX_FIND=0; source "$HOME/nx/nx.sh" (to disable) :: | ||
while [[ ! "${NX_FIND}" =~ ^(0|false|FALSE)$ && ! -f "./package.json" ]] && [[ "$2" != '--global' ]]; do | ||
[[ "$( pwd )" == "$HOME" ]] && echo "No package.json found." && exit 1 | ||
pkg="$( find . -name package.json -maxdepth 3 | grep -v node_modules | awk -F/ '{print NF,$0}' | sort -n | cut -d' ' -f2- | head -1 )" | ||
cd "$( [[ -n "${pkg}" ]] && dirname "${pkg}" || echo '..' )" || exit 1 | ||
done | ||
# :: call "nvm use" automatically before running commands :: export NX_NVM=1; source "$HOME/nx/nx.sh" (to enable) :: | ||
[[ "${NX_NVM}" =~ ^(1|true|TRUE)$ ]] && nvm use &>/dev/null | ||
# :: await confirmation of current node+npm versions before executing command :: export NX_CONFIRM=1; source "$HOME/nx/nx.sh" (to enable) :: | ||
if [[ "${NX_CONFIRM}" =~ ^(1|true|TRUE)$ ]]; then | ||
read -rsn1 -p "${cmd[*]} ${*:2} "$'\n'"Press any key to run · CTRL+C to cancel · node $( node -v ) · npm $( npm -v )"$'\n\n' | ||
fi | ||
"${cmd[@]}" "${@:2}" # :: execute constructed npm command :: | ||
) | ||
# :: default command alias :: n -> nx :: eg. n install -> npm install, n build -> npm run build, etc. | ||
# :: export NX_COMMAND=0; source "$HOME/nx/nx.sh" (to disable) :: | ||
if ! [[ "${NX_COMMAND}" =~ ^(0|false|FALSE)$ ]]; then | ||
alias "${NX_COMMAND:-n}"="nx" | ||
fi | ||
# :: default sub-command aliases :: | ||
# nb -> npm run build nt -> npm test nf -> npm run format nl -> npm run lint nh -> npm run help | ||
# ns -> npm start nk -> npm link ni -> npm install nu -> npm uninstall nis, nid, nus, nud -> npm [un]install --save[-dev] | ||
# :: export NX_ALIASES=0; source "$HOME/nx/nx.sh" (to disable) :: | ||
if ! [[ "${NX_ALIASES}" =~ ^(0|false|FALSE)$ ]]; then | ||
for word in $( tr -cs '[:alnum:]._-/' ' ' <<< "${NX_ALIASES:-install,uninstall,start,test,build,format,lint,k/link,help}" ); do | ||
[[ "${NX_COMMAND}" =~ ^(0|false|FALSE)$ ]] && NX_COMMAND='nx' | ||
alias "${NX_COMMAND:-n}${word:0:1}"="nx ${word#[a-z]/}"; [[ -n "${NX_VERBOSE}" ]] && alias "${NX_COMMAND:-n}${word:0:1}" | ||
if [[ "${word}" =~ ^(un)?install$ ]]; then | ||
alias "${NX_COMMAND:-n}${word:0:1}s"="nx ${word#[a-z]/} --save"; [[ -n "${NX_VERBOSE}" ]] && alias "${NX_COMMAND:-n}${word:0:1}s" | ||
alias "${NX_COMMAND:-n}${word:0:1}d"="nx ${word#[a-z]/} --save-dev"; [[ -n "${NX_VERBOSE}" ]] && alias "${NX_COMMAND:-n}${word:0:1}d" | ||
alias "${NX_COMMAND:-n}${word:0:1}g"="nx ${word#[a-z]/} --global"; [[ -n "${NX_VERBOSE}" ]] && alias "${NX_COMMAND:-n}${word:0:1}g" | ||
fi | ||
done | ||
fi |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
{ | ||
"name": "enex", | ||
"version": "0.0.8", | ||
"name": "nx.sh", | ||
"version": "1.0.0", | ||
"description": "A minimalist command runner for npm packages, with a focus on developer ergonomics.", | ||
"main": "enex", | ||
"main": "nx.sh", | ||
"files": [ | ||
"enex.sh" | ||
"nx.sh" | ||
], | ||
"bin": { | ||
"enex": "enex" | ||
"nx": "nx" | ||
}, | ||
"scripts": { | ||
"lint": "shellcheck enex.sh" | ||
"lint": "shellcheck nx.sh" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/evnp/enex.git" | ||
"url": "https://github.com/evnp/nx.git" | ||
}, | ||
"homepage": "https://github.com/evnp/enex", | ||
"homepage": "https://nx.evnp.sh", | ||
"author": "Evan Purcer <[email protected]> (http://evnp.ca)", | ||
"license": "MIT", | ||
"devDependencies": { | ||
|