From d07d299327feb3c6b49fadfab250083e2d1930d0 Mon Sep 17 00:00:00 2001 From: iirhe Date: Tue, 10 Dec 2024 00:26:50 +0800 Subject: [PATCH 1/2] feat: add completion support for remove/uninstall commands Modified existing logic to include completion support for remove/uninstall commands in npm, yarn, and pnpm. --- getDependencies.js | 8 +++ getScripts.js | 1 - zsh-npm-scripts-autocomplete.plugin.zsh | 86 ++++++++++++++++++------- 3 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 getDependencies.js diff --git a/getDependencies.js b/getDependencies.js new file mode 100644 index 0000000..a810829 --- /dev/null +++ b/getDependencies.js @@ -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) diff --git a/getScripts.js b/getScripts.js index 1466124..d4e6d67 100644 --- a/getScripts.js +++ b/getScripts.js @@ -1,4 +1,3 @@ - const pkg = require(process.argv[2]) const result = Object diff --git a/zsh-npm-scripts-autocomplete.plugin.zsh b/zsh-npm-scripts-autocomplete.plugin.zsh index d2f84b7..67fd719 100644 --- a/zsh-npm-scripts-autocomplete.plugin.zsh +++ b/zsh-npm-scripts-autocomplete.plugin.zsh @@ -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 @@ -21,42 +27,74 @@ __znsaArgsLength() { } __znsaYarnRunCompletion() { - [[ ! "$(__znsaArgsLength)" = "2" ]] && return + # Return if the length of arguments is not 2 or 3 + local argsLength="$(__znsaArgsLength)" + [[ "$argsLength" -ne "2" || "$argsLength" -ne "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 + + # Handle `yarn