'use strict'

var github = require('octonode')
var client = github.client()
var evRepo = client.repo('nodejs/evangelism')

var https = require('https')
var path = require('path')
var fs = require('fs')

/* Currently proof-of-concept work. Outstanding:
 * ================
 * - [ ] gulpify
 * - [ ] add to local boot process or trigger at key times
 * - [ ] support other content patterns (meeting notes, release notes, etc.)
 * - [ ] support similar patterns for other locales
 * - [ ] prepend predictable markdown metadata on download
 */

function checkOrFetchFile (file) {
  let name = file.name
  let downloadUrl = file.download_url

  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
  }

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

  var outputFile = fs.createWriteStream(localPath)
  https.get(downloadUrl, function (response) {
    response.pipe(outputFile)
  })
}

evRepo.contents('weekly-updates', function (err, files) {
  if (err) { throw err }
  files.forEach(checkOrFetchFile)
})

@@ -7,19 +7,21 @@
 trict'%0A%0A
-var
+const
  github 
@@ -46,160 +46,238 @@
 e')%0A
-var client = github.client()%0Avar evRepo = client.repo('nodejs/evangelism')%0A%0Avar https = require('https')%0Avar path = require('path')%0Avar fs = require('fs
+const https = require('https')%0Aconst path = require('path')%0Aconst fs = require('fs')%0A%0Aconst basePath = path.join(__dirname, '..', '..', 'locale', 'en', 'blog', 'weekly-updates')%0Aconst repo = github.client().repo('nodejs/evangelism
 ')%0A%0A
@@ -655,201 +655,114 @@
 %7B%0A  
-let name = file.name%0A  let downloadUrl = file.download_url%0A%0A  let localPath = path.join(__dirname, '..', '..', 'locale', 'en', 'blog', 'weekly-updates', 
+const filePath = path.join(basePath, file.
 name)%0A
+%0A
   
-if (fs.existsSync(localPath)
+fs.access(filePath, function (err) %7B%0A    if (!err
 ) %7B%0A
+  
     
@@ -790,20 +790,24 @@
 pdate $%7B
-name
+filePath
 %7D exists
@@ -839,22 +839,28 @@
     
+  
 return%0A
+  
   %7D%0A%0A  
+ 
+ 
 cons
@@ -884,20 +884,24 @@
 pdate $%7B
-name
+filePath
 %7D does n
@@ -932,57 +932,8 @@
 %0A%0A  
-var outputFile = fs.createWriteStream(localPath)%0A
   ht
@@ -940,24 +940,29 @@
 tps.get(
+file.
 download
 Url, fun
@@ -953,17 +953,18 @@
 download
-U
+_u
 rl, func
@@ -989,32 +989,120 @@
     
-response.pipe(outputFile
+  console.log(%60Weekly Update $%7BfilePath%7D downloaded.%60)%0A      response.pipe(fs.createWriteStream(filePath))%0A    %7D
 )%0A  
@@ -1111,11 +1111,9 @@
 %0A%7D%0A%0A
-evR
+r
 epo.

Use `fs.access` instead of `fs.existsSync`