manis
Advanced tools
Comparing version 0.1.0 to 0.1.1
130
lib/manis.js
@@ -12,2 +12,73 @@ /** | ||
var helper = { | ||
/** | ||
* 用于 finder 查找的 stopper,找到第一个文件后即停止查找 | ||
* | ||
* @inner | ||
* @param {string} start 开始查找的目录 | ||
* @param {string} root 向上查找截止的根目录 | ||
* @param {string[]} paths 已查找到的路径集合 | ||
* @return {boolean} 是否仍要查找 | ||
*/ | ||
noLookup: function (start, root, paths) { | ||
return paths.length > 0 || start === root; | ||
}, | ||
/** | ||
* 设置默认值 | ||
* 默认值优先级最低 | ||
* | ||
* @inner | ||
* @param {Manis} manis Manis 实例 | ||
* @param {string} dataType 配置的类型,defaultValue | userConfig | ||
* @param {(string | Object)} pathOrValue 默认的配置文件路径或默认值 | ||
* @param {Object=} finderOptions 用于查找默认配置的 finder 的配置 | ||
*/ | ||
setConfig: function (manis, dataType, pathOrValue, finderOptions) { | ||
var type = util.typeOf(pathOrValue); | ||
if (type === 'object') { | ||
manis[dataType] = util.mix(manis[dataType], pathOrValue); | ||
return; | ||
} | ||
if (type !== 'string') { | ||
throw new Error('invalid argument'); | ||
} | ||
var match = pathOrValue.match(/^(.*\/)([^\/]+)$/); | ||
if (!match) { | ||
throw new Error('Invalid path.'); | ||
} | ||
// no look up | ||
var stopper = helper.noLookup; | ||
var finder; | ||
var name = match[2]; | ||
if (finderOptions && util.typeOf(finderOptions) === 'object') { | ||
finderOptions = util.extend({name: name, stopper: stopper}, finderOptions); | ||
finder = new Finder(finderOptions); | ||
} | ||
else { | ||
finder = manis.finders.filter(function (finder) { | ||
return finder.name.toLowerCase() === name; | ||
})[0]; | ||
if (finder) { | ||
finder = Object.create(finder); | ||
finder.stopper = stopper; | ||
} | ||
else { | ||
finder = new Finder({name: name, stopper: stopper}); | ||
} | ||
} | ||
manis[dataType] = util.mix(manis[dataType], finder.from(match[1])); | ||
} | ||
}; | ||
/** | ||
@@ -70,3 +141,3 @@ * Manis | ||
this.defaultValue = Object.create(null); | ||
this.userConfig = Object.create(null); | ||
}, | ||
@@ -76,2 +147,3 @@ | ||
* 设置默认值 | ||
* 默认值优先级最低 | ||
* | ||
@@ -83,46 +155,28 @@ * @public | ||
setDefault: function (pathOrValue, finderOptions) { | ||
var type = util.typeOf(pathOrValue); | ||
if (type === 'object') { | ||
this.defaultValue = pathOrValue; | ||
return; | ||
} | ||
helper.setConfig(this, 'defaultValue', pathOrValue, finderOptions); | ||
}, | ||
if (type !== 'string') { | ||
throw new Error('invalid argument'); | ||
} | ||
var match = pathOrValue.match(/^(.*\/)([^\/]+)$/); | ||
if (!match) { | ||
throw new Error('Invalid path.'); | ||
/** | ||
* 设置用户默认值 | ||
* 用户默认值优先级高于默认值 | ||
* | ||
* @public | ||
* @param {(string | Object)} pathOrNameOrValue 默认的配置文件路径、文件名或默认值 | ||
* @param {Object=} finderOptions 用于查找默认配置的 finder 的配置 | ||
*/ | ||
setUserConfig: function (pathOrNameOrValue, finderOptions) { | ||
if (!pathOrNameOrValue && this.finders[0]) { | ||
pathOrNameOrValue = this.finders[0].name; | ||
} | ||
// no look up | ||
var stopper = function (start, root, paths) { | ||
return paths.length > 0 || start === root; | ||
}; | ||
var finder; | ||
var name = match[2]; | ||
if (finderOptions && util.typeOf(finderOptions) === 'object') { | ||
finderOptions = util.extend({name: name, stopper: stopper}, finderOptions); | ||
finder = new Finder(finderOptions); | ||
} | ||
else { | ||
finder = this.finders.filter(function (finder) { | ||
return finder.name.toLowerCase() === name; | ||
})[0]; | ||
if (finder) { | ||
finder = Object.create(finder); | ||
finder.stopper = stopper; | ||
if (util.typeOf(pathOrNameOrValue) === 'string') { | ||
if (pathOrNameOrValue[0] === '~' || pathOrNameOrValue.indexOf('/') < 0) { | ||
pathOrNameOrValue = process.env.HOME + '/' + pathOrNameOrValue.replace(/^~/, ''); | ||
} | ||
else { | ||
finder = new Finder({name: name, stopper: stopper}); | ||
} | ||
pathOrNameOrValue = pathOrNameOrValue.replace(/\/{2}/g, '/'); | ||
} | ||
this.defaultValue = finder.from(match[1]); | ||
helper.setConfig(this, 'userConfig', pathOrNameOrValue, finderOptions); | ||
}, | ||
@@ -181,3 +235,3 @@ | ||
config = util.mix(this.defaultValue, config); | ||
config = util.mix(this.defaultValue, this.userConfig, config); | ||
@@ -184,0 +238,0 @@ if (this.cached) { |
{ | ||
"name": "manis", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Find and read your configuration files recursively", | ||
@@ -5,0 +5,0 @@ "main": "lib/manis.js", |
@@ -28,4 +28,15 @@ Manis | ||
var loader = function (text) { | ||
return JSON.parse(stripJSONComments(text)); | ||
}; | ||
var manis = new Manis({ | ||
files: [ | ||
{ | ||
// just for example, it should be loaded as yaml in fact. | ||
name: '.eslintrc', | ||
get: function (json) { | ||
return {eslint: json}; | ||
} | ||
}, | ||
'.fecsrc', | ||
@@ -39,5 +50,3 @@ { | ||
], | ||
loader: function (content) { | ||
return JSON.parse(stripJSONComments(content)); | ||
} | ||
loader: loader | ||
}); | ||
@@ -92,2 +101,28 @@ | ||
### User config | ||
```javascript | ||
var Manis = require('manis'); | ||
var manis = new Manis({ | ||
files: [ | ||
'.fecsrc', | ||
{ | ||
name: 'package.json', | ||
get: 'fecs' | ||
} | ||
] | ||
}); | ||
manis.setDefault('default/path/to/config/fecs.json'); | ||
// will find `~/.fecsrc` | ||
manis.setUserConfig(); | ||
var options = manis.from('path/to/file.js'); | ||
// do something cool with options | ||
``` | ||
### Within a gulp plugin | ||
@@ -127,2 +162,6 @@ | ||
### void Manis#setUserConfig(); | ||
### void Manis#setUserConfig(Object userConfig); | ||
### void Manis#setUserConfig(string userConfigPathOrName[, Object finderOptions]); | ||
### Object Manis#from(string path); | ||
@@ -129,0 +168,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
17400
390
188
3