Skip to content
This repository
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
Jul 18, 2012
Arnout Kazemier 3rd-Eden Added a square#outofdate method that will check the current github re…
…pository's package.json version

to see if they matches, if they are not the same it will probably mean that your version is out of date
and that you need to download a new version from NPM..

This command is added because people never check for updates on command-line utilities, yet is important
to always run the latest version as it has many bug's fixes
3c9d818
Arnout Kazemier 3rd-Eden Merge branch 'master' of github.com:observing/square into development
Conflicts:
	bin/square
	lib/square.js
13bd298

Showing 3 changed files with 114 additions and 37 deletions. Show diff stats Hide diff stats

  1. +44 24 bin/square
  2. +60 13 lib/square.js
  3. +10 0 test/square.api.test.js
68 bin/square
@@ -2,31 +2,32 @@
2 2 "use strict";
3 3
4 4 /**
5   - * Library modules.
  5 + * Native modules.
6 6 */
7 7
8 8 var fs = require('fs')
9   - , path = require('path')
10   - , color = require('colors')
11   - , _ = require('lodash')
12   - , program = require('commander');
  9 + , path = require('path');
13 10
14 11 /**
15   - * Do we need to silence all the output because we spit the shit to std out?
16   - * This needs to be checked before we parse the argv's using commander so we can
17   - * silence the output before that.
  12 + * Third party modules.
18 13 */
19 14
20   -program.silence = ~process.argv.indexOf('--output') || ~process.argv.indexOf('-o');
  15 +var _ = require('lodash')
  16 + , color = require('colors')
  17 + , program = require('commander');
21 18
22 19 /**
23 20 * Square library internals.
24 21 */
25 22
26 23 var Square = require('../lib/square')
27   - , npm = require('../package')
28 24 , square = new Square({ cli: true });
29 25
  26 +// Do we need to silence all the output because we spit the shit to std out?
  27 +// This needs to be checked before we parse the argv's using commander so we can
  28 +// silence the output before that.
  29 +program.silence = ~process.argv.indexOf('--output') || ~process.argv.indexOf('-o');
  30 +
30 31 /**
31 32 * Argument filters for commander.
32 33 *
@@ -61,7 +62,7 @@ var filters = {
61 62
62 63 // Setup the command line interface.
63 64 program
64   - .version(npm.version)
  65 + .version(require('../package.json').version)
65 66 .usage('[options]')
66 67 .option('-b, --bundle <dir/file.json>', 'the location of the package file')
67 68 .option('-e, --extension <extension>', 'filter on this file extension')
@@ -246,7 +247,7 @@ if (!program.silence)
246 247 [
247 248 ''
248 249 , 'o-o o-o o o o-o o-o o-o '.cyan
249   - , ' \\ | | | | |-| | |- '.cyan + (' version: ' + npm.version).white
  250 + , ' \\ | | | | |-| | |- '.cyan + (' version: ' + program._version).white
250 251 , 'o-o o-O o--o o o-o o-o '.cyan
251 252 , ' | '.cyan
252 253 , ' o '.cyan
@@ -304,26 +305,45 @@ program.notFound = function notFound () {
304 305 // This is for example the case for the init/bootstrap commands.
305 306 if (program.commands_exec) return;
306 307
307   -/**
308   - * Make sure that the bundle location exists, so we don't get any ENOENT errors
309   - */
  308 +// Make sure that the bundle location exists, so we don't get any ENOENT errors
  309 +if (!fs.existsSync(program.bundle)) return program.notFound();
  310 +
  311 +// Start search the given directory tree for square.json files. If we cannot
  312 +// parse the bundle we are just gonna return because don't need to do any more
  313 +// processing in this run.
  314 +if (!square.parse(program.bundle)) return;
310 315
311   -if (!fs.existsSync(program.bundle)) {
312   - return program.notFound();
313   -}
314 316
315 317 /**
316   - * Start search the given directory tree for square.json files. If we cannot
317   - * parse the bundle we are just gonna return because don't need to do any more
318   - * processing in this run.
  318 + * Called when our version is no longer the same version as our upstream
  319 + * repository.
  320 + *
  321 + * @param {String} latest upstream version
  322 + * @param {String} current current version
319 323 */
320 324
321   -if (!square.parse(program.bundle)) return;
  325 +function outofdate(latest, current) {
  326 + [
  327 + 'The [square] build system is out of date, the latest version is '.white
  328 + + latest.green + '.'.white
  329 + , 'but you are still running version '.white + current.red
  330 + + '. Please consider upgrading to the'
  331 + , 'latest version to receive all bugfixes and new features.'
  332 + , ''
  333 + , 'type: '.white + 'npm install square -g'.green
  334 + + ' to download the latest version.'.white
  335 + , ''
  336 + ].forEach(function log (line) {
  337 + square.logger.warning(line);
  338 + });
  339 +}
