2018-08-30 23:11:09 +00:00
|
|
|
import * as plugins from './npmextra.plugins';
|
|
|
|
import * as paths from './npmextra.paths';
|
2016-09-23 20:13:06 +00:00
|
|
|
|
|
|
|
/**
|
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 {
|
2018-08-30 23:11:09 +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
|
|
|
|
*/
|
2018-08-30 23:11:09 +00:00
|
|
|
constructor(cwdArg?: string) {
|
2017-03-18 15:23:47 +00:00
|
|
|
if (cwdArg) {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.cwd = cwdArg;
|
2017-03-18 15:23:47 +00:00
|
|
|
} else {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.cwd = paths.cwd;
|
2016-09-24 14:44:48 +00:00
|
|
|
}
|
2018-08-30 23:11:09 +00:00
|
|
|
this.checkLookupPath();
|
|
|
|
this.checkNpmextraJsonExists();
|
|
|
|
this.checkNpmextraJsonData();
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
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 {
|
2018-08-30 23:11:09 +00:00
|
|
|
let npmextraToolOptions;
|
|
|
|
if (this.npmextraJsonData[toolnameArg]) {
|
|
|
|
npmextraToolOptions = this.npmextraJsonData[toolnameArg];
|
2017-03-18 15:23:47 +00:00
|
|
|
} else {
|
2018-08-30 23:11:09 +00:00
|
|
|
npmextraToolOptions = {};
|
2016-09-24 14:44:48 +00:00
|
|
|
}
|
2019-05-10 15:03:07 +00:00
|
|
|
let mergedOptions = {
|
|
|
|
...defaultOptionsArg,
|
|
|
|
...npmextraToolOptions
|
|
|
|
};
|
2018-08-30 23:11:09 +00:00
|
|
|
return mergedOptions;
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-09-24 14:44:48 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
|
|
|
* checks if the JSON exists
|
|
|
|
*/
|
|
|
|
private checkNpmextraJsonExists() {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.npmextraJsonExists = plugins.smartfile.fs.fileExistsSync(this.lookupPath);
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-09-23 20:13:06 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
|
|
|
* gets lookupPath
|
|
|
|
*/
|
|
|
|
private checkLookupPath() {
|
|
|
|
if (this.cwd) {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.lookupPath = plugins.path.join(this.cwd, 'npmextra.json');
|
2017-03-18 15:23:47 +00:00
|
|
|
} else {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.lookupPath = paths.configFile;
|
|
|
|
}
|
2017-03-18 15:23:47 +00:00
|
|
|
}
|
2016-09-24 14:44:48 +00:00
|
|
|
|
2017-03-18 15:23:47 +00:00
|
|
|
/**
|
|
|
|
* get npmextraJsonData
|
|
|
|
*/
|
|
|
|
private checkNpmextraJsonData() {
|
|
|
|
if (this.npmextraJsonExists) {
|
2018-08-30 23:11:09 +00:00
|
|
|
this.npmextraJsonData = plugins.smartfile.fs.toObjectSync(this.lookupPath);
|
2017-03-18 15:23:47 +00:00
|
|
|
} else {
|
2018-08-30 23:11:09 +00:00
|
|
|
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
|
|
|
}
|