npmextra/ts/index.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2016-07-16 23:23:22 +00:00
import * as plugins from "./npmextra.plugins"
import * as paths from "./npmextra.paths";
2016-07-17 14:34:31 +00:00
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;
};
// 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 {
2016-07-17 15:10:53 +00:00
plugins.beautylog.warn(`${lookupPath} is missing!`);
2016-07-17 14:34:31 +00:00
allData = {};
2016-07-16 23:23:22 +00:00
};
2016-07-17 14:34:31 +00:00
//assign all data
allData = plugins.lodash.merge({},optionsArg.defaultSettings,allData);
if(optionsArg.toolName){
if(allData[optionsArg.toolName]){
return allData[optionsArg.toolName];
2016-07-16 23:23:22 +00:00
} else {
2016-07-17 14:34:31 +00:00
plugins.beautylog.error(`There is no data for ${optionsArg.toolName}`);
2016-07-16 23:23:22 +00:00
return undefined;
}
} else {
return allData;
}
}