Skip to content

Commit 6751c87

Browse files
committed
fix(updateNotifier): Made clever commands faster
1 parent 4489969 commit 6751c87

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

bin/clever

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
11
#!/usr/bin/env node
22

3-
var path = require( 'path' )
4-
, shell = require( 'shelljs' )
5-
, lib = require( path.join( __dirname, '..', 'index' ) );
3+
var path = require( 'path' )
4+
, shell = require( 'shelljs' )
5+
, async = require( 'async' )
6+
, configStore = require( 'configstore' )
7+
, updateNotifier = require( 'update-notifier' )
8+
, rootDir = path.resolve( path.join( __dirname, '..' ) )
9+
, libDir = path.join( rootDir, 'lib' )
10+
, lib = require( path.join( libDir, 'index' ) )
11+
, updateNotifier = require( 'update-notifier' )
12+
, checkUpdatesEvery = 1000 * 60 * 60 * 24 // once every day
13+
, packageJson = require( path.join( rootDir, 'package.json' ) )
14+
, notification;
615

7-
/**
8-
* Check for bower, grunt and npm within $PATH
9-
*
10-
* @param {String} cmd Which command to look up
11-
* @return {Boolean} Returns whether or not the command was found
12-
* @private
13-
*/
16+
async.waterfall(
17+
[
18+
function checkDependencies( checksDone ) {
19+
async.each(
20+
[
21+
{ cmd: 'npm', name: 'NPM', install: '.Please install Node.JS before proceeding.' },
22+
{ cmd: 'bower', name: 'Bower', install: ' please type: npm install -g bower' },
23+
{ cmd: 'grunt', name: 'Grunt-CLI', install: ' please type: npm install -g grunt-cli' }
24+
],
25+
function checkDependencies( check, checkDone ) {
26+
checkDone( !!shell.which( check.cmd ) ? null : check.name + ' is required before using CleverStack-CLI' + check.install );
27+
},
28+
checksDone
29+
);
30+
},
1431

15-
function checkCmd ( cmd ) {
16-
return shell.which( cmd );
17-
}
32+
function checkForUpdates( checkDone ) {
33+
var updateConfig = new configStore( 'update-notifier-cleverstack-cli', {
34+
optOut : false,
35+
lastUpdateCheck : Date.now()
36+
});
1837

19-
var checks = [
20-
{ cmd: 'npm', name: 'NPM', install: '.Please install Node.JS before proceeding.' },
21-
{ cmd: 'bower', name: 'Bower', install: ' please type: npm install -g bower' },
22-
{ cmd: 'grunt', name: 'Grunt-CLI', install: ' please type: npm install -g grunt-cli' }
23-
];
38+
// Only check for updates on a set interval
39+
if ( Date.now() - updateConfig.get( 'lastUpdateCheck' ) > checkUpdatesEvery ) {
40+
notification = updateNotifier( { pkg: packageJson, callback: checkDone } )
41+
} else {
42+
checkDone( null );
43+
}
44+
}
45+
],
46+
function runCommand( err, update ) {
47+
var hasErrored = err !== undefined && err !== null;
2448

25-
checks.forEach( function ( check ) {
26-
if ( !checkCmd( check.cmd ) ) {
27-
lib.utils.error( check.name + ' is required before using CleverStack-CLI' + check.install );
28-
}
29-
});
49+
if ( !!hasErrored && !update ) {
50+
lib.utils.error( err );
51+
} else {
52+
if ( !!hasErrored && !!update && update.type !== 'latest' ) {
53+
notifier.update = update;
54+
notifier.notify( { defer: true } );
55+
}
3056

31-
var updateNotifier = require( path.resolve( path.join( __dirname, '..', 'lib', 'updateNotifier' ) ) )( function( err, update ) {
32-
if ( update && update.type !== 'latest' ) {
33-
updateNotifier.update = update;
34-
updateNotifier.notify( { defer: true } );
57+
// avoid the command within process.argv
58+
lib.command( __dirname, process.argv[ 2 ], process.argv.slice( 3 ) );
59+
}
3560
}
36-
37-
// avoid the command within process.argv
38-
lib.command( __dirname, process.argv[ 2 ], process.argv.slice( 3 ) );
39-
});
61+
);

lib/updateNotifier.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"t-case": "~0.1.0",
104104
"tab": "~0.1.0",
105105
"tar": "~1.0.2",
106-
"update-notifier": "^0.3.0"
106+
"update-notifier": "^0.3.0",
107+
"configstore": "^0.3.1"
107108
},
108109
"devDependencies": {
109110
"mocha": "~2.0.1",

0 commit comments

Comments
 (0)