aboutsummaryrefslogtreecommitdiffstats
path: root/src/_yarn
blob: 0e5dd4c80c083f05bafbe45bfbbd05c0bc3829fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
#compdef yarn
# ------------------------------------------------------------------------------
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for yarn 4.11.0 (https://yarnpkg.com/)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
#  * Shohei YOSHIDA <syohex@gmail.com>
#
# ------------------------------------------------------------------------------

_yarn() {
  typeset -A opt_args
  local context state line
  local curcontext="$curcontext"
  local ret=1

  _arguments -C  \
    '(- *)'{-h,--help}'[show help message and exit]' \
    '(- *)'{-v,--version}'[show version number and exit]' \
    '1: :_yarn_subcommands' \
    '*::arg:->args' \
    && ret=0

  case "$state" in
    (args)
      case $words[1] in
        (add)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            '(-F --fixed)'{-F,--fixed}'[store dependency tags as-is instead of resolving them]' \
            '(-E --exact)'{-E,--exact}'[do not use any semver modifier on the resolved range]' \
            '(-T --tilde)'{-T,--tilde}'[use the "~" semver modifier on the resolved range]' \
            '(-C --caret)'{-C,--caret}'[use the "^" semver modifier on the resolved range]' \
            '(-D --dev)'{-D,--dev}'[add a package as a dev dependency]' \
            '(-P --peer)'{-P,--peer}'[add a package as a peer dependency]' \
            '(-O --optional)'{-O,--optional}'[add/upgrade a package to an optional regular/peer dependency]' \
            '--prefer-dev[add/upgrade a package to a dev dependency]' \
            '(-i --interactive)'{-i,--interactive}'[reuse the specified package from other workspaces in the project]' \
            '--cached[reuse the highest version already used somewhere within the project]' \
            '--mode[change what artifacts installs generate]:mode' \
            '*::package:_files' \
            && ret=0
           ;;
        (bin)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-v --verbose)'{-v,--verbose}'[print both the binary name and the locator of the package]' \
            '--json[format the output as an NDJSON stream]' \
            '*::package' \
            && ret=0
          ;;
        (cache)
          _yarn_cache && ret=0
          ;;
        (config)
          _yarn_config && ret=0
          ;;
        (constraints)
          _yarn_constraints && ret=0
          ;;
        (dedupe)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-s --strategy)'{-s,--strategy}'[strategy to use when dedupe dependencies]:strategy:(highest)' \
            '(-c --check)'{-c,--check}'[exit with exit code 1 when duplicates are found]' \
            '--json[format the output as an NDJSON stream]' \
            '--mode[change what artifacts installs generate]:mode:(skip-build update-lockfile)' \
            '*::package:_files' \
            && ret=0
          ;;
        (dlx)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            \*{-p,--package}'[package to install before the running the command]:package' \
            '(-q --quiet)'{-q,--quiet}'[only report critical errors instead of printing the full install logs]' \
            '*: :_normal' \
            && ret=0
          ;;
        (exec)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '*: :_normal' \
            && ret=0
          ;;
        (explain)
          _yarn_explain && ret=0
          ;;
        (info)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[print versions of a package from the whole project]' \
            '(-R --recursive)'{-R,--recursive}'[print information for all packages, including transitive dependencies]' \
            '(-X --extra)'{-X,--extra}'[an array of requests of extra data provided by plugins]:extra_data' \
            '--cache[print information about the cache entry of a package]' \
            '--dependents[print all dependents for each matching package]' \
            '--manifest[print data obtained by looking at the package archive]' \
            '--name-only[only print the name for the matching packages]' \
            '--virtuals[print each instance of the virtual packages]' \
            '--json[format the output as an NDJSON stream]' \
            '*::package:_files' \
            && ret=0
          ;;
        (init)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-p --private)'{-p,--private}'[initialize a private package]' \
            '(-w --workspace)'{-w,--workspace}'[initialize a workspace root with a packages/directory]' \
            '(-i --install)'{-i,--install}'[initialize package with a specific bundle]' \
            '(-n --name)'{-n,--name}'[initialize a package with the given name]:name' \
            '*::initializer' \
            && ret=0
          ;;
        (install)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            '--immutable[abort if the lockfile was to be modified]' \
            '--immutable-cache[abort if the cache folder was to be modified]' \
            '--refresh-lockfile[refresh the package metadata stored in the lockfile]' \
            '--check-cache[always refetch the packages and ensure that their checksums are consistent]' \
            '--check-resolutions[valudata that the package resolutions are coherent]' \
            '--inline-builds[verbosely print the output of the build steps of dependencies]' \
            '--mode[change what artifacts installs generate]:mode:(skip-build update-lockfile)' \
            && ret=0
          ;;
        (link)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[link all workspaces belonging to target projects to current one]' \
            '(-p --private)'{-p,--private}'[also link private workspaces belonging to target projects to current one]' \
            '(-r --relative)'{-r,--relative}'[link workspaces using relative paths instead of absolute paths]' \
            '*:workspace:_files -/' \
            && ret=0
          ;;
        (node)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '*:node:_node' \
            && ret=0
          ;;
        (npm)
          _yarn_npm && ret=0
          ;;
        (pack)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--install-if-needed[run a preliminary "yarn install" if the package contains build scripts]' \
            '(-n --dry-run)'{-n,--dry-run}'[print the file paths without actually generating package archive]' \
            '--json[format the output as an NDJSON stream]' \
            '(-o --out)'{-o,--out}'[create the archive at the specified path]:path:_files' \
            && ret=0
          ;;
        (patch)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-u --update)'{-u,--update}'[reapply local patches that already apply to this package]' \
            '--json[format the output as an NDJSON stream]' \
            && ret=0
          ;;
        (patch-commit)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-s --save)'{-s,--save}'[add the patch to your resoltion entries]' \
            '*:dir:_files -/' \
            && ret=0
          ;;
        (plugin)
          _yarn_plugin && ret=0
          ;;
        (rebuild)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            && ret=0
          ;;
        (remove)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[apply operation to all workspaces from the current project]' \
            '--mode[change what artifacts installs generate]:mode:(skip-build update-lockfile)' \
            '*:package' \
            && ret=0
          ;;
        (run)
          _yarn_run && ret=0
          ;;
        (search)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            && ret=0
          ;;
        (set)
          _yarn_set && ret=0
          ;;
        (stage)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-c --commit)'{-c,--commit}'[commit the staged files]' \
            '(-r --reset)'{-r,--reset}'[remove all files from the staging area]' \
            '(-n --dry-run)'{-n,--dry-run}'[print the commit message and modified files without staging/committing]' \
            && ret=0
          ;;
        (unlink)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[unlink all workspaces belonging to target project from current one]' \
            '*:workspace:_files -/' \
            && ret=0
          ;;
        (unplug)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[unplug direct dependencies from the entire project]' \
            '(-R --recursive)'{-R,--recursive}'[unplug both direct and transitive dependencies]' \
            '--json[format the output as an NDJSON stream]' \
            '*:package' \
            && ret=0
          ;;
        (up)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-i --interactive)'{-i,--interactive}'[offer various choices, depending on the detached upgrade paths]' \
            '(-F --fixed)'{-F,--fixed}'[store dependency tags as-is instead of resolving them]' \
            '(-E --exact)'{-E,--exact}'[do not use any semver modifier on the resolved range]' \
            '(-T --tilde)'{-T,--tilde}'[use the "~" semver modifier on the resolved range]' \
            '(-C --caret)'{-C,--caret}'[use the "^" semver modifier on the resolved range]' \
            '(-R --recursive)'{-R,--recursive}'[resolve again ALL resolutions for those packages]' \
            '--mode[change what artifacts installs generate]:mode:(skip-build update-lockfile)' \
            '*:package' \
            && ret=0
          ;;
        (upgrade-interactive)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            && ret=0
          ;;
        (version)
          _yarn_version && ret=0
          ;;
        (why)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-R --recursive)'{-R,--recursive}'[list for each workspace]' \
            '--json[format the output as an NDJSON stream]' \
            '--peers[also print the peer dependencies that match the specified name]' \
            '*:package' \
            && ret=0
          ;;
        (workspace)
          _yarn_workspace && ret=0
          ;;
        (workspaces)
          _yarn_workspaces && ret=0
          ;;
      esac
      ;;
  esac

  return ret
}

