如何检查node.js应用程序的版本是否是最新的?

时间:2012-08-19 02:27:01

标签: node.js

由于my node.js application是由用户直接运行的命令行应用程序。它是有意义的,它打电话给github仓库,并将其本地版本与github repo上主分支上的最新版本进行比较。如果版本已过时,请向用户显示一条消息。

那么如何才能完成这款手机并对比版本呢?

1 个答案:

答案 0 :(得分:0)

我自己创建了一个解决方案。需要依赖项bal-util(所以npm install bal-util)。这是代码:

packageCompare函数的源代码可在此处找到: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");
    }
  }
});