Skip to content

Use fs.access instead of fs.existsSync #350

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 3 commits into from
Nov 11, 2015
Merged
Changes from 1 commit
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
36 changes: 18 additions & 18 deletions scripts/tasks/download.js
Original file line number Diff line number Diff line change
@@ -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:
* ================
Expand All @@ -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.`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thefourtheye If you feel strong about using only "Download completed.", I'll change this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually should have been

response.on('end', () => 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)
})