Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

readme.md 6.6KB

před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # update-notifier [![Build Status](https://travis-ci.org/yeoman/update-notifier.svg?branch=master)](https://travis-ci.org/yeoman/update-notifier)
  2. > Update notifications for your CLI app
  3. ![](screenshot.png)
  4. Inform users of your package of updates in a non-intrusive way.
  5. #### Contents
  6. - [Install](#install)
  7. - [Usage](#usage)
  8. - [How](#how)
  9. - [API](#api)
  10. - [About](#about)
  11. - [Users](#users)
  12. ## Install
  13. ```
  14. $ npm install update-notifier
  15. ```
  16. ## Usage
  17. ### Simple
  18. ```js
  19. const updateNotifier = require('update-notifier');
  20. const pkg = require('./package.json');
  21. updateNotifier({pkg}).notify();
  22. ```
  23. ### Comprehensive
  24. ```js
  25. const updateNotifier = require('update-notifier');
  26. const pkg = require('./package.json');
  27. // Checks for available update and returns an instance
  28. const notifier = updateNotifier({pkg});
  29. // Notify using the built-in convenience method
  30. notifier.notify();
  31. // `notifier.update` contains some useful info about the update
  32. console.log(notifier.update);
  33. /*
  34. {
  35. latest: '1.0.1',
  36. current: '1.0.0',
  37. type: 'patch', // Possible values: latest, major, minor, patch, prerelease, build
  38. name: 'pageres'
  39. }
  40. */
  41. ```
  42. ### Options and custom message
  43. ```js
  44. const notifier = updateNotifier({
  45. pkg,
  46. updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
  47. });
  48. if (notifier.update) {
  49. console.log(`Update available: ${notifier.update.latest}`);
  50. }
  51. ```
  52. ## How
  53. Whenever you initiate the update notifier and it's not within the interval threshold, it will asynchronously check with npm in the background for available updates, then persist the result. The next time the notifier is initiated, the result will be loaded into the `.update` property. This prevents any impact on your package startup performance.
  54. The update check is done in a unref'ed [child process](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options). This means that if you call `process.exit`, the check will still be performed in its own process.
  55. The first time the user runs your app, it will check for an update, and even if an update is available, it will wait the specified `updateCheckInterval` before notifying the user. This is done to not be annoying to the user, but might surprise you as an implementer if you're testing whether it works. Check out [`example.js`](example.js) to quickly test out `update-notifier` and see how you can test that it works in your app.
  56. ## API
  57. ### notifier = updateNotifier(options)
  58. Checks if there is an available update. Accepts options defined below. Returns an instance with an `.update` property if there is an available update, otherwise `undefined`.
  59. ### options
  60. Type: `object`
  61. #### pkg
  62. Type: `object`
  63. ##### name
  64. *Required*\
  65. Type: `string`
  66. ##### version
  67. *Required*\
  68. Type: `string`
  69. #### updateCheckInterval
  70. Type: `number`\
  71. Default: `1000 * 60 * 60 * 24` *(1 day)*
  72. How often to check for updates.
  73. #### shouldNotifyInNpmScript
  74. Type: `boolean`\
  75. Default: `false`
  76. Allows notification to be shown when running as an npm script.
  77. #### distTag
  78. Type: `string`\
  79. Default: `'latest'`
  80. Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version.
  81. ### notifier.fetchInfo()
  82. Check update information.
  83. Returns an `object` with:
  84. - `latest` _(String)_ - Latest version.
  85. - `current` _(String)_ - Current version.
  86. - `type` _(String)_ - Type of current update. Possible values: `latest`, `major`, `minor`, `patch`, `prerelease`, `build`.
  87. - `name` _(String)_ - Package name.
  88. ### notifier.notify(options?)
  89. Convenience method to display a notification message. *(See screenshot)*
  90. Only notifies if there is an update and the process is [TTY](https://nodejs.org/api/process.html#process_a_note_on_process_i_o).
  91. #### options
  92. Type: `object`
  93. ##### defer
  94. Type: `boolean`\
  95. Default: `true`
  96. Defer showing the notification to after the process has exited.
  97. ##### message
  98. Type: `string`\
  99. Default: [See above screenshot](https://github.com/yeoman/update-notifier#update-notifier-)
  100. Message that will be shown when an update is available.
  101. Available placeholders:
  102. - `{packageName}` - Package name.
  103. - `{currentVersion}` - Current version.
  104. - `{latestVersion}` - Latest version.
  105. - `{updateCommand}` - Update command.
  106. ```js
  107. notifier.notify({message: 'Run `{updateCommand}` to update.'});
  108. // Output:
  109. // Run `npm install update-notifier-tester@1.0.0` to update.
  110. ```
  111. ##### isGlobal
  112. Type: `boolean`\
  113. Default: Auto-detect
  114. Include the `-g` argument in the default message's `npm i` recommendation. You may want to change this if your CLI package can be installed as a dependency of another project, and don't want to recommend a global installation. This option is ignored if you supply your own `message` (see above).
  115. ##### boxenOptions
  116. Type: `object`\
  117. Default: `{padding: 1, margin: 1, align: 'center', borderColor: 'yellow', borderStyle: 'round'}` *(See screenshot)*
  118. Options object that will be passed to [`boxen`](https://github.com/sindresorhus/boxen).
  119. ### User settings
  120. Users of your module have the ability to opt-out of the update notifier by changing the `optOut` property to `true` in `~/.config/configstore/update-notifier-[your-module-name].json`. The path is available in `notifier.config.path`.
  121. Users can also opt-out by [setting the environment variable](https://github.com/sindresorhus/guides/blob/master/set-environment-variables.md) `NO_UPDATE_NOTIFIER` with any value or by using the `--no-update-notifier` flag on a per run basis.
  122. The check is also skipped automatically:
  123. - on CI
  124. - in unit tests (when the `NODE_ENV` environment variable is `test`)
  125. ## About
  126. The idea for this module came from the desire to apply the browser update strategy to CLI tools, where everyone is always on the latest version. We first tried automatic updating, which we discovered wasn't popular. This is the second iteration of that idea, but limited to just update notifications.
  127. ## Users
  128. There are a bunch projects using it:
  129. - [npm](https://github.com/npm/npm) - Package manager for JavaScript
  130. - [Yeoman](https://yeoman.io) - Modern workflows for modern webapps
  131. - [AVA](https://ava.li) - Simple concurrent test runner
  132. - [XO](https://github.com/xojs/xo) - JavaScript happiness style linter
  133. - [Node GH](https://github.com/node-gh/gh) - GitHub command line tool
  134. [And 2700+ more…](https://www.npmjs.org/browse/depended/update-notifier)
  135. ---
  136. <div align="center">
  137. <b>
  138. <a href="https://tidelift.com/subscription/pkg/npm-update_notifier?utm_source=npm-update-notifier&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  139. </b>
  140. <br>
  141. <sub>
  142. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  143. </sub>
  144. </div>