rename package from @push.rocks/npmextra to @push.rocks/smartconfig
- Rename all source files from npmextra.* to simpler names (classes.appdata.ts, etc.) - Rename Npmextra class to Smartconfig - Config file changed from npmextra.json to smartconfig.json - KV store path changed from ~/.npmextra/kv to ~/.smartconfig/kv - Update all imports, tests, and metadata
This commit is contained in:
79
ts/classes.smartconfig.ts
Normal file
79
ts/classes.smartconfig.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
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 = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user