nx-completion
nx-completion copied to clipboard
> NX Running global Nx CLI with PNPM may have issues.
When running nx in pnpm managed repo, I get the following warning (which sometimes even results in not working command)
> NX Running global Nx CLI with PNPM may have issues.
Prefer to use "pnpm" (https://pnpm.io/cli/exec) to execute commands in this workspace.
TIP create a shortcut such as: alias pnx="pnpm nx --"
Using the alias from the TIP would break this autocompletion, so would be really great if the plugin could work with PNPM natively and run pnpm nx whenever nx is used.
Here is what I came up with as a solution for me:
In .zrhrc
function pnpm-nx() {
local cmd=$BUFFER
if [[ "$cmd" =~ ^nx ]]; then
local new_cmd=$(echo $cmd | sed -E 's/^nx /pnpm nx /g')
BUFFER="$new_cmd"
fi
zle accept-line
}
zle -N pnpm-nx
bindkey "^M" pnpm-nx
So, now whenever I type nx {command} it gets replaced by pnpm nx {command} automatically on Enter. This also doesn't break this autocompletion plugin
I recently started to use pnpm and also noticed the warning but I just ignored it, I wonder what issues it can cause and why. I agree it would be great to handle this directly in the plugin, if you want to contribute you are welcome.
Sometimes, some command will fail if used without pnpm prefix. Usually more complex commands like nx generate or nx migrate, so this is not only warning.
I might take a look on this, but not sure I'll have time. But feel free to reuse that piece of code I've shared.
btw, not sure if this is possible, but I would improve the behavior of that script a bit.
The downside of script from above is that pnpm nx.. command gets written to history instead of nx .... So, I can't navigate back in history with arrows and re-use nx ... command with substitution. nx-completion plugin won't work.
Probably the solution would be to alter terminal history somehow after execution and write there nx ... instead of pnpm nx ...
I'm not sure if this helps in your case, but I have added this tiny script to my .zshrc to get autocomplete working whithout nx being globally installed (yarn in my case):
function nx() {
yarn nx "$@"
}
Just as an idea for potential solution. There is a package I use https://github.com/antfu/ni which can automatically detect package manager used in the project and install dependencies with proper manager.
So, this plugin, potentially could do the same – detect package manager used and append yarn , pnpm or nothing in case of npm
and @Peeeep , your solution works like a charm, thanks.
In my case, I did the following for pnpm
# ~/.zshrc file
function nx() {
pnpm nx "$@"
}