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: e36671d06aaffa2d5a3295f349635d4e781eaa3d
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: 311557ec77dbb35ea130530639b0da7bbb254897
Choose a head ref

Commits on Feb 10, 2017

  1. Copy the full SHA
    01fcd25 View commit details

Commits on Feb 13, 2017

  1. Update dependencies

    sindresorhus committed Feb 13, 2017
    Copy the full SHA
    f1de1bf View commit details
  2. Improve readme

    sindresorhus committed Feb 13, 2017
    Copy the full SHA
    ac47d69 View commit details
  3. ES2015ify

    sindresorhus committed Feb 13, 2017
    1
    Copy the full SHA
    fda96b6 View commit details
  4. 2.0.0

    sindresorhus committed Feb 13, 2017
    Copy the full SHA
    d295cf3 View commit details

Commits on Feb 18, 2017

  1. Copy the full SHA
    607d3c9 View commit details
  2. 2.1.0

    sindresorhus committed Feb 18, 2017
    Copy the full SHA
    86ca238 View commit details

Commits on Mar 16, 2017

  1. Add npm as »reference« (#108)

    mischah authored and sindresorhus committed Mar 16, 2017
    Copy the full SHA
    e2f9233 View commit details

Commits on May 21, 2017

  1. Meta tweaks

    sindresorhus committed May 21, 2017
    Copy the full SHA
    1055d57 View commit details

Commits on Jun 6, 2017

  1. Bail if notify is disabled (#110)

    Avoids initializing configStore and related errors if environment variable or argv disables notifier.
    wejendorp authored and SBoudrias committed Jun 6, 2017
    Copy the full SHA
    d032e12 View commit details
  2. 2.2.0

    SBoudrias committed Jun 6, 2017
    Copy the full SHA
    adf7af0 View commit details

Commits on Jul 12, 2017

  1. Update chalk to 2.0.1 (#117)

    develar authored and sindresorhus committed Jul 12, 2017
    Copy the full SHA
    4bfb8b4 View commit details

Commits on Sep 29, 2017

  1. Move to AVA for testing (#121)

    timdeschryver authored and sindresorhus committed Sep 29, 2017
    Copy the full SHA
    6f2b074 View commit details
  2. Meta tweaks

    sindresorhus committed Sep 29, 2017
    Copy the full SHA
    6008ccf View commit details

Commits on Oct 9, 2017

  1. Copy the full SHA
    97d0b97 View commit details
  2. Copy the full SHA
    70b8248 View commit details
  3. Force bump boxen dependency

    Fixes #125
    sindresorhus committed Oct 9, 2017
    Copy the full SHA
    3eaa793 View commit details
  4. 2.3.0

    sindresorhus committed Oct 9, 2017
    Copy the full SHA
    492c21e View commit details

Commits on Oct 23, 2017

  1. Fix typo in README (#128)

    meain authored and SBoudrias committed Oct 23, 2017
    Copy the full SHA
    3e5cea0 View commit details

Commits on Oct 30, 2017

  1. Disable on CI (#116)

    SimenB authored and sindresorhus committed Oct 30, 2017
    Copy the full SHA
    38d5679 View commit details

Commits on Feb 14, 2018

  1. Update URL to XO

    sindresorhus committed Feb 14, 2018
    Copy the full SHA
    0ad8e5b View commit details

Commits on Mar 13, 2018

  1. Fix URI Scheme in package.json (#136)

    willnode authored and SBoudrias committed Mar 13, 2018
    Copy the full SHA
    accc884 View commit details

Commits on Mar 25, 2018

  1. Add license file

    sindresorhus committed Mar 25, 2018
    Copy the full SHA
    83c4daa View commit details
  2. 2.4.0

    sindresorhus committed Mar 25, 2018
    Copy the full SHA
    edbe3d2 View commit details

Commits on Apr 14, 2018

  1. Add ability to bypass isNpm check with shouldNotifyInNpmScript opti…

    …on (#127)
    
    * Added ability to bypass isNpm with 'shouldNotifyInNpmScript' option
    
    * Updated readme with option
    
    * Fixed grammatical error in readme
    
    * Rename skipIsNpmCheck to shouldNotifyInNpmScript
    
    * Refactored test to use renamed shouldNotifyInNpmScript property
    alexccl authored and SBoudrias committed Apr 14, 2018
    1
    Copy the full SHA
    ac0d3cb View commit details
  2. 2.5.0

    SBoudrias committed Apr 14, 2018
    Copy the full SHA
    5cd6577 View commit details

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. Copy the full SHA
    8df01b3 View commit details

Commits on Mar 28, 2019

  1. 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
Showing with 677 additions and 416 deletions.
  1. +1 −1 .editorconfig
  2. +1 −1 .gitattributes
  3. +2 −0 .github/funding.yml
  4. +3 −0 .github/security.md
  5. +22 −0 .github/workflows/main.yml
  6. +2 −1 .gitignore
  7. +1 −0 .npmrc
  8. +0 −7 .travis.yml
  9. +14 −10 check.js
  10. +1 −1 contributing.md
  11. +10 −6 example.js
  12. +4 −143 index.js
  13. +9 −0 license
  14. +65 −51 package.json
  15. +119 −58 readme.md
  16. +0 −137 test.js
  17. +20 −0 test/fs-error.js
  18. +147 −0 test/notify.js
  19. +63 −0 test/update-notifier.js
  20. +193 −0 update-notifier.js
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* 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.
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

24 changes: 14 additions & 10 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';
var updateNotifier = require('./');
import process from 'node:process';
import UpdateNotifier from './update-notifier.js';

var 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);

updateNotifier.checkNpm().then(function (update) {
// only update the last update check time on success
const update = await updateNotifier.fetchInfo();

// 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 nodejs 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(function () {
} 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)
16 changes: 10 additions & 6 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict';
var updateNotifier = require('./');
import updateNotifier from './index.js';

// you have to run this file two times the first time
// Run: $ node example

// You have to run this file two times the first time
// This is because it never reports updates on the first run
// If you want to test your own usage, ensure you set an older version

updateNotifier({
pkg: {
name: 'public-ip',
version: '0.9.2'
version: '0.9.2',
},
updateCheckInterval: 0
}).notify();
updateCheckInterval: 0,
})
.notify();
147 changes: 4 additions & 143 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,146 +1,7 @@
'use strict';
var spawn = require('child_process').spawn;
var path = require('path');
var format = require('util').format;
var lazyRequire = require('lazy-req')(require);
import UpdateNotifier from './update-notifier.js';

var configstore = lazyRequire('configstore');
var chalk = lazyRequire('chalk');
var semverDiff = lazyRequire('semver-diff');
var latestVersion = lazyRequire('latest-version');
var isNpm = lazyRequire('is-npm');
var boxen = lazyRequire('boxen');
var xdgBasedir = lazyRequire('xdg-basedir');
var ONE_DAY = 1000 * 60 * 60 * 24;

function UpdateNotifier(options) {
this.options = 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 || function () {};

if (!this.hasCallback) {
try {
var 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
var 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', function () {
console.error('\n' + boxen()(msg, {align: 'center'}));
});
}
}
}

UpdateNotifier.prototype.check = function () {
if (this.hasCallback) {
this.checkNpm().then(this.callback.bind(this, null)).catch(this.callback);
return;
}
if (
!this.config ||
this.config.get('optOut') ||
'NO_UPDATE_NOTIFIER' in process.env ||
process.argv.indexOf('--no-update-notifier') !== -1
) {
return;
}

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

if (this.update) {
this.config.del('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();
};

UpdateNotifier.prototype.checkNpm = function () {
return latestVersion()(this.packageName).then(function (latestVersion) {
return {
latest: latestVersion,
current: this.packageVersion,
type: semverDiff()(this.packageVersion, latestVersion) || 'latest',
name: this.packageName
};
}.bind(this));
};

UpdateNotifier.prototype.notify = function (opts) {
if (!process.stdout.isTTY || isNpm() || !this.update) {
return this;
}

opts = opts || {};

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

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

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

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

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

return this;
};

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

module.exports.UpdateNotifier = UpdateNotifier;
}
9 changes: 9 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
116 changes: 65 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,67 @@
{
"name": "update-notifier",
"version": "1.0.3",
"description": "Update notifications for your CLI app",
"license": "BSD-2-Clause",
"repository": "yeoman/update-notifier",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "if-node-version \">=4\" xo && mocha --timeout 20000"
},
"files": [
"index.js",
"check.js"
],
"keywords": [
"npm",
"update",
"updater",
"notify",
"notifier",
"check",
"checker",
"cli",
"module",
"package",
"version"
],
"dependencies": {
"boxen": "^0.6.0",
"chalk": "^1.0.0",
"configstore": "^2.0.0",
"is-npm": "^1.0.0",
"latest-version": "^2.0.0",
"lazy-req": "^1.1.0",
"semver-diff": "^2.0.0",
"xdg-basedir": "^2.0.0"
},
"devDependencies": {
"clear-require": "^1.0.1",
"fixture-stdout": "^0.2.1",
"if-node-version": "^1.1.0",
"mocha": "*",
"strip-ansi": "^3.0.1",
"xo": "*"
}
"name": "update-notifier",
"version": "6.0.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",
"engines": {
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"update-notifier.js",
"check.js"
],
"keywords": [
"npm",
"update",
"updater",
"notify",
"notifier",
"check",
"checker",
"cli",
"module",
"package",
"version"
],
"dependencies": {
"boxen": "^7.0.0",
"chalk": "^5.0.1",
"configstore": "^6.0.0",
"has-yarn": "^3.0.0",
"import-lazy": "^4.0.0",
"is-ci": "^3.0.1",
"is-installed-globally": "^0.4.0",
"is-npm": "^6.0.0",
"is-yarn-global": "^0.4.0",
"latest-version": "^6.0.0",
"pupa": "^3.1.0",
"semver": "^7.3.7",
"semver-diff": "^4.0.0",
"xdg-basedir": "^5.1.0"
},
"devDependencies": {
"ava": "^4.3.0",
"clear-module": "^4.1.2",
"fixture-stdout": "^0.2.1",
"mock-require": "^3.0.3",
"strip-ansi": "^7.0.1",
"xo": "^0.50.0"
},
"ava": {
"timeout": "20s",
"serial": true
}
}
177 changes: 119 additions & 58 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
# 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
![](screenshot.png)

Inform users of your package of updates in a non-intrusive way.

#### Table of Contents
#### Contents

- [Examples](#examples)
- [Install](#install)
- [Usage](#usage)
- [How](#how)
- [API](#api)
- [About](#about)
- [Users](#users)

## Install

## Examples
```sh
npm install update-notifier
```

## Usage

### Simple example
### 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 example
### 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();
@@ -43,120 +50,174 @@ console.log(notifier.update);
{
latest: '1.0.1',
current: '1.0.0',
type: 'patch', // possible values: latest, major, minor, patch, prerelease, build
type: 'patch', // Possible values: latest, major, minor, patch, prerelease, build
name: 'pageres'
}
*/
```

### Example with settings and custom message
### Options and custom message

```js
const notifier = updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7 // 1 week
});

console.log(`Update available: ${notifier.update.latest}`);
if (notifier.update) {
console.log(`Update available: ${notifier.update.latest}`);
}
```


## 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 check process is done in a unref'ed [child process](http://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.
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

### updateNotifier(options)
### notifier = updateNotifier(options)

Checks if there is an available update. Accepts settings defined below. Returns an object with update info if 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`

##### name

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

##### version

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

#### updateCheckInterval

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

How often to check for updates.

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

Type: `boolean`\
Default: `false`

Type: `function`<br>
Allows notification to be shown when running as an npm script.

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

`update` is equal to `notifier.update`
Type: `string`\
Default: `'latest'`

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

### updateNotifier.notify([options])
### notifier.fetchInfo()

Convenience method to display a notification message *(see screenshot)*.
Check update information.

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

#### options

Type: `object`

#### options.defer
##### defer

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

Defer showing the notification to after the process has exited.

#### options.message
##### message

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

The message that will be shown when an update is available.
Message that will be shown when an update is available.

#### options.boxenOpts
Available placeholders:

Type: `object`<br>
Default: `{ padding: 1, margin: 1, align: 'center', borderColor: 'yellow', borderStyle: 'round' }` ([See the screen shot above](https://github.com/yeoman/update-notifier#update-notifier-))
- `{packageName}` - Package name.
- `{currentVersion}` - Current version.
- `{latestVersion}` - Latest version.
- `{updateCommand}` - Update command.

The object that will be passed to [boxen](https://github.com/sindresorhus/boxen).
```js
notifier.notify({message: 'Run `{updateCommand}` to update.'});

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

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].yml`. The path is available in `notifier.config.path`.
##### isGlobal

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.
Type: `boolean`\
Default: Auto-detect

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

## About
##### boxenOptions

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.
Type: `object`\
Default: `{padding: 1, margin: 1, textAlignment: 'center', borderColor: 'yellow', borderStyle: 'round'}` *(See screenshot)*

There are a bunch projects using it:
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`.

- [Yeoman](http://yeoman.io) - Modern workflows for modern webapps
- [AVA](https://ava.li) - Simple concurrent test runner
- [XO](https://github.com/sindresorhus/xo) - JavaScript happiness style linter
- [Pageres](https://github.com/sindresorhus/pageres) - Capture website screenshots
- [Node GH](http://nodegh.io) - GitHub command line tool
- [Bower](http://bower.io) - A package manager for the web
- [Hoodie CLI](http://hood.ie) - Hoodie command line tool
- [Roots](http://roots.cx) - A toolkit for advanced front-end development
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.

[And 600+ more...](https://www.npmjs.org/browse/depended/update-notifier)
The check is also skipped automatically:
- on CI
- in unit tests (when the `NODE_ENV` environment variable is `test`)

## About

## License
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:

[BSD license](http://opensource.org/licenses/bsd-license.php) and copyright Google
- [npm](https://github.com/npm/npm) - Package manager for JavaScript
- [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
- [Node GH](https://github.com/node-gh/gh) - GitHub command line tool

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

---

<div align="center">
<b>
<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>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>
137 changes: 0 additions & 137 deletions test.js

This file was deleted.

20 changes: 20 additions & 0 deletions test/fs-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import process from 'node:process';
import clearModule from 'clear-module';
import test from 'ava';

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

// 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',
});
});
});
147 changes: 147 additions & 0 deletions test/notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import process from 'node:process';
import {inherits} from 'node:util';
import clearModule from 'clear-module';
import FixtureStdout from 'fixture-stdout';
import stripAnsi from 'strip-ansi';
import test from 'ava';
import mock from 'mock-require';

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

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

const setupTest = async isNpmReturnValue => {
for (const name of ['..', 'is-npm']) {
clearModule(name);
}

process.stdout.isTTY = true;

// TODO: Switch to https://github.com/iambumblehead/esmock
mock('is-npm', {isNpmOrYarn: isNpmReturnValue || false});

const {default: UpdateNotifier} = await import('../update-notifier.js');
inherits(Control, UpdateNotifier);
};

let errorLogs = '';

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

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

test.afterEach(() => {
mock.stopAll();
stderr.release();
errorLogs = '';
});

test.failing('use pretty boxen message by default', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: true});

console.log('d', errorLogs);

t.is(stripAnsi(errorLogs), `
╭───────────────────────────────────────────────────╮
│ │
│ Update available 0.0.2 → 1.0.0 │
│ Run npm i -g update-notifier-tester to update │
│ │
╰───────────────────────────────────────────────────╯
`);
});

test.failing('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.failing('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.failing('exclude -g argument when `isGlobal` option is `false`', t => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: false});
t.not(stripAnsi(errorLogs).indexOf('Run npm i update-notifier-tester to update'), -1);
});

test.failing('shouldNotifyInNpmScript should default to false', t => {
const notifier = new Control();
notifier.notify({defer: false});
t.not(stripAnsi(errorLogs).indexOf('Update available'), -1);
});

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

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

test('should not output if current version is the latest', t => {
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', t => {
setupTest(true);
const notifier = new Control(true);
notifier.update.current = '1.0.1';
notifier.notify({defer: false});
t.false(stripAnsi(errorLogs).includes('Update available'));
});
63 changes: 63 additions & 0 deletions test/update-notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import process from 'node:process';
import fs from 'node:fs';
import test from 'ava';
import mockRequire from 'mock-require';
import updateNotifier from '../index.js';

mockRequire('is-ci', false);

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

let argv;
let configstorePath;

test.beforeEach(() => {
// Prevents NODE_ENV 'test' default behavior which disables `update-notifier`
process.env.NODE_ENV = 'ava-test';

argv = [...process.argv];
configstorePath = updateNotifier(generateSettings()).config.path;
});

test.afterEach(() => {
delete process.env.NO_UPDATE_NOTIFIER;
process.argv = argv;
setTimeout(() => {
fs.unlinkSync(configstorePath);
}, 10_000);
});

test('fetch info', async t => {
const update = await updateNotifier(generateSettings()).fetchInfo();
console.log(update);
t.is(update.latest, '0.0.2');
});

test('fetch info with dist-tag', async t => {
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 => {
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 => {
process.argv.push('--no-update-notifier');
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});

test('don\'t initialize configStore when NODE_ENV === "test"', t => {
process.env.NODE_ENV = 'test';
const notifier = updateNotifier(generateSettings());
t.is(notifier.config, undefined);
});
193 changes: 193 additions & 0 deletions update-notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
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';
import semver from 'semver';
import semverDiff from 'semver-diff';
import latestVersion from 'latest-version';
import {isNpmOrYarn} from 'is-npm';
import isInstalledGlobally from 'is-installed-globally';
import isYarnGlobal from 'is-yarn-global';
import hasYarn from 'has-yarn';
import boxen from 'boxen';
import {xdgConfig} from 'xdg-basedir';
import isCi from 'is-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;
_shouldNotifyInNpmScript;

#options;
#packageVersion;
#updateCheckInterval;
#isDisabled;

constructor(options = {}) {
this.#options = options;
options.pkg = options.pkg || {};
options.distTag = 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')
|| isCi;
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 || !semver.gt(this.update.latest, this.update.current)) {
return this;
}

options = {
isGlobal: isInstalledGlobally,
isYarnGlobal: isYarnGlobal(),
...options,
};

let installCommand;
if (options.isYarnGlobal) {
installCommand = `yarn global add ${this._packageName}`;
} else if (options.isGlobal) {
installCommand = `npm i -g ${this._packageName}`;
} else if (hasYarn()) {
installCommand = `yarn add ${this._packageName}`;
} else {
installCommand = `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 = 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;
}
}