Skip to content

Take advantage of the request dependency #418

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
Dec 14, 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
33 changes: 13 additions & 20 deletions scripts/release-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,35 @@

'use strict'

const https = require('https')
const fs = require('fs')
const path = require('path')
const extend = require('util')._extend
const Handlebars = require('handlebars')
const url = require('url')
const request = require('request')

const downloads = require('./helpers/downloads')

function request (uri, method) {
function sendRequest (uri, method) {
return new Promise(function (resolve, reject) {
// user-agent is required when by api.github.com
const opts = extend({
method,
headers: {
'user-agent': 'nodejs.org release blog post script'
request({
headers: { 'User-Agent': 'nodejs.org release blog post script' },
method: method,
uri: uri
}, function (err, res, body) {
if (err) {
return reject(new Error(`Error requesting URL ${uri}: ${err.message}`))
}
}, url.parse(uri))

let data = ''

https.request(opts, function (res) {
if (res.statusCode !== 200) {
return reject(new Error(`Invalid status code (!= 200) while retrieving ${uri}: ${res.statusCode}`))
}

res.on('data', function (chunk) { data += chunk })
res.on('end', function () { resolve(data) })
}).on('error', function (err) {
reject(new Error(`Error requesting URL ${uri}: ${err.message}`))
}).end()
resolve(body)
})
})
}

function download (url) {
return request(url, 'GET')
return sendRequest(url, 'GET')
}

function explicitVersion (version) {
Expand Down Expand Up @@ -159,7 +152,7 @@ function findAuthorLogin (version, section) {
}

function urlOrComingSoon (binary) {
return request(binary.url, 'HEAD').then(
return sendRequest(binary.url, 'HEAD').then(
() => `${binary.title}: ${binary.url}`,
() => `${binary.title}: *Coming soon*`)
}
Expand Down
30 changes: 21 additions & 9 deletions scripts/tasks/download.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const github = require('octonode')
const https = require('https')
const request = require('request')
const path = require('path')
const fs = require('fs')

Expand All @@ -27,14 +27,26 @@ function checkOrFetchFile (file) {
}

console.log(`Weekly Update ${filePath} does not exist. Downloading.`)

https.get(file.download_url, (response) => {
const ws = fs.createWriteStream(filePath)
ws.on('error', (err) => console.error(err.stack))

response.on('end', () => console.log(`Weekly Update ${filePath} downloaded.`))
response.pipe(ws)
}).on('error', (err) => console.error(err.stack))
const uri = file.download_url

request
.get(uri)
.on('error', (err) => console.error(err.stack))
.on('response', function (response) {
if (response.statusCode !== 200) {
this.emit('error', new Error(
`Invalid status code (!= 200) while retrieving ${uri}: ${response.statusCode}`
))
return
}

const ws = fs.createWriteStream(filePath)

ws.on('error', this.emit.bind(this, 'error'))
ws.on('finish', () => console.log(`Weekly Update ${filePath} downloaded.`))

response.pipe(ws)
})
})
}

Expand Down