Skip to content

Commit

Permalink
Lazy load dependencies (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilkh authored and SBoudrias committed Jun 21, 2016
1 parent 38aa83a commit c2a9565
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
41 changes: 21 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
var spawn = require('child_process').spawn;
var path = require('path');
var format = require('util').format;
var Configstore = require('configstore');
var chalk = require('chalk');
var semverDiff = require('semver-diff');
var latestVersion = require('latest-version');
var isNpm = require('is-npm');
var boxen = require('boxen');
var xdgBasedir = require('xdg-basedir');

var lazyRequire = require('lazy-req')(require);

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) {
Expand All @@ -35,7 +36,8 @@ function UpdateNotifier(options) {

if (!this.hasCallback) {
try {
this.config = new Configstore('update-notifier-' + this.packageName, {
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
Expand All @@ -44,13 +46,13 @@ function UpdateNotifier(options) {
} 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')) +
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));
chalk().cyan(format(' sudo chown -R $USER:$(id -gn $USER) %s ', xdgBasedir().config));

process.on('exit', function () {
console.error('\n' + boxen(msg, {align: 'center'}));
console.error('\n' + boxen()(msg, {align: 'center'}));
});
}
}
Expand All @@ -61,7 +63,6 @@ UpdateNotifier.prototype.check = function () {
this.checkNpm().then(this.callback.bind(this, null)).catch(this.callback);
return;
}

if (
!this.config ||
this.config.get('optOut') ||
Expand Down Expand Up @@ -90,25 +91,25 @@ UpdateNotifier.prototype.check = function () {
};

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

UpdateNotifier.prototype.notify = function (opts) {
if (!process.stdout.isTTY || isNpm || !this.update) {
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.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,
Expand All @@ -118,7 +119,7 @@ UpdateNotifier.prototype.notify = function (opts) {
borderStyle: 'round'
};

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

if (opts.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 @@ -38,6 +38,7 @@
"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"
},
Expand Down

0 comments on commit c2a9565

Please sign in to comment.