(( $+functions[_yarn_subcommands] )) ||
_yarn_subcommands() {
  local -a commands=(
    'add:add dependencies to the project'
    'bin:get the path to a binary script'
    'cache:cache related commands'
    'config:read or change current configuration'
    'dedupe:deduplicate dependencies with overlapping ranges'
    'dlx:run a package in a temporary environment'
    'exec:execute a shell script'
    'explain:explain command'
    'info:see information related to packages'
    'init:create a new package'
    'install:install the project dependencies'
    'link:connect the local project to another one'
    'node:run node with the hook already setup'
    'npm:npm related commands'
    'pack:generate a tarball from the active workspace'
    'patch:prepare a package for patching'
    'patch-commit:generate a patch out of a directory'
    "rebuild:rebuild the project's native packages"
    'remove:remove dependencies from the project'
    'run:run a script defined in the package.json'
    'set:enforce a package resolution'
    'stage:add all yarn files to your vcs'
    'unlink:disconnect the local project from another one'
    'unplug:force the unpacking of a list of packages'
    'up:upgrade dependencies across the project'
    'why:display the reason why a package is needed'
    'constraints:check that the project constraints are met'
    'search:open the search interface'
    'upgrade-interactive:open the upgrade interface'
    'plugin:plugin related commands'
    'version:version relarelated commands'
    'workspace:workspace related commands'
    'workspaces:command for workspaces'
  )

  _describe -t commands 'command' commands "$@"
}

