@xec-sh/cli
Version:
Xec: The Universal Shell for TypeScript
140 lines • 4.26 kB
JavaScript
import { ConfigurationManager } from '../config/configuration-manager.js';
import { VariableInterpolator } from '../config/variable-interpolator.js';
export class ConfigAPI {
constructor(options = {}) {
this.options = options;
this.loaded = false;
this.manager = new ConfigurationManager();
this.interpolator = new VariableInterpolator();
}
async load() {
if (this.loaded) {
return;
}
await this.manager.load();
this.loaded = true;
if (this.options.overrides) {
for (const [path, value] of Object.entries(this.options.overrides)) {
this.set(path, value);
}
}
}
get(path) {
this.ensureLoaded();
const parts = path.split('.');
let current = this.manager.getConfig();
for (const part of parts) {
if (current && typeof current === 'object' && part in current) {
current = current[part];
}
else {
return undefined;
}
}
return current;
}
set(path, value) {
this.ensureLoaded();
const parts = path.split('.');
const config = this.manager.getConfig();
let current = config;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!part)
continue;
if (!current[part] || typeof current[part] !== 'object') {
current[part] = {};
}
current = current[part];
}
const lastPart = parts[parts.length - 1];
if (lastPart) {
current[lastPart] = value;
}
}
unset(path) {
this.ensureLoaded();
const parts = path.split('.');
const config = this.manager.getConfig();
let current = config;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!part || !current[part]) {
return;
}
current = current[part];
}
const lastPart = parts[parts.length - 1];
if (lastPart) {
delete current[lastPart];
}
}
async save(path) {
this.ensureLoaded();
await this.manager.save(path);
}
async useProfile(name) {
await this.manager.useProfile(name);
this.interpolator = new VariableInterpolator();
}
getProfile() {
return this.manager.getCurrentProfile();
}
listProfiles() {
this.ensureLoaded();
const profiles = this.get('profiles');
return profiles ? Object.keys(profiles) : [];
}
async resolveTarget(ref) {
this.ensureLoaded();
const resolver = this.manager.getTargetResolver();
return resolver.resolve(ref);
}
interpolate(template, context = {}) {
this.ensureLoaded();
const config = this.manager.getConfig();
const env = {};
for (const [key, value] of Object.entries(process.env)) {
if (value !== undefined) {
env[key] = value;
}
}
const fullContext = {
vars: config.vars || {},
env,
...context
};
return this.interpolator.interpolate(template, fullContext);
}
getAll() {
this.ensureLoaded();
return this.manager.getConfig();
}
hasFeature(feature) {
const features = this.get('features') || [];
return features.includes(feature);
}
getVersion() {
return this.get('version') || '1.0';
}
async validate() {
this.ensureLoaded();
const errors = await this.manager.validate();
return errors.map(err => `${err.path}: ${err.message}`);
}
async reload() {
this.loaded = false;
this.manager = new ConfigurationManager({
...this.options,
projectRoot: process.cwd()
});
await this.load();
}
ensureLoaded() {
if (!this.loaded) {
throw new Error('Configuration not loaded. Call config.load() first.');
}
}
}
export const config = new ConfigAPI();
//# sourceMappingURL=config-api.js.map