Skip to content

Commit

Permalink
feat: center-align message via boxen@0.6.0 (#84)
Browse files Browse the repository at this point in the history
* feat: center-align message via boxen@0.6.0

* test: simplify use of clear-require
  • Loading branch information
nexdrew authored and SBoudrias committed Jun 13, 2016
1 parent ff17da4 commit d61bd9b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var latestVersion = require('latest-version');
var isNpm = require('is-npm');
var boxen = require('boxen');
var xdgBasedir = require('xdg-basedir');
var ansiAlign = require('ansi-align');
var ONE_DAY = 1000 * 60 * 60 * 24;

function UpdateNotifier(options) {
Expand Down Expand Up @@ -43,12 +42,14 @@ function UpdateNotifier(options) {
});
} catch (_) {
// 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 () {
var msg = [chalk.yellow(format(' %s update check failed ', options.pkg.name))];
msg.push(format(' Try running with %s or get access ', chalk.cyan('sudo')));
msg.push(' to the local update config store via ');
msg.push(chalk.cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgBasedir.config)));
console.error('\n' + boxen(ansiAlign.center(msg).join('\n')));
console.error('\n' + boxen(msg, {align: 'center'}));
});
}
}
Expand Down Expand Up @@ -108,6 +109,7 @@ UpdateNotifier.prototype.notify = function (opts) {
var message = '\n' + boxen('Update available ' + chalk.dim(this.update.current) + chalk.reset(' → ') + chalk.green(this.update.latest) + ' \nRun ' + chalk.cyan('npm i -g ' + this.packageName) + ' to update', {
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round'
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
"version"
],
"dependencies": {
"ansi-align": "^1.0.0",
"boxen": "^0.5.1",
"boxen": "^0.6.0",
"chalk": "^1.0.0",
"configstore": "^2.0.0",
"is-npm": "^1.0.0",
Expand All @@ -44,7 +43,9 @@
},
"devDependencies": {
"clear-require": "^1.0.1",
"fixture-stdout": "^0.2.1",
"mocha": "*",
"strip-ansi": "^3.0.1",
"xo": "*"
}
}
78 changes: 72 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
'use strict';
var assert = require('assert');
var fs = require('fs');
var util = require('util');
var clearRequire = require('clear-require');
var FixtureStdout = require('fixture-stdout');
var stripAnsi = require('strip-ansi');
var updateNotifier = require('./');

describe('updateNotifier', function () {
Expand Down Expand Up @@ -44,19 +47,15 @@ describe('updateNotifier', function () {

describe('updateNotifier with fs error', function () {
before(function () {
clearRequire('./');
clearRequire('configstore');
clearRequire('xdg-basedir');
['./', 'configstore', 'xdg-basedir'].forEach(clearRequire);
// set configstore.config to something
// that requires root access
process.env.XDG_CONFIG_HOME = '/usr';
updateNotifier = require('./');
});

after(function () {
clearRequire('./');
clearRequire('configstore');
clearRequire('xdg-basedir');
['./', 'configstore', 'xdg-basedir'].forEach(clearRequire);
delete process.env.XDG_CONFIG_HOME;
updateNotifier = require('./');
});
Expand All @@ -69,3 +68,70 @@ describe('updateNotifier with fs error', function () {
}).config);
});
});

describe('notify(opts)', function () {
var stderr = new FixtureStdout({
stream: process.stderr
});
var processEnvBefore;
var isTTYBefore;

before(function () {
['./', 'is-npm'].forEach(clearRequire);
processEnvBefore = JSON.stringify(process.env);
isTTYBefore = process.stdout.isTTY;
['npm_config_username', 'npm_package_name', 'npm_config_heading'].forEach(function (name) {
delete process.env[name];
});
process.stdout.isTTY = true;
updateNotifier = require('./');
});

after(function () {
['./', 'is-npm'].forEach(clearRequire);
process.env = JSON.parse(processEnvBefore);
process.stdout.isTTY = isTTYBefore;
processEnvBefore = undefined;
isTTYBefore = undefined;
updateNotifier = require('./');
});

var errorLogs = '';

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

afterEach(function () {
stderr.release();
errorLogs = '';
});

it('should use pretty boxen message by default', function () {
function Control() {
this.packageName = 'update-notifier-tester';
this.update = {
current: '0.0.2',
latest: '1.0.0'
};
}
util.inherits(Control, updateNotifier.UpdateNotifier);
var notifier = new Control();
notifier.notify({defer: false});
assert.equal(stripAnsi(errorLogs), [
'',
'',
' ╭───────────────────────────────────────────────────╮',
' │ │',
' │ Update available 0.0.2 → 1.0.0 │',
' │ Run npm i -g update-notifier-tester to update │',
' │ │',
' ╰───────────────────────────────────────────────────╯',
'',
''
].join('\n'));
});
});

0 comments on commit d61bd9b

Please sign in to comment.