(( $+functions[_yarn_cache] )) ||
_yarn_cache() {
  local ret=1

  _arguments -C \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '1:subcmd:(clean)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (clean)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--mirror[remove the global cache files instead of the local cache files]' \
            '--all[remove both the global cache files and the local cache files of the current project]'
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_config] )) ||
_yarn_config() {
  local ret=1

  _arguments -C \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '1:subcmd:(get set unset)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (get)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--why[print the explanation for a why a setting has its value]' \
            '--json[format the output as an NDJSON stream]' \
            '--no-redacted[do not redact secrets(such as tokens) from the output]' \
            '1:name' \
            && ret=0
          ;;
        (set)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[set complete configuration settins to JSON values]' \
            '(-H --home)'{-H,--home}'[update the home configuration instead of the project one]' \
            '1:name' \
            '2:value' \
            && ret=0
          ;;
        (unset)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-H --home)'{-H,--home}'[update the home configuration instead of the project one]' \
            '1:name' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_constraints] )) ||
_yarn_constraints() {
  local ret=1

  _arguments -C \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '(-f --fix)'{-f,--fix}'[attempt to automatically fix unambiguous issues]' \
    '--json[format the output as an NDJSON stream]' \
    '1:subcmd:(query source)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (query)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            '1:query' \
            && ret=0
          ;;
        (source)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-v --verbose)'{-v,--verbose}'[print the fact database automatically compiled from workspace manifests]' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_explain] )) ||
_yarn_explain() {
  local ret=1

  local -a error_codes=(peer-requirements $(yarn explain 2>/dev/null | awk -F'[ :]' '{ print $2 }' 2>/dev/null))

  _arguments -C \
    '(- *)'{-h,--help}'[show help message and exit]' \
    '--json[format the output as an NDJSON stream]' \
    '1:subcmd:(($error_codes))' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (peer-requirements)
          _arguments \
            '1:hash' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_npm] )) ||
