npmextra/ts/npmextra.classes.npmextra.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2016-09-23 20:13:06 +00:00
import * as plugins from './npmextra.plugins'
import * as paths from './npmextra.paths'
/**
2016-09-24 14:44:48 +00:00
* Npmextra class allows easy configuration of tools
2016-09-23 20:13:06 +00:00
*/
2016-09-24 14:44:48 +00:00
export class Npmextra {
cwd: string
lookupPath: string
npmextraJsonExists: boolean
npmextraJsonData: boolean
2016-09-23 20:13:06 +00:00
2016-09-24 14:44:48 +00:00
/**
* creates instance of Npmextra
*/
constructor(cwdArg?: string) {
if (cwdArg) {
this.cwd = cwdArg
} else {
this.cwd = paths.cwd
}
this.checkLookupPath()
this.checkNpmextraJsonExists()
this.checkNpmextraJsonData()
}
2016-09-23 20:13:06 +00:00
2016-09-24 14:44:48 +00:00
/**
* merges the supplied options with the ones from npmextra.json
*/
dataFor(toolnameArg: string, defaultOptionsArg: any) {
let npmextraToolOptions
if(this.npmextraJsonData[toolnameArg]) {
npmextraToolOptions = this.npmextraJsonData[toolnameArg]
2016-09-23 20:13:06 +00:00
} else {
2016-09-24 14:44:48 +00:00
npmextraToolOptions = {}
2016-09-23 20:13:06 +00:00
}
2016-09-24 14:44:48 +00:00
let mergedOptions = plugins.lodash.merge({}, defaultOptionsArg, npmextraToolOptions)
return mergedOptions
}
/**
* checks if the JSON exists
*/
private checkNpmextraJsonExists() {
this.npmextraJsonExists = plugins.smartfile.fs.fileExistsSync(this.lookupPath)
2016-09-23 20:13:06 +00:00
}
2016-09-24 14:44:48 +00:00
/**
* gets lookupPath
*/
private checkLookupPath() {
if (this.cwd) {
this.lookupPath = plugins.path.join(this.cwd, 'npmextra.json')
2016-09-23 20:13:06 +00:00
} else {
2016-09-24 14:44:48 +00:00
this.lookupPath = paths.configFile
};
}
/**
* get npmextraJsonData
*/
private checkNpmextraJsonData() {
if (this.npmextraJsonExists) {
this.npmextraJsonData = plugins.smartfile.fs.toObjectSync(this.lookupPath)
2016-09-23 20:13:06 +00:00
}
}
}