Skip to content

Commit

Permalink
Add template support for notify options
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 30, 2019
1 parent adf7803 commit 49a2284
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const hasYarn = importLazy('has-yarn');
const boxen = importLazy('boxen');
const xdgBasedir = importLazy('xdg-basedir');
const isCi = importLazy('is-ci');
const pupa = importLazy('pupa');

const ONE_DAY = 1000 * 60 * 60 * 24;

Expand Down Expand Up @@ -134,8 +135,7 @@ class UpdateNotifier {
installCommand = `npm i ${this.packageName}`;
}

options.message = options.message || 'Update available ' + chalk().dim(this.update.current) + chalk().reset(' → ') +
chalk().green(this.update.latest) + ' \nRun ' + chalk().cyan(installCommand) + ' to update';
const template = options.message || 'Update available ' + chalk().dim('{current}') + chalk().reset(' → ') + chalk().green('{latest}') + ' \nRun ' + chalk().cyan('{command}') + ' to update';

options.boxenOptions = options.boxenOptions || {
padding: 1,
Expand All @@ -145,7 +145,15 @@ class UpdateNotifier {
borderStyle: 'round'
};

const message = '\n' + boxen()(options.message, options.boxenOptions);
const message = '\n' + boxen()(
pupa()(template, {
name: this.packageName,
current: this.update.current,
latest: this.update.latest,
command: installCommand
}),
options.boxenOptions
);

if (options.defer === false) {
console.error(message);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"is-npm": "^4.0.0",
"is-yarn-global": "^0.3.0",
"latest-version": "^5.0.0",
"pupa": "^2.0.1",
"semver-diff": "^3.1.1",
"xdg-basedir": "^4.0.0"
},
Expand Down
16 changes: 15 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ Defer showing the notification to after the process has exited.
Type: `string`\
Default: [See above screenshot](https://github.com/yeoman/update-notifier#update-notifier-)

Message that will be shown when an update is available.
Message that will be shown when an update is available, support [`pupa`](https://github.com/sindresorhus/pupa) template.

Available placeholders:

- `{name}` - Package name.
- `{current}` - Current version.
- `{latest}` - Latest version.
- `{command}` - Install command.

```js
notifier.notify({message: 'Run `npm install {name}@{latest}` to update.'});

// Run `npm install update-notifier-tester@1.0.0` to update.
```


##### isGlobal

Expand Down
40 changes: 39 additions & 1 deletion test/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ test('use pretty boxen message by default', t => {
`);
});

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

t.not(stripAnsi(errorLogs).indexOf('custom message'), -1);
});

test('support message with placeholders', t => {
const notifier = new Control();
notifier.notify({
defer: false,
isGlobal: true,
message: [
'Name: {name}',
'Current: {current}',
'Latest: {latest}',
'Command: {command}'
].join('\n')
});

t.is(stripAnsi(errorLogs), `
╭──────────────────────────────────────────────╮
│ │
│ Name: update-notifier-tester │
│ Current: 0.0.2 │
│ Latest: 1.0.0 │
│ 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});
Expand All @@ -77,7 +115,7 @@ test('suppress output when running as npm script', t => {
t.false(stripAnsi(errorLogs).includes('Update available'));
});

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

0 comments on commit 49a2284

Please sign in to comment.