_yarn_npm() {
  local ret=1

  _arguments -C \
    '1:subcmd:_yarn_npm_subcommands' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (audit)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[audit dependencies from all workspaces]' \
            '(-R --recursive)'{-R,--recursive}'[audit transitive dependencies as well]' \
            '--environment[which environment to cover]:environment:(production)' \
            '--json[format the output as an NDJSON stream]' \
            '--no-deprecations[do not warn about deprecated packages]' \
            '--severity[minimal severity requested for packages to be displayed]:severity:(info low moderate high critical)' \
            '*--exclude[array of glob patterns of packages to exclude from audit]:pattern' \
            '*--ignore[array of glob patterns of advisory ID to ignore in the audit report]:pattern' \
            && ret=0
          ;;
        (info)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-f --fields)'{-f,--fields}'[a comma-separated list of manifest fields to be displayed]:fields:_yarn_manifest_fields' \
            '--json[format the output as an NDJSON stream]' \
            '*:package' \
            && ret=0
          ;;
        (login)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-S --scope)'{-S,--scope}'[login to the registry configured for a given scope]:scope' \
            '--publish[login to the publish registry]' \
            '--always-auth[set the npmAlwaysAuth configuration]' \
            && ret=0
          ;;
        (logout)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-S --scope)'{-S,--scope}'[logout of the registry configured for a given scope]:scope' \
            '--publish[logout to the publish registry]' \
            '(-A --all)'{-A,--all}'[logout of all registries]' \
            && ret=0
          ;;
        (publish)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--access[access for the published package]:access:(public restricted)' \
            '--tag[tag on the registry that the package should be attached to]' \
            '--tolerate-republish[warn and exit when republishing an already existing version of a package]' \
            '--otp[OTP token to use with the command]:token' \
            '--provenance[generate provenance for the package]' \
            '(-n --dry-run)'{-n,--dry-run}'[show what would be published without actually publishing]' \
            '--json[output the result in JSON format]' \
            && ret=0
          ;;
        (tag)
          _yarn_npm_tag && ret=0
          ;;
        (whoami)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-S --scope)'{-S,--scope}'[print username for the registry configured for a given scope]:scope' \
            '--publish[print username for the publish registry]' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_npm_subcommands] )) ||
_yarn_npm_subcommands() {
  local -a commands=(
    'audit:perform a vulnerability audit against the installed packages'
    'info:show information about a package'
    'login:store new login info to access the npm registry'
    'logout:logout of the npm registry'
    'publish:publish the active workspace to the npm registry'
    'tag:tag operations'
    'whoami:display the name of the authenticated user'
  )
  _describe -t commands 'command' commands "$@"
}

(( $+functions[_yarn_npm_tag] )) ||
_yarn_npm_tag() {
  local ret=1

  _arguments -C \
    '1:subcmd:(add list remove)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (add|remove)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '1:package' \
            '2:tag' \
            && ret=0
          ;;
        (list)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            '1:package' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_plugin] )) ||
_yarn_plugin() {
  local ret=1

  _arguments -C \
    '(- *)'{-h,--help}'[show help message and exit]' \
    '1:subcmd:_yarn_plugin_subcommands' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (check|runtime|list)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            && ret=0
          ;;
        (import)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--checksum[whether to care if this plugin is modified]' \
            '*:package' \
            && ret=0;
          ;;
        (remove)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '1:name' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_plugin_subcommands] )) ||
_yarn_plugin_subcommands() {
  local -a commands=(
    'check:find all third-party plugins that differ from their own spec'
    'import:download plugin'
    'list:list the available official plugins'
    'remove:remove a plugin'
    'runtime:list the active plugins'
  )
  _describe -t commands 'command' commands "$@"
}

