_yarn
17550 bytes
1#compdef yarn
2# ------------------------------------------------------------------------------
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are met:
5# * Redistributions of source code must retain the above copyright
6# notice, this list of conditions and the following disclaimer.
7# * Redistributions in binary form must reproduce the above copyright
8# notice, this list of conditions and the following disclaimer in the
9# documentation and/or other materials provided with the distribution.
10# * Neither the name of the zsh-users nor the
11# names of its contributors may be used to endorse or promote products
12# derived from this software without specific prior written permission.
13#
14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24# ------------------------------------------------------------------------------
25# Description
26# -----------
27#
28# Completion script for yarn (https://yarnpkg.com/)
29#
30# ------------------------------------------------------------------------------
31# Authors
32# -------
33#
34# * Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
35# * Shohei YOSHIDA <syohex@gmail.com>
36#
37# ------------------------------------------------------------------------------
38
39declare -g _yarn_run_cwd
40
41_commands=(
42 'access'
43 'audit:Checks for known security issues with the installed packages'
44 'autoclean:Clean and remove unnecessary files from package dependencies'
45 'cache:List or clean every cached package'
46 "check:Verify package dependencies against yarn's lock file"
47 'config:Manages the yarn configuration files'
48 'create:Creates new projects from any create-* starter kits'
49 'exec'
50 'generate-lock-entry:Generates a lock file entry'
51 'global:Install packages globally on your operating system'
52 'help:Show information about a command'
53 'import:Generate yarn.lock from an existing npm-installed node_modules folder'
54 'info:Show information about a package'
55 'init:Interactively creates or updates a package.json file'
56 'install:Install all the dependencies listed within package.json'
57 'licenses:List licenses for installed packages'
58 'link:Symlink a package folder during development'
59 'login:Store registry username and email'
60 'logout:Clear registry username and email'
61 'node:Runs Node with the same version that the one used by Yarn itself'
62 'outdated:Check for outdated package dependencies'
63 'owner:Manage package owners'
64 'pack:Create a compressed gzip archive of package dependencies'
65 'policies:Defines project-wide policies for your project'
66 'publish:Publish a package to the npm registry'
67 'run:Run a defined package script'
68 'tag:Add, remove, or list tags on a package'
69 'team:Maintain team memberships'
70 'unlink:Unlink a previously created symlink for a package'
71 'unplug:Temporarily copies a package outside of the global cache for debugging purposes'
72 'version:Update the package version'
73 'versions:Display version information of currently installed Yarn, Node.js, and its dependencies'
74 'why:Show information about why a package is installed'
75 'workspace'
76 'workspaces:Show information about your workspaces'
77)
78
79_global_commands=(
80 'add:Installs a package and any packages that it depends on'
81 'bin:Displays the location of the yarn bin folder'
82 'list:List installed packages'
83 'remove:Remove installed package from dependencies updating package.json'
84 'upgrade:Upgrades packages to their latest version based on the specified range'
85 'upgrade-interactive:Interactively upgrade packages'
86)
87
88_yarn_find_package_json() {
89 local dir=$(builtin cd "$1" && pwd)
90
91 while true
92 do
93 if [[ -e "${dir}/package.json" ]]; then
94 echo "${dir}/package.json"
95 return
96 fi
97
98 if [[ $dir == '/' ]]; then
99 break
100 fi
101
102 dir=$(dirname $dir)
103 done
104}
105
106_yarn_commands_scripts() {
107 local -a scripts binaries
108 local packageJson
109
110 if [[ -n $opt_args[--cwd] ]]; then
111 packageJson=$(_yarn_find_package_json $opt_args[--cwd])
112 binaries=($(builtin cd $opt_args[--cwd] && echo node_modules/.bin/*(x:t)))
113 else
114 packageJson=$(_yarn_find_package_json $PWD)
115 binaries=($(echo node_modules/.bin/*(x:t)))
116 fi
117
118 if [[ -n $packageJson ]]; then
119 scripts=($(cat "$packageJson" | perl -0777 -MJSON::PP -n -E '$r=decode_json($_); do{($k=$_)=~s/:/\\:/g;say $k}for sort keys %{$r->{scripts}}'))
120 fi
121
122 _describe 'command or script' _commands -- _global_commands -- scripts -- binaries
123}
124
125_yarn_scripts() {
126 local -a binaries scripts
127 local -a commands
128 local packageJson
129
130 if [[ -n $_yarn_run_cwd ]]; then
131 packageJson=$(_yarn_find_package_json $_yarn_run_cwd)
132 if [[ -d "${_yarn_run_cwd}/node_modules" ]]; then
133 binaries=($(builtin cd $_yarn_run_cwd && echo node_modules/.bin/*(x:t)))
134 else
135 binaries=($(builtin cd $_yarn_run_cwd && yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1'))
136 fi
137 else
138 packageJson=$(_yarn_find_package_json $PWD)
139 if [[ -d node_modules ]]; then
140 binaries=($(echo node_modules/.bin/*(x:t)))
141 else
142 binaries=($(yarn bin | perl -wln -e 'm{^[^:]+: (\S+)$} and print $1'))
143 fi
144 fi
145
146 if [[ -n $packageJson ]]; then
147 scripts=("${(@f)$(cat ${packageJson} | perl -0777 -MJSON::PP -n -E 'binmode(STDOUT, ":encoding(UTF-8)"); %r=%{decode_json($_)->{scripts}}; do{$k=$_;($e=$k)=~s/:/\\:/g; printf "$e:$r{$k}\n"} for sort keys %r')}")
148 fi
149
150 commands=('env' $scripts $binaries)
151 _describe 'command' commands
152}
153
154_yarn_global_commands() {
155 local -a cmds
156 cmds=('ls:List installed packages')
157 _describe 'command' _global_commands
158}
159
160_yarn_commands() {
161 _describe 'command' _commands -- _global_commands
162}
163
164_yarn_add_files() {
165 if compset -P "(file|link):"; then
166 _files
167 fi
168}
169
170_yarn_workspaces() {
171 local version=$(yarn --version |sed -n 's|\([0-9]*\).*|\1|p')
172 local -a workspaces
173 if [[ $version == "1" ]]; then
174 workspaces=(${(@f)$(yarn workspaces info |sed -n -e 's/^ "\([^"]*\)": {/\1/p')})
175 else
176 workspaces=(${(@f)$(yarn workspaces list --json | sed -n 's|.*"name":"\([^"]*\)"}|\1|p')})
177 fi
178 _describe 'workspace' workspaces
179}
180
181_yarn() {
182 local context state state_descr line
183 typeset -A opt_args
184
185 _arguments \
186 '(-h --help)'{-h,--help}'[output usage information]' \
187 '(-V --version)'{-V,--version}'[output the version number]' \
188 '--verbose[output verbose messages on internal operations]' \
189 '--cache-folder=[specify a custom folder to store the yarn cache]:folder:_files -/' \
190 '--check-files[install will verify file tree of packages for consistency]' \
191 '--cwd=[working directory to use]:path:_files -/' \
192 "(--enable-pnp --pnp)--disable-pnp[disable the Plug'n'Play installation]" \
193 '(--no-emoji)--emoji=[enable emoji in output(default: false)]:enabled:(true false)' \
194 '(--emoji)--no-emoji[disable emoji in output]' \
195 '(--disable-pnp)'{--enable-pnp,--pnp}"[enable the Plug'n'Play installation]" \
196 '--flat[only allow one version of a package]' \
197 '--focus[Focus on a single workspace by installing remote copies of its sibling workspaces]' \
198 '--force[install and build packages even if they were built before, overwrite lockfile]' \
199 "--frozen-lockfile[don't generate a lockfile and fail if an update is needed]" \
200 '--global-folder=[modules folder]:folder:_files -/' \
201 '--har[save HAR output of network traffic]' \
202 '--https-proxy=[HTTPS proxy]:host:_hosts' \
203 '--ignore-engines[ignore engines check]' \
204 "--ignore-scripts[don't run lifecycle scripts]" \
205 '--ignore-optional[ignore optional dependencies]' \
206 '--ignore-platform[ignore platform checks]' \
207 '--json[format Yarn log messages as lines of JSON]' \
208 '--link-duplicates[create hardlinks to the repeated modules in node_modules]' \
209 '--link-folder=[specify a custom folder to store global links]' \
210 '--modules-folder=[rather than installing modules into the node_modules folder relative to the cwd, output them here]:folder:_files -/' \
211 '--mutex=[use a mutex to ensure only one yarn instance is executing]:type[\:specifier]' \
212 '--network-concurrency=[maximum number of concurrent network requests]:number' \
213 '--network-timeout=[TCP timeout for network requests]:milliseconds' \
214 "--no-bin-links[don't generate bin links when setting up packages]" \
215 '--no-default-rc[prevent Yarn from automatically detecting yarnrc and npmrc files]' \
216 "--no-lockfile[don't read or generate a lockfile]" \
217 '--non-interactive[do not show interactive prompts]' \
218 '--no-node-version-check[do not warn when using a potentially unsupported Node version]' \
219 '--no-progress[disable progress bar]' \
220 '--offline[trigger an error if any required dependencies are not available in local cache]' \
221 '--otp=[one-time password for two factor authentication]:otpcode' \
222 '--prefer-offline[use network only if dependencies are not available in local cache]' \
223 '--preferred-cache-folder=[specify a custom folder to store the yarn cache if possible]:folder:_files -/' \
224 '(--prod --production)'{--prod,--production}'[install only production dependencies]' \
225 '--proxy=[HTTP proxy]:host:_hosts' \
226 "--pure-lockfile[don't generate a lockfile]" \
227 '--registry=[override configuration registry]:url:_urls' \
228 '(-s --silent)'{-s,--silent}'[skip Yarn console logs, other types of logs (script output) will be printed]' \
229 '--scripts-prepend-node-path=[prepend the node executable dir to the PATH in scripts]:bool:(true false)' \
230 '--skip-integrity-check[run install without checking if node_modules is installed]' \
231 "--strict-semver[don't compare semver loosely]" \
232 '--update-checksum[update package checksums from current repository]' \
233 '--use-yarnrc=[specifies a yarnrc that Yarn should use]:yarnrc:_files' \
234 '1: :_yarn_commands_scripts' \
235 '*:: :->command_args'
236
237
238 case $state in
239 command_args)
240 case $words[1] in
241 help)
242 _arguments \
243 '1: :_yarn_commands' \
244 ;;
245
246 access)
247 _arguments \
248 '1: :(public restricted grant revoke ls-packages ls-collaborators edit)'
249 ;;
250
251 add)
252 _arguments \
253 '(-D --dev)'{-D,--dev}'[install packages in devDependencies]' \
254 '(-P --peer)'{-P,--peer}'[install packages in peerDependencies]' \
255 '(-O --optional)'{-O,--optional}'[install packages in optionalDependencies]' \
256 '(-E --exact)'{-E,--exact}'[install packages as exact versions]' \
257 '(-T --tilde)'{-T,--tilde}'[install the most recent release of the packages that have the same minor version]' \
258 '(--ignore-workspace-root-check -W)'{--ignore-workspace-root-check,-W}'[allows a package to be installed at the workspaces root]' \
259 '--audit[checks for known security issues with the installed packages]' \
260 '*:package-name:_yarn_add_files'
261 ;;
262
263 audit)
264 _arguments \
265 '--verbose[output verbose message]' \
266 '--json[format Yarn log messages as lines of JSON]' \
267 '--level=[only print advisories with severity greater than or equal to]:level:(info low moderate high critical)' \
268 '--groups=[only audit dependencies from listed groups]:groups:->groups_args'
269 ;;
270
271 cache)
272 _arguments \
273 '1: :(list dir clean)' \
274 '*:: :->cache_args'
275 ;;
276
277 check)
278 _arguments \
279 '--integrity[Verifies that versions and hashed values of the package contents in package.json]' \
280 '--verify-tree[Recursively verifies that the dependencies in package.json are present in node_modules]'
281 ;;
282
283 config)
284 _arguments \
285 '1: :(set get delete list)' \
286 '*:: :->config_args'
287 ;;
288
289 global)
290 _arguments \
291 '--prefix=[bin prefix to use to install binaries]' \
292 '1: :_yarn_global_commands' \
293 '*:: :->command_args'
294 ;;
295
296 info)
297 _arguments \
298 '1:package:' \
299 '2:field'
300 ;;
301
302 init)
303 _arguments \
304 '(-y --yes)'{-y,--yes}'[install packages in devDependencies]'
305 ;;
306
307 licenses)
308 _arguments \
309 '1: :(ls generate-disclaimer)' \
310 ;;
311
312 link|unlink|outdated)
313 _arguments \
314 '1:package' \
315 ;;
316
317 list)
318 _arguments \
319 '--depth=[Limit the depth of the shown dependencies]:depth' \
320 '--pattern=[filter the list of dependencies by the pattern]'
321 ;;
322
323 owner)
324 _arguments \
325 '1: :(list add rm)' \
326 '*:: :->owner_args'
327 ;;
328
329 pack)
330 _arguments \
331 '(-f --filename)'{-f,--filename}':filename:_files'
332 ;;
333
334 publish)
335 _arguments \
336 '--new-version:version:' \
337 '--message:message:' \
338 '--no-git-tag-version' \
339 '--access:access:' \
340 '--tag:tag:' \
341 '1: :_files'
342 ;;
343
344 policies)
345 _arguments \
346 '1: :(set-version)'
347 ;;
348
349 remove|upgrade)
350 _arguments \
351 '*:package:'
352 ;;
353
354 run)
355 if [[ -n $opt_args[--cwd] ]]; then
356 _yarn_run_cwd=$opt_args[--cwd]
357 else
358 _yarn_run_cwd=''
359 fi
360 _arguments \
361 '1: :_yarn_scripts' \
362 '*:: :_default'
363 ;;
364
365 tag)
366 _arguments \
367 '1: :(lists add rm)' \
368 '*:: :->tag_args'
369 ;;
370
371 team)
372 _arguments \
373 '1: :(create destroy add rm list)' \
374 '*:: :->team_args'
375 ;;
376
377 upgrade-interactive)
378 _arguments \
379 '--latest[use the version tagged latest in the registry]'
380 ;;
381
382 version)
383 _arguments \
384 '--new-version[create a new version using an interactive session to prompt you]:version:' \
385 '--major[creates a new version by incrementing the major version]' \
386 '--minor[creates a new version by incrementing the minor version]' \
387 '--patch[creates a new version by incrementing the patch version]' \
388 '--premajor[creates a new prerelease version by incrementing the major version]' \
389 '--preminor[creates a new prerelease version by incrementing the minor version]' \
390 '--prepatch[creates a new prerelease version by incrementing the patch version]' \
391 '--prerelease[increments the prerelease version number keeping the main version]' \
392 '--no-git-tag-version[creates a new version without creating a git tag]' \
393 '--no-commit-hooks[bypasses running commit hooks when committing the new version]'
394 ;;
395
396 why)
397 _arguments \
398 '1:query:_files'
399 ;;
400
401 workspace)
402 _arguments \
403 '1:workspace:_yarn_workspaces' \
404 '*:: :_yarn_global_commands'
405 ;;
406
407 workspaces)
408 _arguments \
409 '--json[format Yarn log messages as lines of JSON]' \
410 '1:commands:(info run)'
411 ;;
412
413 *)
414 _default
415 ;;
416 esac
417 ;;
418 esac
419
420 case $state in
421 cache_args)
422 if [[ $words[1] == "list" ]]; then
423 _arguments \
424 '--pattern=[print out every cached package that matches the pattern]:pattern:'
425 fi
426 ;;
427 config_args)
428 case $words[1] in
429 get|delete)
430 _arguments \
431 '1:key:'
432 ;;
433
434 set)
435 _arguments \
436 '(-g --global)'{-g,--global} \
437 '1:key:' \
438 '2:value:'
439 ;;
440 esac
441 ;;
442 groups_args)
443 local dependency_groups=(devDependencies dependencies optionalDependencies peerDependencies bundledDependencies)
444 _values -s ',' 'groups' $dependency_groups
445 ;;
446
447 owner_args)
448 case $words[1] in
449 ls)
450 _arguments \
451 '1:package:'
452 ;;
453
454 add|rm)
455 _arguments \
456 '1:user:' \
457 '2:package:'
458 ;;
459 esac
460 ;;
461
462 tag_args)
463 case $words[1] in
464 ls)
465 _arguments \
466 '1:package'
467 ;;
468
469 add|rm)
470 _arguments \
471 '1:package:' \
472 '2:tag:'
473 ;;
474 esac
475 ;;
476
477 team_args)
478 case $words[1] in
479 create|destroy|ls)
480 _arguments \
481 '1:scope\:team:'
482 ;;
483
484 add|rm)
485 _arguments \
486 '1:scope\:team:' \
487 '2:user:'
488 ;;
489 esac
490 ;;
491 esac
492}
493
494_yarn "$@"
495
496# Local Variables:
497# mode: Shell-Script
498# sh-indentation: 2
499# indent-tabs-mode: nil
500# sh-basic-offset: 2
501# End:
502# vim: ft=zsh sw=2 ts=2 et