Skip to content

Commit

Permalink
1.0.3 :: auto-find package.json in ./frontend sub-dir if present
Browse files Browse the repository at this point in the history
  • Loading branch information
evnp committed Nov 24, 2023
1 parent 70acc67 commit 6e287cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
31 changes: 14 additions & 17 deletions nx.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#!/usr/bin/env bash
# :: nx 1.0.2 ::
# :: nx 1.0.3 ::
# 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' )" || true
# :: 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
[[ -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|"* ]] && cmd=('npm' "$1") || cmd=('npm' 'run' "$1")
[[ "$2" == 'w' ]] && set -- "$1" "--watch" "${@:3}" # :: rewrite 'w' to '--watch' if provided as second arg ""
[[ "$2" == 'w' ]] && set -- "$1" '--watch' "${@:3}" # :: rewrite 'w' to '--watch' if provided as second arg ""
shift # :: done with command arg, following args are all arguments to command ::
# :: 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" ]] && [[ "$1" != '--global' ]]; do
[[ "$( pwd )" == "$HOME" ]] && echo "No package.json found." && exit 1
# :: find closest package.json (file path containing fewest "/" characters) :: to disable auto-find behavior, export NX_FIND=0; source "$HOME/nx/nx.sh" ::
while [[ ! "${NX_FIND}" =~ ^(0|false|FALSE)$ && ! -f './package.json' ]] && [[ "$1" != '--global' ]]; do
[[ -f './frontend/package.json' ]] && cd './frontend' && break # :: look for ./frontend/package.json first ::
[[ "$( 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) ::
# :: call "nvm use" automatically before running commands :: to enable auto-nvm behavior, export NX_NVM=1; source "$HOME/nx/nx.sh" ::
[[ "${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) ::
# :: await confirmation of current node+npm versions before executing command :: to enable await-confirm behavior, export NX_CONFIRM=1; source "$HOME/nx/nx.sh" ::
if [[ "${NX_CONFIRM}" =~ ^(1|true|TRUE)$ ]]; then
read -rsn1 -p "${cmd[*]} ${*}"$'\n'"${cmd[*]//?/˙} ${*//?/˙}"$'\n'"Press any key to run · CTRL+C to cancel · node $( node -v ) · npm $( npm -v )"$'\n\n'
fi
Expand All @@ -26,17 +27,13 @@ function nx() ( local pkg="" cmd="" npmcmds=""
"${cmd[@]}" # :: execute npm command with no additional args::
fi
)
# :: 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
# :: default command alias :: n -> nx :: eg. n install -> npm install, n build -> npm run build, etc. :: to change name, export NX_COMMAND="name"; source "$HOME/nx/nx.sh" ::
# :: default sub-command aliases :: to disable all aliases, export NX_ALIASES=0; source "$HOME/nx/nx.sh" ::
# nb -> npm run build nt -> npm test nf -> npm run format nl -> npm run lint nh -> npm run help np -> npm publish
# 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
alias "${NX_COMMAND:-n}"='nx'
for word in $( tr -cs '[:alnum:]._-/' ' ' <<< "${NX_ALIASES:-install,uninstall,start,test,build,format,lint,k/link,publish,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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nx.sh",
"version": "1.0.2",
"version": "1.0.3",
"description": "A minimalist command runner for npm packages, with a focus on developer ergonomics.",
"main": "nx.sh",
"files": [
Expand Down

0 comments on commit 6e287cb

Please sign in to comment.