Comparing version 0.0.2 to 1.0.1
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
20
313
1
10931
3
2
0
12
2
- Removedlog4js@^1.0.1
- Removedcore-util-is@1.0.3(transitive)
- Removeddate-format@0.0.0(transitive)
- Removeddebug@0.7.42.6.9(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedlog4js@1.1.1(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.0.0(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedsemver@5.7.2(transitive)
- Removedstreamroller@0.4.1(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedlodash@^4.17.11