Skip to content

Commit

Permalink
add setUserConfig, fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswong committed Oct 14, 2015
1 parent 31e9490 commit d9ac86a
Showing 1 changed file with 92 additions and 38 deletions.
130 changes: 92 additions & 38 deletions lib/manis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,77 @@ var path = require('path');
var util = require('./util');
var Finder = require('./finder');

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]));
}
};

/**
* Manis
*
Expand Down Expand Up @@ -66,61 +137,44 @@ util.extend(

this.cached = options.cache && Object.create(null);
this.defaultValue = Object.create(null);

this.userConfig = Object.create(null);
},

/**
* 设置默认值
* 默认值优先级最低
*
* @public
* @param {(string | Object)} pathOrValue 默认的配置文件路径或默认值
* @param {Object=} finderOptions 用于查找默认配置的 finder 的配置
*/
setDefault: function (pathOrValue, finderOptions) {
var type = util.typeOf(pathOrValue);
if (type === 'object') {
this.defaultValue = 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 = function (start, root, paths) {
return paths.length > 0 || start === root;
};
helper.setConfig(this, 'defaultValue', pathOrValue, finderOptions);
},

var finder;
var name = match[2];

if (finderOptions && util.typeOf(finderOptions) === 'object') {
finderOptions = util.extend({name: name, stopper: stopper}, finderOptions);
finder = new Finder(finderOptions);
/**
* 设置用户默认值
* 用户默认值优先级高于默认值
*
* @public
* @param {(string | Object)} pathOrNameOrValue 默认的配置文件路径、文件名或默认值
* @param {Object=} finderOptions 用于查找默认配置的 finder 的配置
*/
setUserConfig: function (pathOrNameOrValue, finderOptions) {
if (!pathOrNameOrValue && this.finders[0]) {
pathOrNameOrValue = this.finders[0].name;
}
else {
finder = this.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});
if (util.typeOf(pathOrNameOrValue) === 'string') {
if (pathOrNameOrValue[0] === '~' || pathOrNameOrValue.indexOf('/') < 0) {
pathOrNameOrValue = process.env.HOME + '/' + pathOrNameOrValue.replace(/^~/, '');
}

pathOrNameOrValue = pathOrNameOrValue.replace(/\/{2}/g, '/');
}

this.defaultValue = finder.from(match[1]);
helper.setConfig(this, 'userConfig', pathOrNameOrValue, finderOptions);
},

/**
Expand Down Expand Up @@ -174,7 +228,7 @@ util.extend(
config = configs[0];
}

config = util.mix(this.defaultValue, config);
config = util.mix(this.defaultValue, this.userConfig, config);

if (this.cached) {
this.cached[from] = config;
Expand Down

0 comments on commit d9ac86a

Please sign in to comment.