Skip to content

Commit

Permalink
Add option to exclude -g argument from default update message (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
colingourlay authored and sindresorhus committed Feb 18, 2017
1 parent d295cf3 commit 607d3c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -107,10 +107,10 @@ class UpdateNotifier {
return this;
}

opts = opts || {};
opts = Object.assign({isGlobal: true}, 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';
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan('npm i ' + (opts.isGlobal ? '-g ' : '') + this.packageName) + ' to update';

opts.boxenOpts = opts.boxenOpts || {
padding: 1,
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -131,6 +131,13 @@ Default: [See above screenshot](https://github.com/yeoman/update-notifier#update

Message that will be shown when an update is available.

##### isGlobal

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

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

##### boxenOpts

Type: `Object`<br>
Expand Down
24 changes: 16 additions & 8 deletions test.js
Expand Up @@ -97,6 +97,15 @@ describe('notify(opts)', () => {
updateNotifier = require('./');
});

function Control() {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
}
util.inherits(Control, updateNotifier.UpdateNotifier);

let errorLogs = '';

beforeEach(() => {
Expand All @@ -112,14 +121,6 @@ describe('notify(opts)', () => {
});

it('should use pretty boxen message by default', () => {
function Control() {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
}
util.inherits(Control, updateNotifier.UpdateNotifier);
const notifier = new Control();
notifier.notify({defer: false});
assert.equal(stripAnsi(errorLogs), [
Expand All @@ -135,4 +136,11 @@ describe('notify(opts)', () => {
''
].join('\n'));
});

it('should exclude -g argument when `isGlobal` option is `false`', () => {
const notifier = new Control();
notifier.notify({defer: false, isGlobal: false});
assert.notEqual(-1, stripAnsi(errorLogs)
.indexOf('Run npm i update-notifier-tester to update'));
});
});

0 comments on commit 607d3c9

Please sign in to comment.