{"size": 1017, "ext": "js", "max_stars_repo_path": "render.js", "max_stars_repo_name": "penguin-digital/penguin.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "render.js", "max_issues_repo_name": "penguin-digital/penguin.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "render.js", "max_forks_repo_name": "penguin-digital/penguin.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.1136363636, "max_line_length": 77, "alphanum_fraction": 0.6588003933}
const jsdom = require('jsdom')
const path = require('path')
const fs = require('fs')
const mongo = require('mongodb')

const URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/dev'
let db

const MongoClient = mongo.MongoClient
const { JSDOM } = jsdom

MongoClient.connect(
  URL,
  { useNewUrlParser: true },
  function(err, client) {
    if (err) throw err
    db = client.db()
    getData()
  }
)

getData = () => {
  const dom = new JSDOM(fs.readFileSync('./index.html'))
  db.collection('Edits')
    .find({})
    .toArray((err, docs) => {
      console.log(docs)
      docs.map(item => {
        dom.window.document.querySelector(item.path).innerHTML = item.content
      })
      ensureDirectoryExistence('./penguin/index.html')
      fs.writeFileSync('./penguin/index.html', dom.serialize())
    })
}

function ensureDirectoryExistence(filePath) {
  var dirname = path.dirname(filePath)
  if (fs.existsSync(dirname)) {
    return true
  }
  ensureDirectoryExistence(dirname)
  fs.mkdirSync(dirname)
}
