npmextra/ts/npmextra.classes.npmextra.ts

71 lines
1.6 KiB
TypeScript
Raw Permalink 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 {
2017-03-18 15:23:47 +00:00
cwd: string
lookupPath: string
npmextraJsonExists: boolean
npmextraJsonData: any
2016-09-23 20:13:06 +00:00
2017-03-18 15:23:47 +00:00
/**
* creates instance of Npmextra
*/
2017-07-12 13:30:49 +00:00
constructor (cwdArg?: string) {
2017-03-18 15:23:47 +00:00
if (cwdArg) {
this.cwd = cwdArg
} else {
this.cwd = paths.cwd
2016-09-24 14:44:48 +00:00
}
2017-03-18 15:23:47 +00:00
this.checkLookupPath()
this.checkNpmextraJsonExists()
this.checkNpmextraJsonData()
}
2016-09-23 20:13:06 +00:00
2017-03-18 15:23:47 +00:00
/**
* merges the supplied options with the ones from npmextra.json
*/
dataFor<IToolConfig>(toolnameArg: string, defaultOptionsArg: any): IToolConfig {
let npmextraToolOptions
if (this.npmextraJsonData[ toolnameArg ]) {
npmextraToolOptions = this.npmextraJsonData[ toolnameArg ]
} else {
npmextraToolOptions = {}
2016-09-24 14:44:48 +00:00
}
2017-07-12 15:13:29 +00:00
let mergedOptions = plugins.smartlodash.merge({}, defaultOptionsArg, npmextraToolOptions)
2017-03-18 15:23:47 +00:00
return mergedOptions
}
2016-09-24 14:44:48 +00:00
2017-03-18 15:23:47 +00:00
/**
* checks if the JSON exists
*/
private checkNpmextraJsonExists() {
this.npmextraJsonExists = plugins.smartfile.fs.fileExistsSync(this.lookupPath)
}
2016-09-23 20:13:06 +00:00
2017-03-18 15:23:47 +00:00
/**
* gets lookupPath
*/
private checkLookupPath() {
if (this.cwd) {
this.lookupPath = plugins.path.join(this.cwd, 'npmextra.json')
} else {
this.lookupPath = paths.configFile
};
}
2016-09-24 14:44:48 +00:00
2017-03-18 15:23:47 +00:00
/**
* get npmextraJsonData
*/
private checkNpmextraJsonData() {
if (this.npmextraJsonExists) {
this.npmextraJsonData = plugins.smartfile.fs.toObjectSync(this.lookupPath)
} else {
this.npmextraJsonData = {}
2016-09-23 20:13:06 +00:00
}
2017-03-18 15:23:47 +00:00
}
2016-09-23 20:13:06 +00:00
}