now merges default options

This commit is contained in:
2016-07-17 16:34:31 +02:00
parent dc53498f22
commit 93bbe75f37
9 changed files with 99 additions and 41 deletions

View File

@ -1,24 +1,48 @@
import * as plugins from "./npmextra.plugins"
import * as paths from "./npmextra.paths";
let allData;
export let dataFor = (toolNameArg:string,cwdArg?:string) => {
if(typeof allData == "undefined"){
let lookupPath;
if(cwdArg){
lookupPath = plugins.path.join(cwdArg,"npmextra.json");
} else {
lookupPath = paths.configFile;
};
allData = plugins.smartfile.fs.toObjectSync(
plugins.path.join(lookupPath)
);
export interface IDataFor {
toolName:string;
defaultSettings?;
cwd?:string
}
export let dataFor = (optionsArg:IDataFor) => {
// handle default settings
if(
typeof optionsArg.toolName != undefined
&& typeof optionsArg.defaultSettings != undefined
){
let newDefaultOptions = {};
newDefaultOptions[optionsArg.toolName] = optionsArg.defaultSettings;
optionsArg.defaultSettings = newDefaultOptions;
};
if(toolNameArg){
if(allData[toolNameArg]){
return allData[toolNameArg];
// set lookup path
let lookupPath:string;
if(optionsArg.cwd){
lookupPath = plugins.path.join(optionsArg.cwd,"npmextra.json");
} else {
lookupPath = paths.configFile;
};
// get allData
let allData;
if(plugins.smartfile.fs.fileExistsSync(lookupPath)){
allData = plugins.smartfile.fs.toObjectSync(lookupPath);
} else {
plugins.beautylog.warn(`${lookupPath} is misssing!`);
allData = {};
};
//assign all data
allData = plugins.lodash.merge({},optionsArg.defaultSettings,allData);
if(optionsArg.toolName){
if(allData[optionsArg.toolName]){
return allData[optionsArg.toolName];
} else {
plugins.beautylog.error(`There is no data for ${toolNameArg}`);
plugins.beautylog.error(`There is no data for ${optionsArg.toolName}`);
return undefined;
}
} else {

View File

@ -1,5 +1,6 @@
import "typings-global";
export import beautylog = require("beautylog");
export import lodash = require("lodash");
export import path = require("path");
export import smartfile = require("smartfile");
export import q = require("q");