Merge pull request #830 from syohex/issue-829

Fix issue when package.json is not in current directory
This commit is contained in:
Shohei YOSHIDA 2021-12-21 23:54:32 +09:00 committed by GitHub
commit b3876c5982
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 4 deletions

View File

@ -85,33 +85,57 @@ _global_commands=(
'upgrade-interactive:Interactively upgrade packages'
)
_yarn_find_package_json() {
local dir=$(cd "$1" && pwd)
while true
do
if [[ -e "${dir}/package.json" ]]; then
echo "${dir}/package.json"
return
fi
if [[ $dir == '/' ]]; then
break
fi
dir=$(dirname $dir)
done
}
_yarn_commands_scripts() {
local -a scripts binaries
local packageJson
if [[ -n $opt_args[--cwd] ]]; then
scripts=($(cd $opt_args[--cwd] && cat package.json | perl -0777 -MJSON::PP -n -E '$r=decode_json($_); say for sort keys %{$r->{scripts}}'))
packageJson=$(_yarn_find_package_json $opt_args[--cwd])
binaries=($(cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t)))
else
scripts=($(cat package.json | perl -0777 -MJSON::PP -n -E '%r=decode_json($_); say for sort keys %{$r->{scripts}}'))
packageJson=$(_yarn_find_package_json $pwd)
binaries=($(echo node_modules/.bin/*(x:t)))
fi
if [[ -n $packageJson ]]; then
scripts=($(cat "$packageJson" | perl -0777 -MJSON::PP -n -E '%r=decode_json($_); say for sort keys %{$r->{scripts}}'))
fi
_describe 'command or script' _commands -- _global_commands -- scripts -- binaries
}
_yarn_scripts() {
local -a binaries scripts
local -a commands
local packageJson
if [[ -n $_yarn_run_cwd ]]; then
scripts=("${(@f)$(cd $_yarn_run_cwd && cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}")
packageJson=$(_yarn_find_package_json $_yarn_run_cwd)
if [[ -d "${_yarn_run_cwd}/node_modules" ]]; then
binaries=($(cd $_yarn_run_cwd && echo node_modules/.bin/*(x:t)))
else
binaries=($(cd $_yarn_run_cwd && yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1'))
fi
else
scripts=("${(@f)$(cat package.json | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}")
packageJson=$(_yarn_find_package_json $pwd)
if [[ -d node_modules ]]; then
binaries=($(echo node_modules/.bin/*(x:t)))
else
@ -119,6 +143,10 @@ _yarn_scripts() {
fi
fi
if [[ -n $packageJson ]]; then
scripts=("${(@f)$(cat ${packageJson} | perl -0777 -MJSON::PP -n -E '%r=%{decode_json($_)->{scripts}}; printf "$_:$r{$_}\n" for sort keys %r')}")
fi
commands=('env' $scripts $binaries)
_describe 'command' commands
}