npmextra/ts/index.ts

69 lines
1.8 KiB
TypeScript
Raw Normal View History

2016-09-16 20:28:38 +00:00
import * as plugins from './npmextra.plugins'
import * as paths from './npmextra.paths'
2016-07-16 23:23:22 +00:00
2016-07-17 14:34:31 +00:00
export interface IDataFor {
2016-09-16 20:28:38 +00:00
toolName: string
defaultSettings?
cwd?: string
2016-07-17 14:34:31 +00:00
}
2016-09-16 20:28:38 +00:00
/**
* gets you the configuration data for
* @executes SYNC
*/
export let dataFor = <IOptions>(optionsArg: IDataFor): IOptions => {
2016-07-17 14:34:31 +00:00
// handle default settings
2016-09-16 20:28:38 +00:00
if (
typeof optionsArg.toolName !== undefined
&& typeof optionsArg.defaultSettings !== undefined
) {
let newDefaultOptions = {}
newDefaultOptions[optionsArg.toolName] = optionsArg.defaultSettings
optionsArg.defaultSettings = newDefaultOptions
2016-07-17 14:34:31 +00:00
};
// set lookup path
2016-09-16 20:28:38 +00:00
let lookupPath: string
if (optionsArg.cwd) {
lookupPath = plugins.path.join(optionsArg.cwd,'npmextra.json')
2016-07-17 14:34:31 +00:00
} else {
2016-09-16 20:28:38 +00:00
lookupPath = paths.configFile
2016-07-17 14:34:31 +00:00
};
2016-09-16 20:28:38 +00:00
2016-07-17 14:34:31 +00:00
// get allData
2016-09-16 20:28:38 +00:00
let allData
if (plugins.smartfile.fs.fileExistsSync(lookupPath)) {
allData = plugins.smartfile.fs.toObjectSync(lookupPath)
2016-07-17 14:34:31 +00:00
} else {
2016-09-16 20:28:38 +00:00
plugins.beautylog.warn(`${lookupPath} is missing!`)
allData = {}
2016-07-16 23:23:22 +00:00
};
2016-07-17 14:34:31 +00:00
2016-09-16 20:28:38 +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-09-16 20:28:38 +00:00
plugins.beautylog.error(`There is no data for ${optionsArg.toolName}`)
return undefined
2016-07-16 23:23:22 +00:00
}
} else {
2016-09-16 20:28:38 +00:00
return allData
2016-07-16 23:23:22 +00:00
}
2016-09-16 20:28:38 +00:00
}
2016-09-16 20:28:38 +00:00
/**
* tells you if a configfile is present
*/
export let configFilePresent = (optionsArg: {cwd?: string}): boolean => {
let lookupPath: string
if (optionsArg.cwd) {
lookupPath = plugins.path.join(optionsArg.cwd,'npmextra.json')
} else {
lookupPath = paths.configFile
};
return plugins.smartfile.fs.fileExistsSync(lookupPath)
}