(( $+functions[_yarn_run] )) ||
_yarn_run() {
  local ret=1

  _arguments \
    '(- *)'{-h,--help}'[show help message and exit]' \
    '--json[format the output as an NDJSON stream]' \
    '--inspect[activate inspector]' \
    '--inspect-brk[activate inspector and break at start]' \
    '(-T --top-level)'{-T,--top-level}'[check the root workspace for scripts and/or binaries]' \
    '(-B --binaries-only)'{-B,--binaries-only}'[ignore any user defined scripts and only check binaries]' \
    '*--require[preload given module]:module' \
    '1: :_yarn_scripts' \
    '*:: :_files' \
    && ret=0

  return ret
}

(( $+functions[_yarn_set] )) ||
_yarn_set() {
  local ret=1

  _arguments -C \
    '(- *)'{-h,--help}'[show help message and exit]' \
    '1:subcmd:(resolution version)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (resolution)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            && ret=0
          ;;
        (version)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--yarn-path[set the yarnPath setting]:path:_files' \
            '--only-if-needed[only lock the Yarn version if it is not already locked]' \
            '1:version: _alternative "type\:type\:(latest berry stable canary classic self)" "file\:file\:_files"' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_version] )) ||
_yarn_version() {
  local ret=1

  _arguments -C -s \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '(-d --deferred)'{-d,--deferred}'[prepare the version to be bumped during the next release cycle]' \
    '(-i --immediate)'{-i,--immediate}'[bump the version immediately]' \
    '1:subcmd:(apply check)' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (apply)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--all[apply the deferred version changes on all workspaces]' \
            '--dry-run[print versions without actually generating the package archive]' \
            '--prerelease[add a prerelease identifier to new versions]' \
            '--exact[use the exact version of each package, removes any range]' \
            '(-R --recursive)'{-R,--recursive}'[release the transitive workspaces as well]' \
            '--json[format the output as an NDJSON stream]' \
            && ret=0
          ;;
        (check)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-i --interactive)'{-i,--interactive}'[open an interactive interface used to set version bumps]' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_workspace] )) ||
_yarn_workspace() {
  local package_json=$(_yarn_package_json_path)
  local package_dir=$(dirname $package_json)

  local -a workspaces=(${(@f)$(yarn workspaces list |sed 's/^.*: //')})
  local -a components=()
  for workspace in $workspaces[@]
  do
    if [[ "$workspace" != '.' && -d $workspace ]]; then
      components+=$(basename "$workspace")
    fi
  done

  _arguments -C -s \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '1:component:(($components))' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      local component="$words[1]"
      local workspace_dir=""
      for workspace in $workspaces[@]
      do
        local d=$(basename $workspace)
        if [[ "$d" == $component ]]; then
          workspace_dir=$workspace
        fi
      done

      local -a scripts=()
      if [[ -d $workspace_dir ]]; then
        if (( $+commands[jq] )); then
          scripts=($(builtin cd $workspace_dir; jq -r '.scripts | keys | .[]' package.json 2>/dev/null))
        elif (( $+commands[perl] )); then
          local package_json="$workspace_dir/package.json"
          scripts=($(cat "$package_json" | perl -0777 -MJSON::PP -n -E '$r=decode_json($_); do{($k=$_)=~s/:/\\:/g;say $k}for sort keys %{$r->{scripts}}' 2>/dev/null))
        fi
      fi

      _arguments \
        '1:scripts:(($scripts))' \
        '*:: :_files' \
        && ret=0
    ;;
  esac

  return ret
}

(( $+functions[_yarn_workspaces] )) ||
_yarn_workspaces() {
  local ret=1

  _arguments -C -s \
    '(- *)'{-h,--help}'[Print this usage information]' \
    '1:subcmd:_yarn_workspaces_subcommands' \
    '*:: :->arg' \
    && ret=0

  case $state in
    (arg)
      case $words[1] in
        (list)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--since[only include workspaces that have been changed since the specified ref]' \
            '(-R,--recursive)'{-R,--recursive}'[find packages via dependencies/devDependencies]' \
            '--no-private[exclude workspaces that have the private field set to true]' \
            '(-v --verbose)'{-v,--verbose}'[also return the cross-dependencies between workspaces]' \
            '--json[format the output as an NDJSON stream]' \
            && ret=0
          ;;
        (focus)
          _arguments \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '--json[format the output as an NDJSON stream]' \
            '--production[only install regular dependencies by ommiting dev dependencies]' \
            '(-A --all)'{-A,--all}'[install the entire project]' \
            && ret=0
          ;;
        (foreach)
          _arguments -s \
            '(- *)'{-h,--help}'[show help message and exit]' \
            '(-A --all)'{-A,--all}'[run the command on all workspaces of a project]' \
            '(-R --recursive)'{-R,--recursive}'[run the command current workspace and all of its recursive dependencies]' \
            '(-W --worktree)'{-W,--worktree}'[run the command on all workspaces of the current worktree]' \
            '(-v --verbose)'{-v,--verbose}'[increase level of logging verbosity up to 2 times]' \
            '(-p --parallel)'{-p,--parallel}'[run the commands in parallel]' \
            '(-i --interlaced)'{-i,--interlaced}'[print the output of commands in real-time instead of buffering]' \
            '(-j --jobs)'{-j,--jobs}'[maximum number of parallel tasks]:jobs:(unlimited)' \
            '(-t --topological)'{-t,--topological}'[run command after all workspaces it depends on(regular) have finished]' \
            '--topological-dev[run command after all workspaces it depends on(regular + dev) have finished]' \
            '*--include[patterns matching workspaces will be traversed]:pattern:_files' \
            '*--exclude[patterns matching workspace will not be traversed]:pattern:_files' \
            '--no-private[avoid running the command on private workspaces]' \
            '--from["from" glob as the starting point for any recursive search]:from' \
            '--since[only include workspaces that have been changed since the specified ref]' \
            '*: :_normal' \
            && ret=0
          ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_yarn_workspaces_subcommands] )) ||
