You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

2017-06-19-adding-command-line-aliases-for-yarn.md 7.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. ---
  2. layout : post
  3. title : "Adding Command Line Aliases for Yarn"
  4. author : G. Kay Lee
  5. author_url : "https://github.com/gsklee"
  6. date : 2017-06-19 00:00:00
  7. categories : announcements
  8. share_text : "Adding Command Line Aliases for Yarn"
  9. ---
  10. One of the core design philosophies of Yarn is to strive for simpleness; a lean CLI without redundant features. That’s why Yarn has resisted adding random built-in shorthands like `npm r` or an aliases system like the one you can find in Git. We believe that the benefits they could possibly bring to the Yarn experience are not justified by the cost required to build and maintain such a full-fledged subsystem.
  11. We’ve also noticed, however, that it is among one of the most common feature requests we received from the community. People do use aliases for several reasons, for example, to replicate their experiences from the `npm` command. The good news is, all modern shell environments actually support command aliases in one form or another, and we encourage you to improve your CLI experience using these ways that are baked into your favorite shell already.
  12. Let’s say you check for package distribution tags information pretty often, are a report message addict as well as an emoji hater, and you’d like to have a handy shorthand for this common task. Below we’ve compiled a list of ways to add the command alias of `yarn info --verbose --no-emoji <package> dist-tags` in a number of popular shells for your convenience:
  13. ## Bash & Zsh
  14. [Bash](<https://en.wikipedia.org/wiki/Bash_(Unix_shell)>) is the default shell on most Unix-like systems; together with [Zsh](https://en.wikipedia.org/wiki/Z_shell), they both descended from the earlier [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell) and hence syntaxes are largely compatible. To add a simple alias in either Bash or Zsh, simply have the following line added into your `.bashrc` or `.zshrc`, respectively:
  15. ```sh
  16. alias ynf="yarn info --verbose --no-emoji"
  17. ```
  18. Restart your shell and now you’ll be able to do:
  19. ```sh
  20. ynf react dist-tags
  21. ```
  22. ```
  23. yarn info v0.24.6
  24. verbose 0.261 Checking for configuration file "/Users/gsklee/.npmrc".
  25. verbose 0.262 Checking for configuration file "/Users/gsklee/.npmrc".
  26. verbose 0.262 Checking for configuration file "/Users/gsklee/.nvm/versions/node/v8.1.2/.npmrc".
  27. verbose 0.263 Checking for configuration file "/Users/gsklee/.npmrc".
  28. verbose 0.263 Checking for configuration file "/Users/.npmrc".
  29. verbose 0.265 Checking for configuration file "/Users/gsklee/.yarnrc".
  30. verbose 0.265 Found configuration file "/Users/gsklee/.yarnrc".
  31. verbose 0.267 Checking for configuration file "/Users/gsklee/.yarnrc".
  32. verbose 0.267 Found configuration file "/Users/gsklee/.yarnrc".
  33. verbose 0.268 Checking for configuration file "/Users/gsklee/.nvm/versions/node/v8.1.2/.yarnrc".
  34. verbose 0.268 Checking for configuration file "/Users/gsklee/.yarnrc".
  35. verbose 0.268 Found configuration file "/Users/gsklee/.yarnrc".
  36. verbose 0.27 Checking for configuration file "/Users/.yarnrc".
  37. verbose 0.274 current time: 2017-06-16T09:43:50.256Z
  38. verbose 0.339 Performing "GET" request to "https://registry.yarnpkg.com/react".
  39. verbose 0.488 Request "https://registry.yarnpkg.com/react" finished with status code 200.
  40. { latest: '15.6.1',
  41. '0.10.0-rc1': '0.10.0-rc1',
  42. '0.11.0-rc1': '0.11.0-rc1',
  43. next: '16.0.0-alpha.13',
  44. dev: '15.5.0-rc.2',
  45. '0.14-stable': '0.14.9',
  46. '15-next': '15.6.0-rc.1' }
  47. Done in 0.28s.
  48. ```
  49. Now, if you’d like to further alias the `dist-tags` part as well, you’ll need to use a function instead because Bash/Zsh aliases do not accept additional parameters:
  50. ```sh
  51. function ynftag { yarn info --verbose --no-emoji "$@" dist-tags; }
  52. ```
  53. You’ll then be able to get the same output by simply typing:
  54. ```sh
  55. ynftag react
  56. ```
  57. ## Fish
  58. [Fish](https://en.wikipedia.org/wiki/Friendly_interactive_shell) is a newer “[exotic shell](https://en.wikipedia.org/wiki/Unix_shell#Exotic_shells)” that deviates from traditional shell designs. It offers “abbreviations” that expand into full commands live as you type, much like the so-called snippets in modern code editors. To add an abbreviation:
  59. ```sh
  60. abbr --add ynf yarn info --verbose --no-emoji
  61. ```
  62. When it comes to passing in additional arguments, however, you have to use functions just like in Bash and Zsh:
  63. ```sh
  64. function ynftag --wraps yarn --description "yarn info --verbose --no-emoji <package> dist-tags"
  65. yarn info --verbose --no-emoji $argv dist-tags
  66. end
  67. ```
  68. To persist your alias command definition, save it to your autoload directory:
  69. ```sh
  70. funcsave ynftag
  71. ```
  72. ## Windows PowerShell
  73. [PowerShell](https://en.wikipedia.org/wiki/PowerShell) is the default shell in current version of Windows. Unlike Unix shells which are built upon text processing and piping, inputs and outputs in PowerShell are .NET objects; as such, aliases in PowerShell do not work as string substitutions, but rather pointers to existing functions. This means that you’ll need to use functions to define your aliases whether additional parameters are involved or not.
  74. Here is a [guidelines-abiding](https://msdn.microsoft.com/en-us/library/ms714428) example of the `ynftag` alias:
  75. ```sh
  76. function Get-NpmPackageDistributionTags { yarn info --verbose --no-emoji @Args dist-tags }
  77. New-Alias ynftag Get-NpmPackageDistributionTags
  78. ```
  79. Here's a yarn alias that re-adds the `ls` command to list packages:
  80. ```sh
  81. # yarn broke 'ls'
  82. # Scope private do we don't call yarn recursively!
  83. function Private:yarn() {
  84. $modifiedArgs = @()
  85. foreach ( $arg in $args ) {
  86. if ( $arg -cmatch '^ls$' ) {
  87. $arg = 'list'
  88. }
  89. $modifiedArgs += $arg
  90. }
  91. & yarn $modifiedArgs
  92. }
  93. ```
  94. Save the code above to one of the many [PowerShell profiles](https://blogs.technet.microsoft.com/heyscriptingguy/2013/01/04/understanding-and-using-powershell-profiles/) that suits you best to persist the definition.
  95. ## Command Prompt (`cmd`)
  96. If you’re still using the clunky Command Prompt, we believe that it’d be better for you, in the long run, to learn to use PowerShell instead. It’s more capable, modern, and everything is just way more consistent. Nonetheless, here is how you define an alias within the current Command Prompt instance:
  97. ```sh
  98. doskey ynftag=yarn info --verbose --no-emoji $* dist-tags
  99. ```
  100. Since Command Prompt doesn’t come with a `.bashrc` equivalent, in order to persist your aliases permanently, you’ll need to create a custom `cmdrc.cmd` file (could be any name, but we recommend you to stick with the long-standing naming convention) inside your home directory, with the following content:
  101. ```sh
  102. @echo off
  103. doskey ynftag=yarn info --verbose --no-emoji $* dist-tags
  104. ```
  105. Then modify your Command Prompt shortcut target to:
  106. ```sh
  107. # Replace `cmdrc.cmd` with the full path that leads to the file.
  108. cmd.exe /k cmdrc.cmd
  109. ```
  110. ## In Conclusion
  111. Yarn is a powerful JavaScript tool, but it’s also a tool that resides in your shell environment. By leveraging the innate capabilities of your shell, Yarn can do far more for you right now and right away.

js yarn包管理组件依赖分析

Contributors (1)