Skip to content

Commit

Permalink
feat(configure): use the Aurelia loader dependency to load configurat…
Browse files Browse the repository at this point in the history
…ion files instead of HTTP
  • Loading branch information
Vheissu committed Mar 1, 2016
1 parent 68f1c0e commit 2b9a814
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/configure.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'core-js';

import {inject} from 'aurelia-dependency-injection';
import {HttpClient} from 'aurelia-http-client';
import {Loader} from 'aurelia-loader';

// Secure references that can't be changed outside of Configure singleton class
const ENVIRONMENT = new WeakMap();
Expand All @@ -11,23 +11,24 @@ const CONFIG_FILE = new WeakMap();
const CONFIG_OBJECT = new WeakMap();
const CASCADE_MODE = new WeakMap();

@inject(HttpClient)
@inject(Loader)
export class Configure {
constructor(http) {
constructor(loader) {
// Injected dependencies
this.http = http;
this.loader = loader;

CONFIG_OBJECT.set(this, {});

ENVIRONMENT.set(this, 'default');
ENVIRONMENTS.set(this, false);
DIRECTORY.set(this, 'config');
CONFIG_FILE.set(this, 'application.json');
CONFIG_FILE.set(this, 'config.json');
CASCADE_MODE.set(this, true);
}

/**
* Set Directory
*
* Sets the location to look for the config file
*
* @param path
Expand All @@ -38,6 +39,7 @@ export class Configure {

/**
* Set Config
*
* Sets the filename to look for in the defined directory
*
* @param name
Expand All @@ -48,6 +50,7 @@ export class Configure {

/**
* Set Environment
*
* Changes the environment value
*
* @param environment
Expand All @@ -58,6 +61,7 @@ export class Configure {

/**
* Set Environments
*
* Specify multiple environment domains to allow for
* dynamic environment switching.
*
Expand All @@ -74,6 +78,7 @@ export class Configure {

/**
* Set Cascade Mode
*
* By default if a environment config value is not found, it will
* go looking up the config file to find it (a la inheritance style). Sometimes
* you just want a config value from a specific environment and nowhere else
Expand All @@ -87,6 +92,7 @@ export class Configure {

/**
* Get Config
*
* Returns the entire configuration object pulled and parsed from file
*
* @returns {V}
Expand All @@ -97,6 +103,7 @@ export class Configure {

/**
* Get Environment
*
* Gets the current environment value
*
* @returns {V}
Expand All @@ -107,6 +114,7 @@ export class Configure {

/**
* Get Environments
*
* Gets any user supplied environment mappings
*
* @returns {array}
Expand All @@ -117,6 +125,7 @@ export class Configure {

/**
* Get Cascade Mode
*
* Gets the current cascade mode boolean
* @returns {boolean}
*/
Expand All @@ -126,6 +135,7 @@ export class Configure {

/**
* Get Directory
*
* Gets the current directory
*
* @returns {V}
Expand All @@ -136,6 +146,7 @@ export class Configure {

/**
* Get Config
*
* Get the config file name
*
* @returns {V}
Expand All @@ -146,6 +157,7 @@ export class Configure {

/**
* Is
*
* A method for determining if the current environment
* equals that of the supplied environment value*
* @param environment
Expand Down Expand Up @@ -283,11 +295,29 @@ export class Configure {
this.obj[parent][child] = val;
}
}

/**
* Merge
*
* Allows you to merge in configuration options
* this method might be used to merge in server-loaded
* configuration options with local ones.
*
* @param obj
*
*/
merge(obj) {
let currentConfig = CONFIG_OBJECT.get(this);
let merged = Object.assign(currentConfig, obj);

CONFIG_OBJECT.set(this, merged);
}

/**
* Set All
* A dangerous method that sets the entire config object
* only used during bootstrapping phase
* Sets and overwrites the entire configuration object
* used internally, but also can be used to set the configuration
* from outside of the usual JSON loading logic.
*
* @param obj
*/
Expand All @@ -313,13 +343,7 @@ export class Configure {
* @returns {Promise}
*/
loadConfig() {
return new Promise((resolve, reject) => {
this.http
.get(`${this.directory}/${this.config}`)
.then(response => {
resolve(response.content);
})
.catch(() => reject(new Error('Configuration file could not be found or loaded.')));
});
return this.loader.loadText(`${this.directory}/${this.config}`)
.catch(() => reject(new Error('Configuration file could not be found or loaded.')));
}
}

0 comments on commit 2b9a814

Please sign in to comment.