_yarn 17 KB

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