Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/update-notifier
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5cd65771e2fab2567592ae217d3ce647679462af
Choose a base ref
...
head repository: sindresorhus/update-notifier
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 739ec6f31c455ed3c3c562f0126ed53098a8bd96
Choose a head ref

Commits on May 13, 2018

  1. Docs: isGlobal option does not default to true (#142)

    The value has been auto-detected since #114
    dideler authored and SBoudrias committed May 13, 2018
    Copy the full SHA
    d371834 View commit details

Commits on Sep 12, 2018

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8df01b3 View commit details

Commits on Mar 28, 2019

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0d49f51 View commit details
  2. Require Node.js 8

    sindresorhus committed Mar 28, 2019
    Copy the full SHA
    aafd8a0 View commit details

Commits on Apr 7, 2019

  1. Add failing test for #153 (#154)

    LitoMore authored and sindresorhus committed Apr 7, 2019
    Copy the full SHA
    14632e4 View commit details
  2. Add distTag option (#151)

    LitoMore authored and sindresorhus committed Apr 7, 2019
    Copy the full SHA
    c8faa84 View commit details

Commits on Apr 12, 2019

  1. Fix failing test (#155)

    LitoMore authored and SBoudrias committed Apr 12, 2019
    Copy the full SHA
    79e89ad View commit details

Commits on Apr 19, 2019

  1. Copy the full SHA
    5f06620 View commit details

Commits on May 10, 2019

  1. Copy the full SHA
    ad8ed1b View commit details
  2. 3.0.0

    SBoudrias committed May 10, 2019
    Copy the full SHA
    a7bb3ee View commit details

Commits on May 25, 2019

  1. Create funding.yml

    sindresorhus committed May 25, 2019
    Copy the full SHA
    72f83d1 View commit details

Commits on May 31, 2019

  1. Tidelift tasks

    sindresorhus committed May 31, 2019
    Copy the full SHA
    1712928 View commit details

Commits on Jul 2, 2019

  1. Copy the full SHA
    f9d168a View commit details
  2. Update URL to TTY (#163)

    ecmarsh authored and SBoudrias committed Jul 2, 2019
    Copy the full SHA
    a6d6b49 View commit details
  3. Update Travis matrix

    SBoudrias committed Jul 2, 2019
    Copy the full SHA
    f8b4e60 View commit details
  4. 3.0.1

    SBoudrias committed Jul 2, 2019
    Copy the full SHA
    592b025 View commit details

Commits on Jul 14, 2019

  1. Copy the full SHA
    bf73119 View commit details

Commits on Dec 12, 2019

  1. Disable when NODE_ENV is test (#173)

    ehmicky authored and sindresorhus committed Dec 12, 2019
    Copy the full SHA
    b1525e6 View commit details
  2. Update dependencies

    sindresorhus committed Dec 12, 2019
    Copy the full SHA
    ccaf686 View commit details
  3. Copy the full SHA
    bc1721a View commit details
  4. Copy the full SHA
    39682de View commit details
  5. Remove the callback option (#158)

    LitoMore authored and sindresorhus committed Dec 12, 2019
    Copy the full SHA
    fb5161c View commit details
  6. 4.0.0

    sindresorhus committed Dec 12, 2019
    Copy the full SHA
    adf7803 View commit details

Commits on Feb 14, 2020

  1. Add template support for the message option (#175)

    Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
    fisker and sindresorhus authored Feb 14, 2020
    Copy the full SHA
    adbeb6e View commit details
  2. Update dependencies

    sindresorhus committed Feb 14, 2020
    Copy the full SHA
    4062f12 View commit details
  3. 4.1.0

    sindresorhus committed Feb 14, 2020
    Copy the full SHA
    9d9f4ff View commit details
  4. Fix Travis

    sindresorhus committed Feb 14, 2020
    Copy the full SHA
    64c5236 View commit details

Commits on Apr 19, 2020

  1. Use HTTPS links

    sindresorhus committed Apr 19, 2020
    Copy the full SHA
    71a3f19 View commit details

Commits on Aug 16, 2020

  1. Copy the full SHA
    f42fc8f View commit details
  2. 4.1.1

    sindresorhus committed Aug 16, 2020
    Copy the full SHA
    c9d2166 View commit details

Commits on Sep 16, 2020

  1. Copy the full SHA
    132b7ce View commit details

Commits on Sep 29, 2020

  1. Copy the full SHA
    3dfe42d View commit details
  2. Require Node.js 10

    sindresorhus committed Sep 29, 2020
    Copy the full SHA
    5b440c2 View commit details
  3. 5.0.0

    sindresorhus committed Sep 29, 2020
    Copy the full SHA
    da7c464 View commit details

Commits on Nov 1, 2020

  1. Update dependencies

    sindresorhus committed Nov 1, 2020
    Copy the full SHA
    9e2b772 View commit details
  2. 5.0.1

    sindresorhus committed Nov 1, 2020
    Copy the full SHA
    f63b2eb View commit details

Commits on Jan 24, 2021

  1. Copy the full SHA
    d8fa601 View commit details
  2. Move to GitHub Actions

    sindresorhus committed Jan 24, 2021
    Copy the full SHA
    0a6027e View commit details

Commits on Jan 31, 2021

  1. Upgrade dependencies

    Fixes #203
    Fixes #202
    sindresorhus committed Jan 31, 2021
    Copy the full SHA
    73c391b View commit details
  2. 5.1.0

    sindresorhus committed Jan 31, 2021
    Copy the full SHA
    3fdb218 View commit details

Commits on Jun 23, 2022

  1. Require Node.js 14 and move to ESM

    Fixes #218
    Fixes #216
    Fixes #214
    sindresorhus committed Jun 23, 2022
    Copy the full SHA
    9183541 View commit details
  2. 6.0.0

    sindresorhus committed Jun 23, 2022
    Copy the full SHA
    311557e View commit details
  3. Update dependencies (#222)

    santi authored Jun 23, 2022
    Copy the full SHA
    3f7c9f3 View commit details
  4. 6.0.1

    sindresorhus committed Jun 23, 2022
    Copy the full SHA
    020613b View commit details

Commits on Jun 24, 2022

  1. Fix license

    sindresorhus committed Jun 24, 2022
    Copy the full SHA
    d152f85 View commit details
  2. 6.0.2

    sindresorhus committed Jun 24, 2022
    Copy the full SHA
    3b6b9b1 View commit details

Commits on Jul 19, 2022

  1. Copy the full SHA
    3046d0f View commit details

Commits on Oct 27, 2023

  1. Drop Yarn install commands in update message

    Yarn users should know how to translate from npm commands already as most packages document npm commands only. It’s also unfair to other package managers, like `pnpm` and `bun` that we don’t include them and I don’t want to maintain install commands for all of them.
    sindresorhus committed Oct 27, 2023
    Copy the full SHA
    a1d8d9d View commit details
  2. Require Node.js 18

    sindresorhus committed Oct 27, 2023
    Copy the full SHA
    431dac4 View commit details
  3. 7.0.0

    sindresorhus committed Oct 27, 2023
    Copy the full SHA
    12e11d6 View commit details
Showing with 492 additions and 343 deletions.
  1. +1 −2 .gitattributes
  2. +2 −0 .github/funding.yml
  3. +3 −0 .github/security.md
  4. +21 −0 .github/workflows/main.yml
  5. +0 −4 .travis.yml
  6. +13 −9 check.js
  7. +1 −1 contributing.md
  8. +5 −5 example.js
  9. +3 −151 index.js
  10. +62 −53 package.json
  11. +70 −52 readme.md
  12. +8 −9 test/fs-error.js
  13. +78 −22 test/notify.js
  14. +43 −35 test/update-notifier.js
  15. +182 −0 update-notifier.js
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 2 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: sindresorhus
tidelift: npm/update-notifier
3 changes: 3 additions & 0 deletions .github/security.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

22 changes: 13 additions & 9 deletions check.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
/* eslint-disable unicorn/no-process-exit */
'use strict';
let updateNotifier = require('.');
import process from 'node:process';
import UpdateNotifier from './update-notifier.js';

const options = JSON.parse(process.argv[2]);
const updateNotifier = new UpdateNotifier(JSON.parse(process.argv[2]));

updateNotifier = new updateNotifier.UpdateNotifier(options);
try {
// Exit process when offline
setTimeout(process.exit, 1000 * 30);

const update = await updateNotifier.fetchInfo();

updateNotifier.checkNpm().then(update => {
// Only update the last update check time on success
updateNotifier.config.set('lastUpdateCheck', Date.now());

if (update.type && update.type !== 'latest') {
updateNotifier.config.set('update', update);
}

// Call process exit explicitly to terminate the child process
// Otherwise the child process will run forever, according to the Node.js docs
// Call process exit explicitly to terminate the child process,
// otherwise the child process will run forever, according to the Node.js docs
process.exit();
}).catch(() => {
} catch (error) {
console.error(error);
process.exit(1);
});
}
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
See the [contributing docs](https://github.com/yeoman/yeoman/blob/master/contributing.md)
See the [contributing docs](https://github.com/yeoman/yeoman/blob/main/contributing.md)
10 changes: 5 additions & 5 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const updateNotifier = require('.');
import updateNotifier from './index.js';

// Run: $ node example

@@ -10,7 +9,8 @@ const updateNotifier = require('.');
updateNotifier({
pkg: {
name: 'public-ip',
version: '0.9.2'
version: '0.9.2',
},
updateCheckInterval: 0
}).notify();
updateCheckInterval: 0,
})
.notify();
154 changes: 3 additions & 151 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,155 +1,7 @@
'use strict';
const spawn = require('child_process').spawn;
const path = require('path');
const format = require('util').format;
const importLazy = require('import-lazy')(require);
import UpdateNotifier from './update-notifier.js';

const configstore = importLazy('configstore');
const chalk = importLazy('chalk');
const semverDiff = importLazy('semver-diff');
const latestVersion = importLazy('latest-version');
const isNpm = importLazy('is-npm');
const isInstalledGlobally = importLazy('is-installed-globally');
const boxen = importLazy('boxen');
const xdgBasedir = importLazy('xdg-basedir');
const isCi = importLazy('is-ci');
const ONE_DAY = 1000 * 60 * 60 * 24;

class UpdateNotifier {
constructor(options) {
options = options || {};
this.options = options;
options.pkg = options.pkg || {};

// Reduce pkg to the essential keys. with fallback to deprecated options
// TODO: Remove deprecated options at some point far into the future
options.pkg = {
name: options.pkg.name || options.packageName,
version: options.pkg.version || options.packageVersion
};

if (!options.pkg.name || !options.pkg.version) {
throw new Error('pkg.name and pkg.version required');
}

this.packageName = options.pkg.name;
this.packageVersion = options.pkg.version;
this.updateCheckInterval = typeof options.updateCheckInterval === 'number' ? options.updateCheckInterval : ONE_DAY;
this.hasCallback = typeof options.callback === 'function';
this.callback = options.callback || (() => {});
this.disabled = 'NO_UPDATE_NOTIFIER' in process.env ||
process.argv.indexOf('--no-update-notifier') !== -1 ||
isCi();
this.shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;

if (!this.disabled && !this.hasCallback) {
try {
const ConfigStore = configstore();
this.config = new ConfigStore(`update-notifier-${this.packageName}`, {
optOut: false,
// Init with the current time so the first check is only
// after the set interval, so not to bother users right away
lastUpdateCheck: Date.now()
});
} catch (err) {
// Expecting error code EACCES or EPERM
const msg =
chalk().yellow(format(' %s update check failed ', options.pkg.name)) +
format('\n Try running with %s or get access ', chalk().cyan('sudo')) +
'\n to the local update config store via \n' +
chalk().cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgBasedir().config));

process.on('exit', () => {
console.error('\n' + boxen()(msg, {align: 'center'}));
});
}
}
}
check() {
if (this.hasCallback) {
this.checkNpm()
.then(update => this.callback(null, update))
.catch(err => this.callback(err));
return;
}

if (
!this.config ||
this.config.get('optOut') ||
this.disabled
) {
return;
}

this.update = this.config.get('update');

if (this.update) {
this.config.delete('update');
}

// Only check for updates on a set interval
if (Date.now() - this.config.get('lastUpdateCheck') < this.updateCheckInterval) {
return;
}

// Spawn a detached process, passing the options as an environment property
spawn(process.execPath, [path.join(__dirname, 'check.js'), JSON.stringify(this.options)], {
detached: true,
stdio: 'ignore'
}).unref();
}
checkNpm() {
return latestVersion()(this.packageName).then(latestVersion => {
return {
latest: latestVersion,
current: this.packageVersion,
type: semverDiff()(this.packageVersion, latestVersion) || 'latest',
name: this.packageName
};
});
}
notify(opts) {
const suppressForNpm = !this.shouldNotifyInNpmScript && isNpm();
if (!process.stdout.isTTY || suppressForNpm || !this.update) {
return this;
}

opts = Object.assign({isGlobal: isInstalledGlobally()}, opts);

opts.message = opts.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') +
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (opts.isGlobal ? '-g ' : '') + this.packageName) + ' to update';

opts.boxenOpts = opts.boxenOpts || {
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round'
};

const message = '\n' + boxen()(opts.message, opts.boxenOpts);

if (opts.defer === false) {
console.error(message);
} else {
process.on('exit', () => {
console.error(message);
});

process.on('SIGINT', () => {
console.error('');
process.exit();
});
}

return this;
}
}

module.exports = options => {
export default function updateNotifier(options) {
const updateNotifier = new UpdateNotifier(options);
updateNotifier.check();
return updateNotifier;
};

module.exports.UpdateNotifier = UpdateNotifier;
}
115 changes: 62 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
{
"name": "update-notifier",
"version": "2.5.0",
"description": "Update notifications for your CLI app",
"license": "BSD-2-Clause",
"repository": "yeoman/update-notifier",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava --timeout=20s"
},
"files": [
"index.js",
"check.js"
],
"keywords": [
"npm",
"update",
"updater",
"notify",
"notifier",
"check",
"checker",
"cli",
"module",
"package",
"version"
],
"dependencies": {
"boxen": "^1.2.1",
"chalk": "^2.0.1",
"configstore": "^3.0.0",
"import-lazy": "^2.1.0",
"is-ci": "^1.0.10",
"is-installed-globally": "^0.1.0",
"is-npm": "^1.0.0",
"latest-version": "^3.0.0",
"semver-diff": "^2.0.0",
"xdg-basedir": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"clear-module": "^2.1.0",
"fixture-stdout": "^0.2.1",
"mock-require": "^2.0.2",
"strip-ansi": "^4.0.0",
"xo": "^0.18.2"
}
"name": "update-notifier",
"version": "7.3.0",
"description": "Update notifications for your CLI app",
"license": "BSD-2-Clause",
"repository": "yeoman/update-notifier",
"funding": "https://github.com/yeoman/update-notifier?sponsor=1",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"sideEffects": false,
"engines": {
"node": ">=18"
},
"scripts": {
"test": "xo && NODE_OPTIONS='--loader=esmock --no-warnings' ava"
},
"files": [
"index.js",
"update-notifier.js",
"check.js"
],
"keywords": [
"npm",
"update",
"updater",
"notify",
"notifier",
"check",
"checker",
"cli",
"module",
"package",
"version"
],
"dependencies": {
"boxen": "^8.0.0",
"chalk": "^5.3.0",
"configstore": "^7.0.0",
"is-in-ci": "^1.0.0",
"is-installed-globally": "^1.0.0",
"is-npm": "^6.0.0",
"latest-version": "^9.0.0",
"pupa": "^3.1.0",
"semver": "^7.6.3",
"xdg-basedir": "^5.1.0"
},
"devDependencies": {
"ava": "^6.1.3",
"clear-module": "^4.1.2",
"esmock": "^2.6.7",
"fixture-stdout": "^0.2.1",
"strip-ansi": "^7.1.0",
"xo": "^0.59.2"
},
"ava": {
"timeout": "20s",
"serial": true
}
}
122 changes: 70 additions & 52 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# update-notifier [![Build Status](https://travis-ci.org/yeoman/update-notifier.svg?branch=master)](https://travis-ci.org/yeoman/update-notifier)
# update-notifier

> Update notifications for your CLI app
@@ -15,33 +15,31 @@ Inform users of your package of updates in a non-intrusive way.
- [About](#about)
- [Users](#users)


## Install

```
$ npm install update-notifier
```sh
npm install update-notifier
```


## Usage

### Simple

```js
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
import updateNotifier from 'update-notifier';
import packageJson from './package.json' assert {type: 'json'};

updateNotifier({pkg}).notify();
updateNotifier({pkg: packageJson}).notify();
```

### Comprehensive

```js
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
import updateNotifier from 'update-notifier';
import packageJson from './package.json' assert {type: 'json'};

// Checks for available update and returns an instance
const notifier = updateNotifier({pkg});
const notifier = updateNotifier({pkg: packageJson});

// Notify using the built-in convenience method
notifier.notify();
@@ -71,123 +69,143 @@ if (notifier.update) {
}
```


## How

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.
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.

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.


## API

### notifier = updateNotifier(options)

Checks if there is an available update. Accepts options defined below. Returns an instance with an `.update` property there is an available update, otherwise `undefined`.
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`.

### options

Type: `object`

#### pkg

Type: `Object`
Type: `object`

##### name

*Required*<br>
*Required*\
Type: `string`

##### version

*Required*<br>
*Required*\
Type: `string`

#### updateCheckInterval

Type: `number`<br>
Type: `number`\
Default: `1000 * 60 * 60 * 24` *(1 day)*

How often to check for updates.

#### callback(error, update)
#### shouldNotifyInNpmScript

Type: `boolean`\
Default: `false`

Allows notification to be shown when running as an npm script.

#### distTag

Type: `string`\
Default: `'latest'`

Type: `Function`
Which [dist-tag](https://docs.npmjs.com/adding-dist-tags-to-packages) to use to find the latest version.

Passing a callback here will make it check for an update directly and report right away. Not recommended as you won't get the benefits explained in [`How`](#how). `update` is equal to `notifier.update`.
### notifier.fetchInfo()

### notifier.notify([options])
Check update information.

Returns an `object` with:

- `latest` *(string)* - Latest version.
- `current` *(string)* - Current version.
- `type` *(string)* - Type of current update. Possible values: `latest`, `major`, `minor`, `patch`, `prerelease`, `build`.
- `name` *(string)* - Package name.

### notifier.notify(options?)

Convenience method to display a notification message. *(See screenshot)*

Only notifies if there is an update and the process is [TTY](https://nodejs.org/api/process.html#process_tty_terminals_and_process_stdout).
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).

#### options

Type: `Object`
Type: `object`

##### defer

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Defer showing the notification to after the process has exited.

##### message

Type: `string`<br>
Type: `string`\
Default: [See above screenshot](https://github.com/yeoman/update-notifier#update-notifier-)

Message that will be shown when an update is available.

##### isGlobal
Available placeholders:

Type: `boolean`<br>
Default: `true`
- `{packageName}` - Package name.
- `{currentVersion}` - Current version.
- `{latestVersion}` - Latest version.
- `{updateCommand}` - Update command.

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).
```js
notifier.notify({message: 'Run `{updateCommand}` to update.'});

##### boxenOpts
// Output:
// Run `npm install update-notifier-tester@1.0.0` to update.
```

Type: `Object`<br>
Default: `{padding: 1, margin: 1, align: 'center', borderColor: 'yellow', borderStyle: 'round'}` *(See screenshot)*
##### isGlobal

Options object that will be passed to [`boxen`](https://github.com/sindresorhus/boxen).
Type: `boolean`\
Default: Auto-detect

##### shouldNotifyInNpmScript
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).

Type: `boolean`<br>
Default: `false`
##### boxenOptions

Allows notification to be shown when running as an npm script.
Type: `object`\
Default: `{padding: 1, margin: 1, textAlignment: 'center', borderColor: 'yellow', borderStyle: 'round'}` *(See screenshot)*

Options object that will be passed to [`boxen`](https://github.com/sindresorhus/boxen).

### User settings

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`.

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.

The check is also skipped on CI automatically.
Users can also opt-out by [setting the environment variable](https://github.com/sindresorhus/guides/blob/main/set-environment-variables.md) `NO_UPDATE_NOTIFIER` with any value or by using the `--no-update-notifier` flag on a per run basis.

The check is also skipped automatically:
- [in CI](https://github.com/sindresorhus/is-in-ci)
- in unit tests (when the `NODE_ENV` environment variable is `test`)

## About

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.


## Users

There are a bunch projects using it:

- [npm](https://github.com/npm/npm) - Package manager for JavaScript
- [Yeoman](http://yeoman.io) - Modern workflows for modern webapps
- [AVA](https://ava.li) - Simple concurrent test runner
- [Yeoman](https://yeoman.io) - Modern workflows for modern webapps
- [AVA](https://avajs.dev) - Simple concurrent test runner
- [XO](https://github.com/xojs/xo) - JavaScript happiness style linter
- [Pageres](https://github.com/sindresorhus/pageres) - Capture website screenshots
- [Node GH](http://nodegh.io) - GitHub command line tool

[And 1600+ more…](https://www.npmjs.org/browse/depended/update-notifier)


## License
- [Node GH](https://github.com/node-gh/gh) - GitHub command line tool

BSD-2-Clause © Google
[And 2700+ more…](https://www.npmjs.org/browse/depended/update-notifier)
17 changes: 8 additions & 9 deletions test/fs-error.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import process from 'node:process';
import clearModule from 'clear-module';
import test from 'ava';

let updateNotifier;
for (const name of ['..', 'configstore', 'xdg-basedir']) {
clearModule(name);
}

test.before(() => {
['.', 'configstore', 'xdg-basedir'].forEach(clearModule);
// Set configstore.config to something
// that requires root access
process.env.XDG_CONFIG_HOME = '/usr';
updateNotifier = require('..');
});
// Set configstore.config to something that requires root access
process.env.XDG_CONFIG_HOME = '/usr';
const {default: updateNotifier} = await import('../index.js');

test('fail gracefully', t => {
t.notThrows(() => {
updateNotifier({
packageName: 'npme',
packageVersion: '3.7.0'
packageVersion: '3.7.0',
});
});
});
100 changes: 78 additions & 22 deletions test/notify.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
import util from 'util';
import clearModule from 'clear-module';
import process from 'node:process';
import {inherits} from 'node:util';
import FixtureStdout from 'fixture-stdout';
import stripAnsi from 'strip-ansi';
import test from 'ava';
import mock from 'mock-require';
import esmock from 'esmock';

const stderr = new FixtureStdout({
stream: process.stderr
stream: process.stderr,
});

function Control(shouldNotifyInNpmScript) {
this.packageName = 'update-notifier-tester';
this._packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
latest: '1.0.0',
};
this.shouldNotifyInNpmScript = shouldNotifyInNpmScript;
this._shouldNotifyInNpmScript = shouldNotifyInNpmScript;
}

const setupTest = isNpmReturnValue => {
['.', 'is-npm'].forEach(clearModule);
const setupTest = async isNpmReturnValue => {
process.stdout.isTTY = true;
mock('is-npm', isNpmReturnValue || false);
const updateNotifier = require('..');
util.inherits(Control, updateNotifier.UpdateNotifier);

const UpdateNotifier = await esmock('../update-notifier.js', {
'is-npm': {isNpmOrYarn: isNpmReturnValue || false},
});

inherits(Control, UpdateNotifier);
};

let errorLogs = '';

test.beforeEach(() => {
setupTest();
test.beforeEach(async () => {
await setupTest();

stderr.capture(s => {
errorLogs += s;
return false;
});
});

test.afterEach(() => {
mock.stopAll();
stderr.release();
errorLogs = '';
});
@@ -46,8 +48,9 @@ test('use pretty boxen message by default', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: true});

t.is(stripAnsi(errorLogs), `
console.log('d', errorLogs);

t.is(stripAnsi(errorLogs), `
╭───────────────────────────────────────────────────╮
│ │
│ Update available 0.0.2 → 1.0.0 │
@@ -58,6 +61,43 @@ test('use pretty boxen message by default', t => {
`);
});

test('supports custom message', t => {
const notifier = new Control();
notifier.notify({
defer: false,
isGlobal: true,
message: 'custom message',
});

t.true(stripAnsi(errorLogs).includes('custom message'));
});

test('supports message with placeholders', t => {
const notifier = new Control();
notifier.notify({
defer: false,
isGlobal: true,
message: [
'Package Name: {packageName}',
'Current Version: {currentVersion}',
'Latest Version: {latestVersion}',
'Update Command: {updateCommand}',
].join('\n'),
});

t.is(stripAnsi(errorLogs), `
╭─────────────────────────────────────────────────────╮
│ │
│ Package Name: update-notifier-tester │
│ Current Version: 0.0.2 │
│ Latest Version: 1.0.0 │
│ Update Command: npm i -g update-notifier-tester │
│ │
╰─────────────────────────────────────────────────────╯
`);
});

test('exclude -g argument when `isGlobal` option is `false`', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: false});
@@ -70,16 +110,32 @@ test('shouldNotifyInNpmScript should default to false', t => {
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
});

test('suppress output when running as npm script', t => {
setupTest(true);
test('suppress output when running as npm script', async t => {
await setupTest(true);
const notifier = new Control();
notifier.notify({defer: false});
t.is(stripAnsi(errorLogs).indexOf('Update available'), -1);
t.false(stripAnsi(errorLogs).includes('Update available'));
});

test('should ouput if running as npm script and shouldNotifyInNpmScript option set', t => {
setupTest(true);
test('should output if running as npm script and shouldNotifyInNpmScript option set', async t => {
await setupTest(true);
const notifier = new Control(true);
notifier.notify({defer: false});
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
t.true(stripAnsi(errorLogs).includes('Update available'));
});

test('should not output if current version is the latest', async t => {
await setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.0';
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});

test('should not output if current version is more recent than the reported latest', async t => {
await setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.1';
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});
78 changes: 43 additions & 35 deletions test/update-notifier.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
import fs from 'fs';
import process from 'node:process';
import fs from 'node:fs';
import test from 'ava';
import mockRequire from 'mock-require';
import esmock from 'esmock';

mockRequire('is-ci', false);

// eslint-disable-next-line import/first
import updateNotifier from '..';

const generateSettings = options => {
options = options || {};
return {
pkg: {
name: 'update-notifier-tester',
version: '0.0.2'
},
callback: options.callback || null
};
};
const generateSettings = (options = {}) => ({
pkg: {
name: 'update-notifier-tester',
version: '0.0.2',
},
distTag: options.distTag,
});

let argv;
let configstorePath;

test.beforeEach(() => {
argv = process.argv.slice();
configstorePath = updateNotifier(generateSettings()).config.path;
// Prevents NODE_ENV 'test' default behavior which disables `update-notifier`
process.env.NODE_ENV = 'ava-test';

argv = [...process.argv];
});

test.afterEach(() => {
delete process.env.NO_UPDATE_NOTIFIER;
process.argv = argv;

setTimeout(() => {
fs.unlinkSync(configstorePath);
}, 10000);
try {
fs.unlinkSync(configstorePath);
} catch {}
}, 10_000);
});

test('check for update', async t => {
const update = await updateNotifier(generateSettings()).checkNpm();
t.is(update.current, '0.0.2');
test('fetch info', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-in-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
const update = await updateNotifier(generateSettings()).fetchInfo();
console.log(update);
t.is(update.latest, '0.0.2');
});

test.cb('check for update with callback', t => {
t.plan(1);

updateNotifier(generateSettings({
callback: () => {
t.pass();
t.end();
}
}));
test('fetch info with dist-tag', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-in-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
const update = await updateNotifier(generateSettings({distTag: '0.0.3-rc1'})).fetchInfo();
t.is(update.latest, '0.0.3-rc1');
});

test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', t => {
test('don\'t initialize configStore when NO_UPDATE_NOTIFIER is set', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-in-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
process.env.NO_UPDATE_NOTIFIER = '1';
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

test('don\'t initialize configStore when --no-update-notifier is set', t => {
test('don\'t initialize configStore when --no-update-notifier is set', async t => {
const updateNotifier = await esmock('../index.js', undefined, {'is-in-ci': false});
configstorePath = updateNotifier(generateSettings()).config.path;
process.argv.push('--no-update-notifier');
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

test('don\'t initialize configStore when NODE_ENV === "test"', async t => {
process.env.NODE_ENV = 'test';
const updateNotifier = await esmock('../index.js', undefined, {'is-in-ci': false});
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});
182 changes: 182 additions & 0 deletions update-notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import process from 'node:process';
import {spawn} from 'node:child_process';
import {fileURLToPath} from 'node:url';
import path from 'node:path';
import {format} from 'node:util';
import ConfigStore from 'configstore';
import chalk from 'chalk';
// Only import what we need for performance
import semverDiff from 'semver/functions/diff.js';
import semverGt from 'semver/functions/gt.js';
import latestVersion from 'latest-version';
import {isNpmOrYarn} from 'is-npm';
import isInstalledGlobally from 'is-installed-globally';
import boxen from 'boxen';
import {xdgConfig} from 'xdg-basedir';
import isInCi from 'is-in-ci';
import pupa from 'pupa';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const ONE_DAY = 1000 * 60 * 60 * 24;

export default class UpdateNotifier {
// Public
config;
update;

// Semi-private (used for tests)
_packageName; // eslint-disable-line lines-between-class-members
_shouldNotifyInNpmScript;

#options; // eslint-disable-line lines-between-class-members
#packageVersion;
#updateCheckInterval;
#isDisabled;

constructor(options = {}) {
this.#options = options;
options.pkg ??= {};
options.distTag ??= 'latest';

// Reduce pkg to the essential keys. with fallback to deprecated options
// TODO: Remove deprecated options at some point far into the future
options.pkg = {
name: options.pkg.name ?? options.packageName,
version: options.pkg.version ?? options.packageVersion,
};

if (!options.pkg.name || !options.pkg.version) {
throw new Error('pkg.name and pkg.version required');
}

this._packageName = options.pkg.name;
this.#packageVersion = options.pkg.version;
this.#updateCheckInterval = typeof options.updateCheckInterval === 'number' ? options.updateCheckInterval : ONE_DAY;
this.#isDisabled = 'NO_UPDATE_NOTIFIER' in process.env
|| process.env.NODE_ENV === 'test'
|| process.argv.includes('--no-update-notifier')
|| isInCi;
this._shouldNotifyInNpmScript = options.shouldNotifyInNpmScript;

if (!this.#isDisabled) {
try {
this.config = new ConfigStore(`update-notifier-${this._packageName}`, {
optOut: false,
// Init with the current time so the first check is only
// after the set interval, so not to bother users right away
lastUpdateCheck: Date.now(),
});
} catch {
// Expecting error code EACCES or EPERM
const message
= chalk.yellow(format(' %s update check failed ', options.pkg.name))
+ format('\n Try running with %s or get access ', chalk.cyan('sudo'))
+ '\n to the local update config store via \n'
+ chalk.cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgConfig));

process.on('exit', () => {
console.error(boxen(message, {textAlignment: 'center'}));
});
}
}
}

check() {
if (
!this.config
|| this.config.get('optOut')
|| this.#isDisabled
) {
return;
}

this.update = this.config.get('update');

if (this.update) {
// Use the real latest version instead of the cached one
this.update.current = this.#packageVersion;

// Clear cached information
this.config.delete('update');
}

// Only check for updates on a set interval
if (Date.now() - this.config.get('lastUpdateCheck') < this.#updateCheckInterval) {
return;
}

// Spawn a detached process, passing the options as an environment property
spawn(process.execPath, [path.join(__dirname, 'check.js'), JSON.stringify(this.#options)], {
detached: true,
stdio: 'ignore',
}).unref();
}

async fetchInfo() {
const {distTag} = this.#options;
const latest = await latestVersion(this._packageName, {version: distTag});

return {
latest,
current: this.#packageVersion,
type: semverDiff(this.#packageVersion, latest) ?? distTag,
name: this._packageName,
};
}

notify(options) {
const suppressForNpm = !this._shouldNotifyInNpmScript && isNpmOrYarn;
if (!process.stdout.isTTY || suppressForNpm || !this.update || !semverGt(this.update.latest, this.update.current)) {
return this;
}

options = {
isGlobal: isInstalledGlobally,
...options,
};

const installCommand = options.isGlobal ? `npm i -g ${this._packageName}` : `npm i ${this._packageName}`;

const defaultTemplate = 'Update available '
+ chalk.dim('{currentVersion}')
+ chalk.reset(' → ')
+ chalk.green('{latestVersion}')
+ ' \nRun ' + chalk.cyan('{updateCommand}') + ' to update';

const template = options.message || defaultTemplate;

options.boxenOptions ??= {
padding: 1,
margin: 1,
textAlignment: 'center',
borderColor: 'yellow',
borderStyle: 'round',
};

const message = boxen(
pupa(template, {
packageName: this._packageName,
currentVersion: this.update.current,
latestVersion: this.update.latest,
updateCommand: installCommand,
}),
options.boxenOptions,
);

if (options.defer === false) {
console.error(message);
} else {
process.on('exit', () => {
console.error(message);
});

process.on('SIGINT', () => {
console.error('');
process.exit();
});
}

return this;
}
}