Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

izi-config

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

izi-config - npm Package Compare versions

Comparing version 0.0.2 to 1.0.1

lib/configuration.js

155

index.js

@@ -1,126 +0,43 @@

'use strict';
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var log4js = require('log4js');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const loader = require('./lib/loader');
const PhaseResolver = require('./lib/phaseResolver');
/**
*
*/
module.exports = new Config();
const ROOT_DIR = path.dirname(require.main.filename);
const DEFAULT_CONFIG = path.join(ROOT_DIR, 'conf/default.json') ;
function PfxLogger(domain, pfx) {
var that = this;
var logger = log4js.getLogger(domain);
pfx = '[PFX:' + pfx + ']:';
const keywords = ['root', 'require', 'include', 'common', 'prod', 'phase'];
var getArgs = function() {
var args = [].slice.call(arguments);
if((args.length > 0) && (typeof(args[0]) == 'string')) {
args[0] = pfx + ' ' + args[0];
} else {
args.unshift(pfx);
}
return args;
}
this.trace = function() {
logger.trace.apply(logger, getArgs.apply(that, arguments));
}
this.debug = function() {
logger.debug.apply(logger, getArgs.apply(that, arguments));
}
this.info = function() {
logger.info.apply(logger, getArgs.apply(that, arguments));
}
this.warn = function() {
logger.warn.apply(logger, getArgs.apply(that, arguments));
}
this.error = function() {
logger.error.apply(logger, getArgs.apply(that, arguments));
}
this.fatal = function() {
logger.fatal.apply(logger, getArgs.apply(that, arguments));
}
}
// Configuration object
let _conf = {
root: ROOT_DIR,
require: module => require(path.join(ROOT_DIR, module))
};
/**
* Main configuration container
*/
function Config() {
this._args = require('minimist')(process.argv.slice(2));
this.root = path.dirname(require.main.filename);
this.getLogger = function(domain, pfx) {
if(typeof(pfx) !== 'undefined') {
return new PfxLogger(domain, pfx);
} else {
return log4js.getLogger(domain);
}
}
this.require = function(m) {
return require(path.join(this.root, m));
}
this.init = function(global_config, local_config, options) {
var global_json = getJson(global_config);
var local_json = {}
if(local_config) {
local_json = getJson(local_config);
} else if(typeof global_json.local_config !== 'undefined') {
local_json = getJson(path.join(this.root, global_json.local_config));
}
setupOptions(this, options || local_json.options || global_json.options);
_.assign(this, global_json.common);
_.merge(this, global_json[this._phase] || {});
_.merge(this, local_json.common || {});
_.merge(this, local_json[this._phase] || {});
if(typeof this.log4js !== 'undefined') {
log4js.configure(this.log4js);
}
this.logger = this.getLogger('IZI');
}
// Check for configuration file in default location
var default_location = path.join(this.root, 'conf/config.json')
if(fs.existsSync(default_location)) {
this.init(default_location);
}
// Initialize configuration object
function init(config_file) {
const modules = loader(config_file);
const main = modules[config_file];
const phaseResolver = new PhaseResolver(main.phase);
main.resolve(phaseResolver.resolve(), modules);
_.merge(_conf, main.resolved);
}
/**
*
* @param conf
* @param options
*/
function setupOptions(conf, options) {
options = options || {};
options.default_phase = options.default_phase || 'dev';
options.phase_arg = options.phase_arg || 'm';
conf._phase = conf._args[options.phase_arg] || options.default_phase;
}
/** @fn getJson
* Tries to fetch json configuration object based on given input parameter.
*
* @param conf
* Configuration object of path to file containing configuration.
* @returns
* Forwards given configuration object, or returns
* configuration parsed from given file.
*/
function getJson(conf) {
if(typeof conf === 'object') {
return conf;
} else {
if(!fs.existsSync(conf)) {
throw 'Unable to open configuration file ' + conf;
}
return JSON.parse(fs.readFileSync(conf, {encoding: 'utf-8'}));
}
}
if(fs.existsSync(DEFAULT_CONFIG)) {
init(DEFAULT_CONFIG);
module.exports = _conf;
} else {
module.exports = config_file => {
if(!path.isAbsolute(config_file)) {
config_file = path.join(ROOT_DIR, config_file);
}
if(!fs.existsSync(config_file)) {
throw Error(`File ${config_file} doesn't exist`);
}
init(config_file);
module.exports = _conf;
return _conf;
}
}
{
"name": "izi-config",
"version": "0.0.2",
"description": "Module for handling configuration files",
"version": "1.0.1",
"description": "Configuration handler for izi-app",
"main": "index.js",

@@ -9,22 +9,16 @@ "scripts": {

},
"repository": {
"type": "git",
"url": "https://bitbucket.org/izisoft/config-js"
},
"keywords": [
"configuration",
"config"
"configuration"
],
"author": "izisoft",
"license": "MIT",
"homepage": "https://bitbucket.org/izisoft/config-js",
"author": "kcwiakala",
"license": "ISC",
"dependencies": {
"lodash": "^4.12.0",
"log4js": "^1.0.1",
"lodash": "^4.17.11",
"minimist": "^1.2.0"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5"
"chai": "^4.2.0",
"mocha": "^5.2.0",
"rereq": "^0.3.1"
}
}
{
"common": {
"local_loaded": true
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc