-
Notifications
You must be signed in to change notification settings - Fork 62
/
localsStore.js
36 lines (33 loc) · 1.19 KB
/
localsStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var fs = require('fs')
var extend = require('extend')
var url = require('url')
var me = module.exports = {}
var locals = {}
me.refresh = function resfresh() {
locals = JSON.parse(fs.readFileSync('./locals.json'))
var hash = locals.explanationsHash = {}
locals.explanations.reverse().forEach(function(d) { hash[d.slug] = d })
Object.keys(hash).forEach(function(slug) {
hash[slug].slug = slug
hash[slug].path = locals.basepath + slug + '/'
})
var files = fs.readdirSync(locals.src + '/explanations')
var filesHash = {}
files.forEach(function(d) { filesHash[d] = true })
// Check that each explanation has its folder.
locals.explanations.forEach(function(d) {
if (!filesHash[d.slug])
cb(new Error('missing folder for explanation ' + d.slug))
})
locals.people = JSON.parse(fs.readFileSync('./inspiring-people.json'))
locals.people.forEach(function(d) {
if(d.src) d.hostname = url.parse(d.src).hostname.replace(/www\./, '')
})
return locals
}
// Return all the locals for a given slug.
me.forSlug = function forSlug(slug) {
var myLocals = locals.explanationsHash[slug] || {}
myLocals.href = 'http://setosa.io' + myLocals.path
return extend({}, locals, myLocals)
}