Skip to content

replace custom logic in loadVersions with node-version-data package #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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