Skip to content

Commit

Permalink
replace custom logic with node-version-data package
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 27, 2015
1 parent 6b9515a commit 3086185
Show file tree
Hide file tree
Showing 3 changed files with 2,960 additions and 325 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"metalsmith-serve": "0.0.3",
"metalsmith-stylus": "1.0.0",
"ncp": "2.0.0",
"node-version-data": "1.0.0",
"octonode": "0.7.4",
"require-dir": "0.3.0",
"semver": "5.0.3",
Expand Down
61 changes: 7 additions & 54 deletions scripts/load-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,21 @@
'use strict'

const fs = require('fs')
const semver = require('semver')
const map = require('map-async')
const https = require('https')
const nodeVersionData = require('node-version-data')

function loadVersions (callback) {
map(
[ 'https://nodejs.org/dist/index.json', 'https://iojs.org/dist/index.json' ],
download,
function (err, versions) {
if (err) { return callback(err) }
versions = munge(versions)
callback(null, versions)
}
)
}

function download (url, cb) {
let data = ''
https.get(url, function (res) {
res.on('data', function (chunk) { data += chunk })
res.on('end', function () {
try {
cb(null, JSON.parse(data))
} catch (e) {
return cb(e)
}
})
}).on('error', function (e) {
console.error('Error downloading file from %s: %s', url, e.message)
cb(e)
})
}

function munge (versions) {
versions[0].forEach(function (v) {
v.url = 'https://nodejs.org/dist/' + v.version + '/'
v.name = 'Node.js'
})
versions[1].forEach(function (v) {
v.url = 'https://iojs.org/dist/' + v.version + '/'
v.name = 'io.js'
})

let allVersions = versions[0].concat(versions[1])

allVersions.sort(function (a, b) {
return semver.compare(b.version, a.version)
})

return allVersions
}

module.exports = loadVersions
module.exports = nodeVersionData

if (require.main === module) {
loadVersions(function (err, versions) {
nodeVersionData((err, versions) => {
if (err) {
console.error('Aborting due to download error from node or iojs')
console.error(err.stack)
return process.exit(1)
}

fs.writeFileSync(__dirname + '/../source/versions.json', JSON.stringify(versions, null, 2))
fs.writeFileSync(
__dirname + '/../source/versions.json'
, JSON.stringify(versions, null, 2)
)
})
}
Loading

0 comments on commit 3086185

Please sign in to comment.