balupton
balupton

Reputation: 48650

How to check if your node.js application's version is the latest?

As my node.js application is a command line application that is run by the user directly. It would make sense for it to phone the github repo and compare its local version against the lastest on the master branch on the github repo. And if the version is outdated, display a message to the user.

So how would one accomplish this phone home and compare versions?

Upvotes: 1

Views: 395

Answers (1)

balupton
balupton

Reputation: 48650

Created a solution to do this myself. Requires the dependency bal-util (so npm install bal-util). Here's the code:

Source code of the packageCompare function can be found here: https://github.com/balupton/bal-util/blob/master/src/lib/compare.coffee

CoffeeScript

# Prepare
balUtil = require('bal-util')
pathUtil = require('path')
debug = false

# Compare
balUtil.packageCompare({
    local: pathUtil.join(__dirname, '..', 'package.json')
    remote: 'https://raw.github.com/bevry/docpad/master/package.json'
    newVersionCallback: (details) ->
        if debug
            console.log """
                There is a new version of #{details.local.name} available, you should probably upgrade...
                current version:  #{details.local.version}
                new version:      #{details.remote.version}
                grab it here:     #{details.remote.homepage}
                """
        else
            console.log "There is a new version of #{details.local.name} available"
})

JavaScript

// Prepare
var balUtil, debug, pathUtil;
balUtil = require('bal-util');
pathUtil = require('path');
debug = false;

// Compare
balUtil.packageCompare({
  local: pathUtil.join(__dirname, '..', 'package.json'),
  remote: 'https://raw.github.com/bevry/docpad/master/package.json',
  newVersionCallback: function(details) {
    if (debug) {
      return console.log("There is a new version of " + details.local.name + " available, you should probably upgrade...\ncurrent version:  " + details.local.version + "\nnew version:      " + details.remote.version + "\ngrab it here:     " + details.remote.homepage);
    } else {
      return console.log("There is a new version of " + details.local.name + " available");
    }
  }
});

Upvotes: 1

Related Questions