Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPM Completion Conflict and Add Support for pnpm/yarn Dependency Removal #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions getDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const pkg = require(process.argv[2])

const result = Object
.entries({ ...pkg.dependencies, ...pkg.devDependencies })
.map(entry => `${entry[0]}: ${entry[1]}`)
.join('\n')

console.log(result)
2 changes: 0 additions & 2 deletions getScripts.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

const pkg = require(process.argv[2])

const result = Object
.entries(pkg.scripts || {})
.filter(entry => /^\w/.test(entry[0]))
.map(entry => {
entry[0] = entry[0].replace(/([$:])/ig, '\\$1')
entry[1] = entry[1].replace(/([$:])/ig, '\\$1')
Expand Down
68 changes: 36 additions & 32 deletions zsh-npm-scripts-autocomplete.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
local _plugin_path=$0
local _PWD=`echo $_plugin_path | sed -e 's/\/zsh-npm-scripts-autocomplete\.plugin\.zsh//'`
__zna_pwd="$_PWD"

__znsaGetDependencies() {
local pkgJson="$1"
node "$__zna_pwd/getDependencies.js" "$pkgJson" 2>/dev/null
}

__znsaGetScripts() {
local pkgJson="$1"
node "$__zna_pwd/getScripts.js" "$pkgJson" 2>/dev/null
Expand All @@ -21,42 +27,40 @@ __znsaArgsLength() {
}

__znsaYarnRunCompletion() {
[[ ! "$(__znsaArgsLength)" = "2" ]] && return
local pkgJson="$(__znsaFindFile package.json)"
[[ "$pkgJson" = "" ]] && return
local -a options
options=(${(f)"$(__znsaGetScripts $pkgJson)"})
[[ "$#options" = 0 ]] && return
_describe 'values' options
}
# Return if the length of arguments is not 2 or 3
local argsLength="$(__znsaArgsLength)"
[[ "$argsLength" -ne "2" && "$argsLength" -ne "3" ]] && return

## to lazy to handler different number of arguments
## just copy and paste it
__znsaNpmRunCompletion() {
[[ ! "$(__znsaArgsLength)" = "3" ]] && return
# Return if package.json is not found
local pkgJson="$(__znsaFindFile package.json)"
[[ "$pkgJson" = "" ]] && return
local -a options
options=(${(f)"$(__znsaGetScripts $pkgJson)"})
[[ "$#options" = 0 ]] && return
_describe 'values' options
}
[[ "$pkgJson" == "" ]] && return

__znsaHandleYarn() {
__znsaYarnRunCompletion
}
# Handle `yarn <script>` command
if [[ "$argsLength" = "2" ]]; then
# Get the scripts
local -a options=(${(f)"$(__znsaGetScripts $pkgJson)"})
[[ "$#options" = 0 ]] && return

# Describe the options for autocompletion
_describe 'values' options
return
fi

# Handle `yarn/pnpm remove/rm <dependency>` command
if [[ "$argsLength" = "3" ]]; then
# Return if the command is not `remove` or `rm`
[[ "${words[2]}" != "rm" && "${words[2]}" != "remove" ]] && return

__znsaHandleNpm(){
case "${words[2]}" in
run)
__znsaNpmRunCompletion
;;
esac
# Get the dependencies
local -a options=(${(f)"$(__znsaGetDependencies $pkgJson)"})
[[ "$#options" = 0 ]] && return

# Describe the options for autocompletion
_describe 'values' options
return
fi
}

alias nr="npm run"
compdef __znsaYarnRunCompletion yarn
compdef __znsaYarnRunCompletion nr
compdef __znsaYarnRunCompletion pnpm
compdef __znsaHandleNpm npm
compdef __znsaHandleNpm bun
compdef __znsaYarnRunCompletion yarn
compdef __znsaNpmRunCompletion bun