manis
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -78,15 +78,23 @@ /** | ||
var self = this; | ||
config = findPaths(this.name, from, this.stopper).reduceRight(function (init, path) { | ||
var config = self.loader(fs.readFileSync(path, 'utf-8'), path); | ||
var configs = findConfigs( | ||
this.name, | ||
from, | ||
this.stopper, | ||
function (path) { | ||
var config = self.loader(fs.readFileSync(path, 'utf-8'), path); | ||
if (self.get) { | ||
var type = util.typeOf(self.get); | ||
if (type === 'function') { | ||
config = self.get(config); | ||
if (self.get) { | ||
var type = util.typeOf(self.get); | ||
if (type === 'function') { | ||
config = self.get(config); | ||
} | ||
else { | ||
config = config[self.get]; | ||
} | ||
} | ||
else { | ||
config = config[self.get]; | ||
} | ||
return config; | ||
} | ||
); | ||
config = configs.reduceRight(function (init, config) { | ||
return util.extend(init, config); | ||
@@ -110,14 +118,15 @@ }, Object.create(null)); | ||
* @param {string} start 开始查找的目录,默认为当前目录 | ||
* @param {?Function=} stopper 终结查找的判断方法 | ||
* @param {?Function} stopper 终结查找的判断方法 | ||
* @param {Function} loader 终结查找的判断方法 | ||
* @return {string[]} 能查找到文件的所有路径 | ||
*/ | ||
function findPaths(filename, start, stopper) { | ||
function findConfigs(filename, start, stopper, loader) { | ||
var root = path.resolve('/'); | ||
stopper = stopper || function (start, root, paths) { | ||
stopper = stopper || function (start, root, configs) { | ||
return start === root; | ||
}; | ||
var paths = []; | ||
var configs = []; | ||
var filepath; | ||
@@ -130,6 +139,10 @@ | ||
if (fs.existsSync(filepath)) { | ||
paths.push(filepath); | ||
var config = loader(filepath); | ||
if (Object.keys(config).length > 0) { | ||
configs.push(config); | ||
} | ||
} | ||
if (stopper(start, root, paths)) { | ||
if (stopper(start, root, configs)) { | ||
break; | ||
@@ -142,3 +155,3 @@ } | ||
return paths; | ||
return configs; | ||
} | ||
@@ -151,6 +164,7 @@ | ||
* @param {boolean} cache 是否缓存 | ||
* @param {Function=} stopper 自定义的 stopper | ||
* @param {Function=} loader 自定义的 loader | ||
* @return {Finder} 根据参数创建的 Finder 实例 | ||
*/ | ||
Finder.create = function (name, cache, loader) { | ||
Finder.create = function (name, cache, stopper, loader) { | ||
var options = name; | ||
@@ -164,2 +178,6 @@ if (util.typeOf(name) === 'string') { | ||
if (!options.stopper && util.typeOf(stopper) === 'function') { | ||
options.stopper = stopper; | ||
} | ||
if (!options.loader && util.typeOf(loader) === 'function') { | ||
@@ -166,0 +184,0 @@ options.loader = loader; |
@@ -20,10 +20,23 @@ /** | ||
* @param {string} root 向上查找截止的根目录 | ||
* @param {string[]} paths 已查找到的路径集合 | ||
* @param {Object[]} configs 已查找到的配置集合 | ||
* @return {boolean} 是否仍要查找 | ||
*/ | ||
noLookup: function (start, root, paths) { | ||
return paths.length > 0 || start === root; | ||
noLookup: function (start, root, configs) { | ||
return configs.length > 0 || start === root; | ||
}, | ||
/** | ||
* 生成根据 root 决定是否继续查找的 stopper | ||
* | ||
* @param {string} name root 字段名称 | ||
* @return {Function} 函数说明见 noLookup | ||
*/ | ||
buildStopper: function (name) { | ||
return function (start, root, configs) { | ||
var config = configs[configs.length - 1]; | ||
return config && config[name] || start === root; | ||
}; | ||
}, | ||
/** | ||
@@ -35,3 +48,3 @@ * 设置默认值 | ||
* @param {Manis} manis Manis 实例 | ||
* @param {string} dataType 配置的类型,defaultValue | userConfig | ||
* @param {string} dataType 配置的类型,defaultConfig | userConfig | ||
* @param {(string | Object)} pathOrValue 默认的配置文件路径或默认值 | ||
@@ -110,3 +123,7 @@ * @param {Object=} finderOptions 用于查找默认配置的 finder 的配置 | ||
merge: true, | ||
lookup: true | ||
lookup: true, | ||
orphan: false, | ||
rootName: 'root', | ||
enableRoot: false, | ||
stopper: null | ||
}, options); | ||
@@ -137,8 +154,18 @@ | ||
var stopper = options.stopper; | ||
if (!stopper) { | ||
if (options.orphan) { | ||
stopper = helper.noLookup; | ||
} | ||
else if (options.enableRoot) { | ||
stopper = helper.buildStopper(options.rootName); | ||
} | ||
} | ||
this.finders = files.map(function (file) { | ||
return Finder.create(file, options.cache, options.loader); | ||
return Finder.create(file, options.cache, stopper, options.loader); | ||
}); | ||
this.cached = options.cache && Object.create(null); | ||
this.defaultValue = Object.create(null); | ||
this.defaultConfig = Object.create(null); | ||
this.userConfig = Object.create(null); | ||
@@ -156,3 +183,3 @@ }, | ||
setDefault: function (pathOrValue, finderOptions) { | ||
helper.setConfig(this, 'defaultValue', pathOrValue, finderOptions); | ||
helper.setConfig(this, 'defaultConfig', pathOrValue, finderOptions); | ||
}, | ||
@@ -176,3 +203,3 @@ | ||
if (pathOrNameOrValue[0] === '~' || pathOrNameOrValue.indexOf('/') < 0) { | ||
pathOrNameOrValue = process.env.HOME + '/' + pathOrNameOrValue.replace(/^~/, ''); | ||
pathOrNameOrValue = util.homeDirectory + '/' + pathOrNameOrValue.replace(/^~/, ''); | ||
} | ||
@@ -215,7 +242,9 @@ | ||
if (!this.options.lookup || this.cached && (config = this.cached[from])) { | ||
if (this.cached && (config = this.cached[from])) { | ||
return config; | ||
} | ||
var configs = this.finders.map( | ||
var options = this.options; | ||
if (options.lookup) { | ||
var configs = this.finders.map( | ||
function (finder) { | ||
@@ -226,15 +255,18 @@ return finder.from(from); | ||
if (this.options.merge) { | ||
config = configs.reduceRight( | ||
function (mixed, current) { | ||
return util.extend(mixed, current); | ||
}, | ||
Object.create(null) | ||
); | ||
if (options.orphan) { | ||
config = util.pick(configs); | ||
} | ||
else if (options.merge) { | ||
config = configs.reduceRight(function (init, config) { | ||
return util.extend(init, config); | ||
}, Object.create(null)); | ||
} | ||
else { | ||
config = configs[0]; | ||
} | ||
} | ||
else { | ||
config = configs[0]; | ||
} | ||
config = util.mix(this.defaultValue, this.userConfig, config); | ||
config = options.orphan | ||
? util.pick([config, this.userConfig, this.defaultConfig]) | ||
: util.mix(this.defaultConfig, this.userConfig, config); | ||
@@ -241,0 +273,0 @@ if (this.cached) { |
@@ -55,4 +55,18 @@ /** | ||
/** | ||
* get type of target | ||
* 挑选第一个非空的对象 | ||
* | ||
* @param {Objects[]} objs 对象集合 | ||
* @return {Object} | ||
*/ | ||
exports.pick = function (objs) { | ||
for (var i = 0, obj; obj = objs[i++];) { | ||
if (Object.keys(obj).length > 0) { | ||
return obj; | ||
} | ||
} | ||
}; | ||
/** | ||
* Get type of target | ||
* | ||
* @param {*} target target need to get type | ||
@@ -64,1 +78,38 @@ * @return {string} | ||
}; | ||
/** | ||
* Get user's home directory | ||
* | ||
* @param {Object} processObj an object like process | ||
* @return {string} | ||
*/ | ||
exports.getHome = function (processObj) { | ||
processObj = processObj || process; | ||
var env = processObj.env; | ||
var home = env.HOME; | ||
var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME; | ||
return { | ||
win32: function () { | ||
return env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || home; | ||
}, | ||
darwin: function () { | ||
return home || user && ('/Users/' + user); | ||
}, | ||
linux: function () { | ||
return home || user && (processObj.getuid() === 0 ? '/root' : '/home/' + user); | ||
} | ||
}[processObj.platform](); | ||
}; | ||
/** | ||
* User's home directory | ||
* | ||
* @type {string} | ||
*/ | ||
exports.homeDirectory = exports.getHome(); |
{ | ||
"name": "manis", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Find and read your configuration files recursively", | ||
@@ -5,0 +5,0 @@ "main": "lib/manis.js", |
@@ -132,3 +132,3 @@ Manis | ||
module.exports = function MyGulpPlugin(options) { | ||
var manis = new Manis('.fecsrc', options); | ||
var manis = new Manis('.fecsrc', options); | ||
@@ -178,2 +178,8 @@ return map(function (file, cb) { | ||
- `rootName`, String, The name of flag when `enableRoot` set to true. default is 'root'. | ||
- `enableRoot`, Boolean, Enable the root flag to stop lookup in up-level directory. default is false. | ||
- `stopper`, Function, the predicate for stopping search. default is null. | ||
#### finderOptions | ||
@@ -180,0 +186,0 @@ |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
20428
478
194
2