From fdedf38a1ccc570ca5ce30e4bf72a0927fa119b1 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 11 Nov 2015 10:05:46 +0100 Subject: [PATCH 1/3] Use `fs.access` instead of `fs.existsSync` --- scripts/tasks/download.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/scripts/tasks/download.js b/scripts/tasks/download.js index 401678782446d..309d359966afd 100644 --- a/scripts/tasks/download.js +++ b/scripts/tasks/download.js @@ -1,12 +1,12 @@ 'use strict' -var github = require('octonode') -var client = github.client() -var evRepo = client.repo('nodejs/evangelism') +const github = require('octonode') +const https = require('https') +const path = require('path') +const fs = require('fs') -var https = require('https') -var path = require('path') -var fs = require('fs') +const basePath = path.join(__dirname, '..', '..', 'locale', 'en', 'blog', 'weekly-updates') +const repo = github.client().repo('nodejs/evangelism') /* Currently proof-of-concept work. Outstanding: * ================ @@ -18,24 +18,24 @@ var fs = require('fs') */ function checkOrFetchFile (file) { - let name = file.name - let downloadUrl = file.download_url + const filePath = path.join(basePath, file.name) - let localPath = path.join(__dirname, '..', '..', 'locale', 'en', 'blog', 'weekly-updates', name) - if (fs.existsSync(localPath)) { - console.log(`Weekly Update ${name} exists. (No SHA check, yet.)`) - return - } + fs.access(filePath, function (err) { + if (!err) { + console.log(`Weekly Update ${filePath} exists. (No SHA check, yet.)`) + return + } - console.log(`Weekly Update ${name} does not exist. Downloading.`) + console.log(`Weekly Update ${filePath} does not exist. Downloading.`) - var outputFile = fs.createWriteStream(localPath) - https.get(downloadUrl, function (response) { - response.pipe(outputFile) + https.get(file.download_url, function (response) { + console.log(`Weekly Update ${filePath} downloaded.`) + response.pipe(fs.createWriteStream(filePath)) + }) }) } -evRepo.contents('weekly-updates', function (err, files) { +repo.contents('weekly-updates', function (err, files) { if (err) { throw err } files.forEach(checkOrFetchFile) }) From 2bc445ad9cfd9c71ea253569a3304bf38d051666 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 11 Nov 2015 11:02:27 +0100 Subject: [PATCH 2/3] Add missing `error` listeners --- scripts/tasks/download.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/tasks/download.js b/scripts/tasks/download.js index 309d359966afd..04f50b1b52d9b 100644 --- a/scripts/tasks/download.js +++ b/scripts/tasks/download.js @@ -30,7 +30,14 @@ function checkOrFetchFile (file) { https.get(file.download_url, function (response) { console.log(`Weekly Update ${filePath} downloaded.`) - response.pipe(fs.createWriteStream(filePath)) + + const ws = fs.createWriteStream(filePath) + ws.on('error', function (err) { + console.error(err.stack) + }) + response.pipe(ws) + }).on('error', function (err) { + console.error(err.stack) }) }) } From ba338a59fb0eed2816fa0b800dbe814b0890e6d3 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 11 Nov 2015 11:19:01 +0100 Subject: [PATCH 3/3] Use arrow functions --- scripts/tasks/download.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/scripts/tasks/download.js b/scripts/tasks/download.js index 04f50b1b52d9b..1e4d398b292e5 100644 --- a/scripts/tasks/download.js +++ b/scripts/tasks/download.js @@ -20,7 +20,7 @@ const repo = github.client().repo('nodejs/evangelism') function checkOrFetchFile (file) { const filePath = path.join(basePath, file.name) - fs.access(filePath, function (err) { + fs.access(filePath, (err) => { if (!err) { console.log(`Weekly Update ${filePath} exists. (No SHA check, yet.)`) return @@ -28,21 +28,17 @@ function checkOrFetchFile (file) { console.log(`Weekly Update ${filePath} does not exist. Downloading.`) - https.get(file.download_url, function (response) { - console.log(`Weekly Update ${filePath} downloaded.`) - + https.get(file.download_url, (response) => { const ws = fs.createWriteStream(filePath) - ws.on('error', function (err) { - console.error(err.stack) - }) + ws.on('error', (err) => console.error(err.stack)) + + response.on('end', () => console.log(`Weekly Update ${filePath} downloaded.`)) response.pipe(ws) - }).on('error', function (err) { - console.error(err.stack) - }) + }).on('error', (err) => console.error(err.stack)) }) } -repo.contents('weekly-updates', function (err, files) { +repo.contents('weekly-updates', (err, files) => { if (err) { throw err } files.forEach(checkOrFetchFile) })