• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var log = require('npmlog')
2var request = require('request')
3var semver = require('semver')
4
5function getLatestNodejsVersion (url, cb) {
6  var tracker = log.newItem('getLatestNodejsVersion', 1)
7  tracker.info('getLatestNodejsVersion', 'Getting Node.js release information')
8  var version = 'v0.0.0'
9  url = url || 'https://nodejs.org/dist/index.json'
10  request(url, function (e, res, index) {
11    tracker.finish()
12    if (e) return cb(e)
13    if (res.statusCode !== 200) {
14      return cb(new Error('Status not 200, ' + res.statusCode))
15    }
16    try {
17      JSON.parse(index).forEach(function (item) {
18        if (item.lts && semver.gt(item.version, version)) version = item.version
19      })
20      cb(null, version)
21    } catch (e) {
22      cb(e)
23    }
24  })
25}
26
27module.exports = getLatestNodejsVersion
28