npmextra/ts/npmextra.classes.npmextra.ts

73 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2023-08-03 17:22:34 +00:00
import * as plugins from './npmextra.plugins.js';
import * as paths from './npmextra.paths.js';
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 {
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
*/
constructor(cwdArg?: string) {
2017-03-18 15:23:47 +00:00
if (cwdArg) {
this.cwd = cwdArg;
2017-03-18 15:23:47 +00:00
} else {
this.cwd = paths.cwd;
2016-09-24 14:44:48 +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 {
let npmextraToolOptions;
if (this.npmextraJsonData[toolnameArg]) {
npmextraToolOptions = this.npmextraJsonData[toolnameArg];
2017-03-18 15:23:47 +00:00
} else {
npmextraToolOptions = {};
2016-09-24 14:44:48 +00:00
}
2019-05-10 15:03:07 +00:00
let mergedOptions = {
...defaultOptionsArg,
2021-01-27 21:00:49 +00:00
...npmextraToolOptions,
2019-05-10 15:03:07 +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() {
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) {
this.lookupPath = plugins.path.join(this.cwd, 'npmextra.json');
2017-03-18 15:23:47 +00:00
} else {
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) {
this.npmextraJsonData = plugins.smartfile.fs.toObjectSync(this.lookupPath);
2017-03-18 15:23:47 +00:00
} 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
}