diff --git a/dist/config.js b/dist/config.js new file mode 100644 index 0000000..cfd2aac --- /dev/null +++ b/dist/config.js @@ -0,0 +1,32 @@ +/* + * cylon configuration loader + * cylonjs.com + * + * Copyright (c) 2013 The Hybrid Group + * Licensed under the Apache 2.0 license. +*/ + + +(function() { + var fetch, namespace; + + namespace = require('node-namespace'); + + fetch = function(variable, defaultValue) { + if (defaultValue == null) { + defaultValue = false; + } + if (process.env[variable] != null) { + return process.env[variable]; + } else { + return defaultValue; + } + }; + + namespace('Config', function() { + return this.testing_mode = fetch("CYLON_TEST", false); + }); + + module.exports = Config; + +}).call(this); diff --git a/dist/cylon.js b/dist/cylon.js index 92ea10b..11c2d9a 100644 --- a/dist/cylon.js +++ b/dist/cylon.js @@ -13,6 +13,8 @@ __slice = [].slice, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + require('./config'); + require('./utils'); require('./logger'); diff --git a/dist/robot.js b/dist/robot.js index 290374b..adc5ac0 100644 --- a/dist/robot.js +++ b/dist/robot.js @@ -72,7 +72,6 @@ this.registerAdaptor("./test/test-adaptor", "test"); this.registerDriver("./test/ping", "ping"); this.registerDriver("./test/test-driver", "test"); - this.testing = process.env['CYLON_TEST']; this.initConnections(opts.connection || opts.connections); this.initDevices(opts.device || opts.devices); this.work = opts.work || function() { @@ -215,7 +214,7 @@ connection: connection, extraParams: opts }); - if (this.robot.testing != null) { + if (Config.testing_mode) { testAdaptor = this.robot.requireAdaptor('test').adaptor({ name: adaptorName, connection: connection, @@ -251,7 +250,7 @@ device: device, extraParams: opts }); - if (this.robot.testing != null) { + if (Config.testing_mode) { testDriver = this.robot.requireDriver('test').driver({ name: driverName, device: device, diff --git a/src/config.coffee b/src/config.coffee new file mode 100644 index 0000000..754967e --- /dev/null +++ b/src/config.coffee @@ -0,0 +1,38 @@ +### + * cylon configuration loader + * cylonjs.com + * + * Copyright (c) 2013 The Hybrid Group + * Licensed under the Apache 2.0 license. +### + +namespace = require 'node-namespace' + +# Public: Fetches a variable from the environment, returning a provided value if +# it's not set. +# +# variable - variable to fetch from the environment +# defaultValue - value to return if the ENV variable isn't set +# +# Examples +# +# process.env["CYLON_TEST"] #=> undefined +# fetch("CYLON_TEST", "not set") +# #=> "not set" +# +# process.env["CYLON_TEST"] #=> false +# fetch("CYLON_TEST", true) +# #=> false +# +# process.env["CYLON_TEST"] #=> true +# fetch("CYLON_TEST", false) +# #=> true +# +# Returns the env var or default value +fetch = (variable, defaultValue = false) -> + if process.env[variable]? then process.env[variable] else defaultValue + +namespace 'Config', -> + @testing_mode = fetch("CYLON_TEST", false) + +module.exports = Config diff --git a/src/cylon.coffee b/src/cylon.coffee index 1f4f340..460e733 100644 --- a/src/cylon.coffee +++ b/src/cylon.coffee @@ -8,6 +8,7 @@ 'use strict' +require './config' require './utils' require './logger' require './driver' diff --git a/src/robot.coffee b/src/robot.coffee index 1f0f56e..2eebaf7 100644 --- a/src/robot.coffee +++ b/src/robot.coffee @@ -66,8 +66,6 @@ namespace 'Cylon', -> @registerDriver "./test/ping", "ping" @registerDriver "./test/test-driver", "test" - @testing = process.env['CYLON_TEST'] - @initConnections(opts.connection or opts.connections) @initDevices(opts.device or opts.devices) @work = opts.work or -> (Logger.info "No work yet") @@ -187,7 +185,7 @@ namespace 'Cylon', -> connection: connection, extraParams: opts - if @robot.testing? + if Config.testing_mode testAdaptor = @robot.requireAdaptor('test').adaptor name: adaptorName, connection: connection, @@ -231,7 +229,7 @@ namespace 'Cylon', -> device: device, extraParams: opts - if @robot.testing? + if Config.testing_mode testDriver = @robot.requireDriver('test').driver name: driverName, device: device,