322 340
323 341 // process the plugins
324 342 program.plugins.forEach(function load (plugin) {
325 343 square.plugin(plugin);
326 344 });
327 345
328   -// building all the things
329   -square.build(program.group);
  346 +square
  347 + .once('outofdate', outofdate)
  348 + .outofdate(true)
  349 + .build();
73 lib/square.js
@@ -4,29 +4,30 @@
4 4 * Native modules.
5 5 */
6 6
7   -var fs = require('fs')
8   - , os = require('os')
  7 +var EventEmitter = require('events').EventEmitter
  8 + , createHash = require('crypto').createHash
9 9 , zlib = require('zlib')
10 10 , path = require('path')
11   - , createHash = require('crypto').createHash
12   - , EventEmitter = require('events').EventEmitter;
  11 + , fs = require('fs')
  12 + , os = require('os');
13 13
14 14 /**
15   - * Super charge the EventEmitters, and sprinkle everything with sugar.
  15 + * Third party modules.
16 16 */
17 17
18   -require('eventreactor');
19   -require('sugar');
  18 +var canihaz = require('canihaz')('square')
  19 + , exec = require('shelljs').exec
  20 + , Logger = require('devnull')
  21 + , async = require('async')
  22 + , eson = require('eson')
  23 + , _ = require('lodash');
20 24
21 25 /**
22   - * Third party modules.
  26 + * Super charge the EventEmitters, and sprinkle everything with sugar.
23 27 */
24 28
25   -var Logger = require('devnull')
26   - , async = require('async')
27   - , eson = require('eson')
28   - , _ = require('lodash')
29   - , exec = require('shelljs').exec;
  29 +require('eventreactor');
  30 +require('sugar');
30 31
31 32 /**
32 33 * Pre-process filters.
@@ -667,6 +668,52 @@ Square.prototype.build = function build(extension, fn) {
667 668 };
668 669
669 670 /**
  671 + * Check if we are running an out of date build of square. Because users are not
  672 + * actively checking if their binary is up to date and will be missing out on
  673 + * critical bug fixes and feature releases.
  674 + *
  675 + * @param {Boolean} random should we do a random check
  676 + * @param {String} branch optional branch
  677 + * @returns {Boolean} allowed to check
  678 + * @api public
  679 + */
  680 +
  681 +Square.prototype.outofdate = function outofdate(random, branch) {
  682 + // allow users to cancel our update check
  683 + if (this.package.configuration && this.package.configuration.noupdate) return false;
  684 +
  685 + var luckynumber = Math.floor(Math.random() * 11)
  686 + , url = 'https://raw.github.com/observing/square/'
  687 + + (branch || 'master')
  688 + + '/package.json'
  689 + , self = this;
  690 +
  691 + // rate limit the amount of checks that we are allowed to do, we don't want to
  692 + // check github for every invocation of square
  693 + if (random && luckynumber !== 7) return false
  694 +
  695 + canihaz.request(function lazyload (err, request) {
  696 + if (err) return;
  697 +
  698 + request({ uri: url }, function done (err, res, body) {
  699 + var latest;
  700 +
  701 + // no need to do error handling here.. if there was an error, we can't
  702 + // probably parse JSON anyways and it will just die inside the JSON.parse
  703 + try { latest = JSON.parse(body.toString('utf8')).version; }
  704 + catch (e) { return; }
  705 +
  706 + if (latest && latest !== self.version) {
  707 + self.emit('outofdate', latest, self.version);
  708 + }
  709 + });
  710 + });
  711 +
  712 + return;
  713 +};
  714 +
  715 +
  716 +/**
670 717 * Process the template string.
671 718 *
672 719 * @param {String} str
10 test/square.api.test.js
@@ -153,6 +153,16 @@ describe('[square] API', function () {
153 153
154 154 describe('#build', function () {});
155 155
  156 + describe('#outofdate', function () {
  157 + it('should check the upstream repo for its version number');
  158 +
  159 + it('should check different branches of the upstream');
  160 +
  161 + it('should randomly check for an upstream');
  162 +
  163 + it('should not check upstream if forbidden by configuration');
  164 + });
  165 +
156 166 describe('#tag', function () {
157 167 it('should return the branch name, as this is a git repository');
158 168

No commit comments for this range

Something went wrong with that request. Please try again.