1

因为我的 node.js 应用程序是由用户直接运行的命令行应用程序。打电话给 github repo 并将其本地版本与 github repo 上的 master 分支上的最新版本进行比较是有意义的。如果版本过时,则向用户显示一条消息。

那么如何完成这款手机之家并比较版本呢?

4

1 回答 1

0

自己创建了一个解决方案来做到这一点。需要依赖bal-util (so npm install bal-util)。这是代码:

该函数的源代码packageCompare可以在这里找到:https ://github.com/balupton/bal-util/blob/master/src/lib/compare.coffee

咖啡脚本

# 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");
    }
  }
});
于 2012-08-19T02:27:01.370 回答