Files
smartconfig/ts/classes.smartconfig.ts

80 lines
1.7 KiB
TypeScript

import * as plugins from './plugins.js';
import * as paths from './paths.js';
/**
* Smartconfig class allows easy configuration of tools
*/
export class Smartconfig {
cwd: string;
lookupPath: string;
smartconfigJsonExists: boolean;
smartconfigJsonData: any;
/**
* creates instance of Smartconfig
*/
constructor(cwdArg?: string) {
if (cwdArg) {
this.cwd = cwdArg;
} else {
this.cwd = paths.cwd;
}
this.checkLookupPath();
this.checkSmartconfigJsonExists();
this.checkSmartconfigJsonData();
}
/**
* merges the supplied options with the ones from smartconfig.json
*/
dataFor<IToolConfig>(
toolnameArg: string,
defaultOptionsArg: any,
): IToolConfig {
let smartconfigToolOptions;
if (this.smartconfigJsonData[toolnameArg]) {
smartconfigToolOptions = this.smartconfigJsonData[toolnameArg];
} else {
smartconfigToolOptions = {};
}
let mergedOptions = {
...defaultOptionsArg,
...smartconfigToolOptions,
};
return mergedOptions;
}
/**
* checks if the JSON exists
*/
private checkSmartconfigJsonExists() {
this.smartconfigJsonExists = plugins.smartfile.fs.fileExistsSync(
this.lookupPath,
);
}
/**
* gets lookupPath
*/
private checkLookupPath() {
if (this.cwd) {
this.lookupPath = plugins.path.join(this.cwd, '.smartconfig.json');
} else {
this.lookupPath = paths.configFile;
}
}
/**
* get smartconfigJsonData
*/
private checkSmartconfigJsonData() {
if (this.smartconfigJsonExists) {
this.smartconfigJsonData = plugins.smartfile.fs.toObjectSync(
this.lookupPath,
);
} else {
this.smartconfigJsonData = {};
}
}
}