Skip to content

Commit

Permalink
Read YAML from the environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelig committed Jul 26, 2016
1 parent 318bc2f commit 32a0e9a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 43 deletions.
18 changes: 0 additions & 18 deletions .moniteurrc.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,5 @@
# - .moniteurrc.yml (override file):
# copy and paste the contents of this file to .moniteurrc.yml
# and tweak the values as desired

# assets:
# FT desktop CSS bundle:
# - "http://s1.ft-static.com/m/style/90975546/bundles/core.css"
# - "http://navigation.webservices.ft.com/v1/navigation/ft/css/style.min.css"
# - "http://s1.ft-static.com/m/style/5c37627a/bundles/nonArticle.css"
# Guardian's CSS:
# - "http://assets.guim.co.uk/stylesheets/global.css"
# - "http://assets.guim.co.uk/stylesheets/head.default.css"
# Main CSS: "fixtures/main.css"
# Another CSS: "fixtures/main2.css"
# ABC Script: "fixtures/abc.js"
# My XYZ Script: "fixtures/xyz.js"
# My Bundle of Scripts:
# - "fixtures/abc.js"
# - "fixtures/xyz.js"
# A remote Script: "http://assets.guim.co.uk/javascripts/bootstraps/app.js"
# A website: "http://www.theguardian.com"
db:
directory: ".moniteurdb"
1 change: 0 additions & 1 deletion .moniteurrc.development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ assets:
db:
# Uncomment to use Redis instead of the filesystem
# redis_url: redis://localhost:6379
directory: ".moniteurdb"
28 changes: 4 additions & 24 deletions bin/moniteur.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import babelify from 'express-babelify-middleware'
import path from 'path'
import lem from 'lem'
import logger from 'morgan'
import yaml from 'js-yaml'
nconf.formats.yaml = require('nconf-yaml')

const log = debug('moniteur:log')
Expand All @@ -22,7 +23,7 @@ nconf
.env({
separator: '__',
lowerCase: true,
whitelist: ['REDISCLOUD_URL', 'REDIS_URL', 'NODE_ENV', 'DB__REDIS_URL', 'ASSETS']
whitelist: ['REDISCLOUD_URL', 'REDIS_URL', 'NODE_ENV', 'DB__REDIS_URL']
})
.argv()

Expand All @@ -31,32 +32,11 @@ nconf
.file('user', { file: path.join(__dirname, '/../.moniteurrc.yml'), format: nconf.formats.yaml })
.file('default', { file: path.join(__dirname, '/../.moniteurrc.default.yml'), format: nconf.formats.yaml })

// Transform:
// foo:http://foo.com/asset.js,
// bar:http://bar.com/asset.js
// Into:
// {
// foo: "http://foo.com/asset.js",
// bar: "http://bar.com/asset.js"
// }
const processAssets = (assetList) =>
assetList
// Remove all linebreaks that might have been inserted in
// the Heroku environment variables
.replace(/(\r\n|\n|\r)/gm, '')
// Then let's break this string into an array
// and return an object of key-value paris
.split(',').reduce((assets, asset) => {
const [key, ...url] = asset.split(':')
assets[key] = url.join('')
.replace(/http(s?)\/\//, 'http$1://')
return assets
}, {})
nconf
.set('assets', process.env.ASSETS ? yaml.safeLoad(process.env.ASSETS) : nconf.get('assets'))

// nconf evaluates the : in the protocol as a key:value pair
// so we're restoring the colon in the URL protocols
nconf
.set('assets', process.env.ASSETS ? processAssets(process.env.ASSETS) : nconf.get('assets'))
nconf
.set('db:redis_url',
process.env.REDIS_URL ? process.env.REDIS_URL.replace(/redis\/\//, 'redis://')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"glob": "^7.0.5",
"gzip-size": "^3.0.0",
"highcharts": "^4.2.5",
"js-yaml": "^3.6.1",
"jquery": "^3.1.0",
"lem": "0.4.8",
"level": "^1.4.0",
Expand Down

0 comments on commit 32a0e9a

Please sign in to comment.