smartenv/index.js

110 lines
3.6 KiB
JavaScript
Raw Normal View History

/// <reference path="index.ts" />
var Environment = (function () {
function Environment(runtimeEnvArg, userAgentArg) {
if (userAgentArg === void 0) { userAgentArg = "undefined"; }
this.runtimeEnv = runtimeEnvArg;
this.userAgent = userAgentArg;
if (runtimeEnvArg == "node") {
this.isBrowser = false;
this.isNode = true;
this.nodeVersion = process.version;
}
else {
this.isBrowser = true;
this.isNode = true;
this.nodeVersion = "undefined";
}
}
;
return Environment;
})();
2015-11-30 18:58:35 +00:00
/// <reference path="index.ts" />
/**
* Deals with the environment the current JS script is running in.
*/
var SmartenvEnvironment;
(function (SmartenvEnvironment) {
2015-12-02 14:23:48 +00:00
var environment;
var envDetermined = false;
/**
* returns the environment
* @returns {Environment}
*/
var getEnv = function () {
if (!envDetermined) {
(function () {
var localRunTimeEnv = "undefined";
var localUserAgent = "undefined";
if (typeof window !== 'undefined') {
localRunTimeEnv = 'browser';
localUserAgent = navigator.userAgent;
}
else if (typeof process !== 'undefined') {
localRunTimeEnv = 'node';
}
environment = new Environment(localRunTimeEnv, localUserAgent);
})();
}
;
return environment;
};
/**
* prints the environment to console
*/
var printEnv = function () {
if (this.getEnv().isNode) {
var smartenvVersion = require("./package.json").version;
plugins.beautylog.log("node version is " + this.getEnv().nodeVersion + " and smartenv version is " + smartenvVersion);
}
else {
plugins.beautylog.log("browser is " + this.getEnv().userAgent);
}
plugins.beautylog.log("the smartenv registration store currently holds the following properties:");
console.log(Object.getOwnPropertyNames(smartenv.obs.getAll()));
2015-12-02 14:23:48 +00:00
};
SmartenvEnvironment.init = function (objectArg) {
objectArg.getEnv = getEnv;
objectArg.printEnv = printEnv;
};
2015-11-30 18:58:35 +00:00
})(SmartenvEnvironment || (SmartenvEnvironment = {}));
/// <reference path="index.ts" />
var SmartenvObjectStorage;
(function (SmartenvObjectStorage) {
function init() {
var obs = {};
var obsItems = {};
obs.add = function (objectArg, paramName) {
2015-11-30 18:58:35 +00:00
if (paramName === void 0) { paramName = "undefined"; }
if (paramName == "undefined") {
plugins.beautylog.error("paramName is undefined");
return;
}
obsItems[paramName] = objectArg;
};
obs.get = function (keyName) {
2015-11-30 18:58:35 +00:00
return obsItems[keyName];
};
obs.getAll = function () {
2015-11-30 18:58:35 +00:00
return obsItems;
};
obs.addComplete = function (itemsArg) {
obsItems = plugins._.assign(obsItems, itemsArg);
return obsItems;
};
return obs;
}
2015-11-30 18:58:35 +00:00
SmartenvObjectStorage.init = init;
})(SmartenvObjectStorage || (SmartenvObjectStorage = {}));
/// <reference path="typings/tsd.d.ts" />
/// <reference path="smartenv.classes.ts" />
/// <reference path="smartenv.environment.ts" />
/// <reference path="smartenv.objectstorage.ts" />
var plugins = {
beautylog: require("beautylog")("os"),
_: require("lodash")
2015-11-26 05:31:31 +00:00
};
2015-11-30 18:58:35 +00:00
var smartenv = {}; //create smartenv object
2015-12-02 14:23:48 +00:00
SmartenvEnvironment.init(smartenv);
2015-11-30 18:58:35 +00:00
smartenv.obs = SmartenvObjectStorage.init();
2015-11-26 05:31:31 +00:00
module.exports = smartenv;