_yarn_workspaces_subcommands() {
  local -a commands=(
    'focus:install a single workspace and its dependencies'
    'foreach:run a command on all workspaces'
    'list:list all available workspaces'
  )
  _describe -t commands 'command' commands "$@"
}

#
# Utilities
#

(( $+functions[_yarn_manifest_fields] )) ||
_yarn_manifest_fields() {
  local -a fields=(
    name version description keywords homepage bugs license
    author contributors funding files exports main browser
    bin man directories repository scripts config dependencies
    devDependencies peerDependencies peerDependenciesMeta bundleDependencies
    optionalDependencies overrides engines os cpu libc devEngines
    private publishConfig workspaces
  )

  _values -s , fields $fields
}

(( $+functions[_yarn_package_json_path] )) ||
_yarn_package_json_path() {
  local dir=$(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_scripts() {
  local -a binaries

  if (( $+commands[jq] )); then
    binaries=($(yarn bin --json 2>/dev/null | jq -r '.name' 2>/dev/null))
  elif (( $+commands[perl] )); then
    binaries=($(yarn bin 2>/dev/null | perl -wln -e 'm{^[^:]+:\s+([a-z]\S+)} and print $1' 2>/dev/null))
  fi

  local -a scripts
  local package_json=$(_yarn_package_json_path)

  if [[ -e "$package_json" ]]; then
    if (( $+commands[jq] )); then
      scripts=($(jq -r '.scripts | keys | .[]' "$package_json" 2>/dev/null))
    elif (( $+commands[perl] )); then
      scripts=("${(@f)$(cat ${package_json} | perl -0777 -MJSON::PP -n -E 'binmode(STDOUT, ":encoding(UTF-8)"); %r=%{decode_json($_)->{scripts}}; printf "$_\n" for sort keys %r' 2>/dev/null )}")
    fi
  fi

  local -a candidates=($scripts $binaries)
  _values 'scripts' $candidates
}

_yarn "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et