UNPKG

6.54 kBMarkdownView Raw
1# update-notifier [![Build Status](https://travis-ci.org/yeoman/update-notifier.svg?branch=master)](https://travis-ci.org/yeoman/update-notifier)
2
3> Update notifications for your CLI app
4
5![](screenshot.png)
6
7Inform users of your package of updates in a non-intrusive way.
8
9#### Contents
10
11- [Install](#install)
12- [Usage](#usage)
13- [How](#how)
14- [API](#api)
15- [About](#about)
16- [Users](#users)
17
18## Install
19
20```
21$ npm install update-notifier
22```
23
24## Usage
25
26### Simple
27
28```js
29const updateNotifier = require('update-notifier');
30const pkg = require('./package.json');
31
32updateNotifier({pkg}).notify();
33```
34
35### Comprehensive
36
37```js
38const updateNotifier = require('update-notifier');
39const pkg = require('./package.json');
40
41// Checks for available update and returns an instance
42const notifier = updateNotifier({pkg});
43
44// Notify using the built-in convenience method
45notifier.notify();
46
47// `notifier.update` contains some useful info about the update
48console.log(notifier.update);
49/*
50{
51 latest: '1.0.1',
52 current: '1.0.0',
53 type: 'patch', // Possible values: latest, major, minor, patch, prerelease, build
54 name: 'pageres'
55}
56*/
57```
58
59### Options and custom message
60
61```js
62const notifier = updateNotifier({
63 pkg,
64 updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
65});
66
67if (notifier.update) {
68 console.log(`Update available: ${notifier.update.latest}`);
69}
70```
71
72## How
73
74Whenever 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.
75The 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.
76
77The 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.
78
79## API
80
81### notifier = updateNotifier(options)
82
83Checks 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`.
84
85### options
86
87Type: `object`
88
89#### pkg
90
91Type: `object`
92
93##### name
94
95*Required*\
96Type: `string`
97
98##### version
99
100*Required*\
101Type: `string`
102
103#### updateCheckInterval
104
105Type: `number`\
106Default: `1000 * 60 * 60 * 24` *(1 day)*
107
108How often to check for updates.
109
110#### shouldNotifyInNpmScript
111
112Type: `boolean`\
113Default: `false`
114
115Allows notification to be shown when running as an npm script.
116
117#### distTag
118
119Type: `string`\
120Default: `'latest'`
121
122Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version.
123
124### notifier.fetchInfo()
125
126Check update information.
127
128Returns an `object` with:
129
130- `latest` _(String)_ - Latest version.
131- `current` _(String)_ - Current version.
132- `type` _(String)_ - Type of current update. Possible values: `latest`, `major`, `minor`, `patch`, `prerelease`, `build`.
133- `name` _(String)_ - Package name.
134
135### notifier.notify(options?)
136
137Convenience method to display a notification message. *(See screenshot)*
138
139Only notifies if there is an update and the process is [TTY](https://nodejs.org/api/process.html#process_a_note_on_process_i_o).
140
141#### options
142
143Type: `object`
144
145##### defer
146
147Type: `boolean`\
148Default: `true`
149
150Defer showing the notification to after the process has exited.
151
152##### message
153
154Type: `string`\
155Default: [See above screenshot](https://github.com/yeoman/update-notifier#update-notifier-)
156
157Message that will be shown when an update is available.
158
159##### isGlobal
160
161Type: `boolean`\
162Default: Auto-detect
163
164Include 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).
165
166##### boxenOptions
167
168Type: `object`\
169Default: `{padding: 1, margin: 1, align: 'center', borderColor: 'yellow', borderStyle: 'round'}` *(See screenshot)*
170
171Options object that will be passed to [`boxen`](https://github.com/sindresorhus/boxen).
172
173### User settings
174
175Users 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`.
176
177Users 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.
178
179The check is also skipped automatically:
180 - on CI
181 - in unit tests (when the `NODE_ENV` environment variable is `test`)
182
183## About
184
185The 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.
186
187## Users
188
189There are a bunch projects using it:
190
191- [npm](https://github.com/npm/npm) - Package manager for JavaScript
192- [Yeoman](http://yeoman.io) - Modern workflows for modern webapps
193- [AVA](https://ava.li) - Simple concurrent test runner
194- [XO](https://github.com/xojs/xo) - JavaScript happiness style linter
195- [Pageres](https://github.com/sindresorhus/pageres) - Capture website screenshots
196- [Node GH](http://nodegh.io) - GitHub command line tool
197
198[And 2700+ more…](https://www.npmjs.org/browse/depended/update-notifier)
199
200---
201
202<div align="center">
203 <b>
204 <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>
205 </b>
206 <br>
207 <sub>
208 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
209 </sub>
210</div>