intent-cli
Version:
A fully functional CLI built with TypeScript and modern tools
93 lines • 3.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = exports.config = void 0;
const conf_1 = __importDefault(require("conf"));
const logger_1 = require("./logger");
class ConfigManager {
constructor() {
this.config = new conf_1.default({
projectName: 'intent-cli',
projectVersion: '1.0.0',
defaults: {
apiEndpoint: 'https://api.example.com',
timeout: 30000,
retries: 3,
theme: 'auto',
output: 'table',
},
});
this.migrateConfig();
}
migrateConfig() {
const currentVersion = this.config.get('configVersion');
const latestVersion = '1.0.0';
if (currentVersion !== latestVersion) {
logger_1.logger.info('Migrating configuration to latest version...');
// Add migration logic here if needed
this.config.set('configVersion', latestVersion);
logger_1.logger.info('Configuration migration completed.');
}
}
get(key) {
return this.config.get(key);
}
set(key, value) {
this.config.set(key, value);
logger_1.logger.debug(`Configuration updated: ${String(key)} = ${JSON.stringify(value)}`);
}
getAll() {
return this.config.store;
}
clear() {
this.config.clear();
// Ensure the underlying store is truly empty (avoid Conf re-applying defaults)
try {
this.config.store = {};
}
catch (e) {
// fall back silently if store isn't writable
}
logger_1.logger.info('Configuration cleared.');
}
reset() {
this.config.clear();
this.migrateConfig();
logger_1.logger.info('Configuration reset to defaults.');
}
delete(key) {
this.config.delete(key);
logger_1.logger.debug(`Configuration key deleted: ${String(key)}`);
}
has(key) {
return this.config.has(key);
}
validate() {
const errors = [];
const config = this.getAll();
if (config.apiEndpoint && typeof config.apiEndpoint !== 'string') {
errors.push('apiEndpoint must be a string');
}
if (config.timeout && (typeof config.timeout !== 'number' || config.timeout <= 0)) {
errors.push('timeout must be a positive number');
}
if (config.retries && (typeof config.retries !== 'number' || config.retries < 0)) {
errors.push('retries must be a non-negative number');
}
if (config.theme && !['light', 'dark', 'auto'].includes(config.theme)) {
errors.push('theme must be one of: light, dark, auto');
}
if (config.output && !['json', 'table', 'yaml'].includes(config.output)) {
errors.push('output must be one of: json, table, yaml');
}
return {
isValid: errors.length === 0,
errors,
};
}
}
exports.ConfigManager = ConfigManager;
exports.config = new ConfigManager();
//# sourceMappingURL=config.js.map