get-latest-nodejs-version.js 800 B

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