Skip to content
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

Support for different environments. #4

Merged
merged 5 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions bin/assets/scaffold/config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value"
}
38 changes: 29 additions & 9 deletions bin/elm-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,28 @@ renderElm = function(file) {
}
}

renderHtml = function() {
readConfig = function(options){
var file = path.resolve(`config/${options.env}.json`)
var data;

try {
data = JSON.parse(fs.readFileSync(file, 'utf-8'))
} catch (e) {
console.log("Error reading environment configuration:\n > " + e)
data = {}
}

return data;
}

renderHtml = function(config) {
return `<html>
<head>
</head>
<body style="overflow: hidden;margin:0;">
<script>
window.ENV = ${JSON.stringify(config)}
</script>
<script src='main.js' type='application/javascript'>
</script>
<script>Elm.fullscreen(Elm.Main);</script>
Expand Down Expand Up @@ -205,13 +222,13 @@ buildElm = function() {
}
}

buildHtml = function() {
buildHtml = function(config) {
var destination = path.resolve('dist/index.html')

return function(callback) {
console.log('Building HTML...')

fs.writeFileSync(destination, renderHtml())
fs.writeFileSync(destination, renderHtml(config))

callback(null, null);
}
Expand Down Expand Up @@ -263,16 +280,18 @@ exports.scaffold = function(directory) {
if (fs.existsSync(elmUiConfig)) {
return;
}

fs.writeFileSync(elmUiConfig, JSON.stringify(defaultEmlPackage(), null, " "))
}

exports.serve = function() {
exports.serve = function(options) {
var router = require('koa-router')();
var serve = require('koa-static');
var app = require('koa')();
var config = readConfig(options);

router.get('/', function*(next) {
this.body = renderHtml()
this.body = renderHtml(config)
})

router.get('/main.js', function*(next) {
Expand Down Expand Up @@ -325,8 +344,9 @@ exports.install = function() {
})
}

exports.build = function() {
exports.build = function(options) {
var destination = path.resolve('dist')
var config = readConfig(options);

// Ensure destination
if (!fs.existsSync(destination)) {
Expand All @@ -336,9 +356,9 @@ exports.build = function() {
// Build things with async
async.series([
copyPublic(),
buildHtml(path.join(destination, 'index.html')),
buildElm(path.join(destination, 'main.js')),
buildCSS(path.join(destination, 'main.css'))
buildHtml(config),
buildElm(),
buildCSS()
], function(err, results) {
if (err) {
console.error(err)
Expand Down
11 changes: 8 additions & 3 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ var elmUi = require('./elm-ui')
var path = require('path')
var fs = require('fs')

var options = function(){
return { env: program.env }
}

program
.version('0.0.1')
.option('-e, --env [env]', 'Environment', 'development')

program
.command('install')
Expand All @@ -27,14 +32,14 @@ program
.command('server')
.description('Starts development server')
.action(function(env, opts){
elmUi.serve()
elmUi.serve(options())
})

program
.command('build')
.description('Builds final files')
.action(function(env, opts){
elmUi.build()
elmUi.build(options())
})

program.parse(process.argv);
program.parse(process.argv)
3 changes: 2 additions & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"Ui.Helpers.Drag",
"Ui.Helpers.Animation",
"Ui.Helpers.Dropdown",
"Ui.Helpers.Intendable"
"Ui.Helpers.Intendable",
"Ui.Utils.Env"
],
"dependencies": {
"circuithub/elm-number-format": "1.0.2 <= v < 2.0.0",
Expand Down
16 changes: 16 additions & 0 deletions source/Native/Env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Elm.Native.Env = {};
Elm.Native.Env.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.Env = elm.Native.Env || {};
if (elm.Native.Env.values) {return elm.Native.Env.values; }

/* Gets a value from the ENV. */
function get(key){
return window.ENV && window.ENV[key]
}

/* Interface. */
return elm.Native.Env.values = {
get: get
};
};
27 changes: 27 additions & 0 deletions source/Ui/Utils/Env.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Ui.Utils.Env where

{-| Module for interacting with the environment variables.

@docs get
-}
import Json.Decode as Json
import Native.Env
import Debug

{-| Gets the value of the given environment variable with a decoder and a
default value. -}
get : String -> a -> Json.Decoder a -> a
get key default decoder =
let
value = Native.Env.get key
in
case Json.decodeValue decoder value of
Ok data -> data
Err msg ->
let
log =
Debug.log
("Error getting ENV value '" ++ key ++ "'")
msg
in
default