Skip to content

Commit

Permalink
Add template support for the message option (#175)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
fisker and sindresorhus authored Feb 14, 2020
1 parent adf7803 commit adbeb6e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
20 changes: 17 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,13 @@ 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 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,
Expand All @@ -145,7 +151,15 @@ class UpdateNotifier {
borderStyle: 'round'
};

const message = '\n' + boxen()(options.message, options.boxenOptions);
const message = '\n' + 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);
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
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ Default: [See above screenshot](https://github.com/yeoman/update-notifier#update

Message that will be shown when an update is available.

Available placeholders:

- `{packageName}` - Package name.
- `{currentVersion}` - Current version.
- `{latestVersion}` - Latest version.
- `{updateCommand}` - Update command.

```js
notifier.notify({message: 'Run `{updateCommand}` to update.'});

// Output:
// Run `npm install [email protected]` to update.
```

##### isGlobal

Type: `boolean`\
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('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});
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 adbeb6e

Please sign in to comment.