Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | /**
* UnifiedConfigService - High-level configuration management
*
* Provides a unified interface for all configuration operations,
* combining storage, resolution, and migration capabilities.
*/
import path from 'path';
import os from 'os';
import { ConfigStore } from './config-store.js';
import { EnvOverrideLayer } from './env-override-layer.js';
import { ConfigResolver } from './config-resolver.js';
/**
* UnifiedConfigService provides the main API for configuration management
*/
export class UnifiedConfigService {
store;
envLayer;
resolver;
initialized = false;
constructor(options = {}) {
const configDir = options.configDir || path.join(os.homedir(), '.config', 'quiqr');
const userId = options.userId || 'ELECTRON';
this.store = new ConfigStore(configDir);
this.envLayer = new EnvOverrideLayer();
this.resolver = new ConfigResolver(this.store, this.envLayer);
// Auto-initialize synchronously
// Note: This uses sync file operations for initial setup
this.initializeSync(userId);
}
/**
* Initialize the service synchronously
* Used for auto-initialization in constructor
*/
initializeSync(userId = 'ELECTRON') {
if (this.initialized)
return;
this.store.ensureConfigDirSync();
this.envLayer.loadFromEnvironment();
this.resolver.initializeSync(userId);
this.initialized = true;
}
/**
* Initialize the service (async version for re-initialization)
*/
async initialize(userId = 'ELECTRON') {
if (this.initialized)
return;
await this.store.ensureConfigDir();
this.envLayer.loadFromEnvironment();
await this.resolver.initialize(userId);
this.initialized = true;
}
/**
* Ensure the service is initialized
*/
ensureInitialized() {
if (!this.initialized) {
throw new Error('UnifiedConfigService not initialized. Call initialize() first.');
}
}
// ============================================================
// User Preference Methods
// ============================================================
/**
* Get the effective value of a user preference
* This resolves through 2 layers: user preferences → app defaults
*/
getEffectivePreference(key) {
this.ensureInitialized();
return this.resolver.getEffectivePreference(key);
}
/**
* Get all effective preferences as a merged object
*/
getEffectivePreferences() {
this.ensureInitialized();
return this.resolver.getEffectivePreferences();
}
/**
* Get a preference with full resolution metadata
*/
resolvePreference(key) {
this.ensureInitialized();
return this.resolver.resolvePreference(key);
}
/**
* Set a user preference
*/
async setUserPreference(key, value) {
this.ensureInitialized();
await this.resolver.saveUserPreference(key, value);
}
/**
* Set multiple user preferences at once
*/
async setUserPreferences(prefs) {
this.ensureInitialized();
const userConfig = this.resolver.getUserConfig();
if (!userConfig) {
throw new Error('User config not loaded');
}
const updatedConfig = {
...userConfig,
preferences: {
...userConfig.preferences,
...prefs,
},
};
await this.store.writeUserConfig(updatedConfig, this.resolver.getUserId());
await this.resolver.reload();
}
// ============================================================
// User Config Methods (non-preference user data)
// ============================================================
/**
* Get the last opened site for the current user
*/
getLastOpenedSite() {
this.ensureInitialized();
const userConfig = this.resolver.getUserConfig();
return userConfig?.lastOpenedSite || { siteKey: null, workspaceKey: null, sitePath: null };
}
/**
* Set the last opened site
*/
async setLastOpenedSite(siteKey, workspaceKey, sitePath) {
this.ensureInitialized();
const userConfig = this.resolver.getUserConfig();
if (!userConfig) {
throw new Error('User config not loaded');
}
const updatedConfig = {
...userConfig,
lastOpenedSite: { siteKey, workspaceKey, sitePath },
};
await this.store.writeUserConfig(updatedConfig, this.resolver.getUserId());
await this.resolver.reload();
}
/**
* Get the skip welcome screen setting
*/
getSkipWelcomeScreen() {
this.ensureInitialized();
return this.resolver.getUserConfig()?.skipWelcomeScreen || false;
}
/**
* Set the skip welcome screen setting
*/
async setSkipWelcomeScreen(skip) {
this.ensureInitialized();
const userConfig = this.resolver.getUserConfig();
if (!userConfig) {
throw new Error('User config not loaded');
}
const updatedConfig = {
...userConfig,
skipWelcomeScreen: skip,
};
await this.store.writeUserConfig(updatedConfig, this.resolver.getUserId());
await this.resolver.reload();
}
/**
* Get last opened publish target for a site
*/
getLastPublishTarget(siteKey) {
this.ensureInitialized();
const targets = this.resolver.getUserConfig()?.lastOpenedPublishTargetForSite || {};
return targets[siteKey] || null;
}
/**
* Set last opened publish target for a site
*/
async setLastPublishTarget(siteKey, publishKey) {
this.ensureInitialized();
const userConfig = this.resolver.getUserConfig();
if (!userConfig) {
throw new Error('User config not loaded');
}
const updatedConfig = {
...userConfig,
lastOpenedPublishTargetForSite: {
...userConfig.lastOpenedPublishTargetForSite,
[siteKey]: publishKey,
},
};
await this.store.writeUserConfig(updatedConfig, this.resolver.getUserId());
await this.resolver.reload();
}
/**
* Generic: Get a user state field (non-preference user data)
*/
getUserState(key) {
this.ensureInitialized();
return this.resolver.getUserState(key);
}
/**
* Generic: Set a user state field (non-preference user data)
*/
async setUserState(key, value) {
this.ensureInitialized();
await this.resolver.saveUserState(key, value);
}
// ============================================================
// Instance Settings Methods
// ============================================================
/**
* Get instance settings
*/
getInstanceSettings() {
this.ensureInitialized();
return this.resolver.getInstanceSettings();
}
/**
* Get an instance setting value by path (dot notation)
*/
getInstanceSetting(path) {
this.ensureInitialized();
return this.resolver.getEffectiveInstanceSetting(path);
}
/**
* Check if experimental features are enabled
*/
isExperimentalFeaturesEnabled() {
this.ensureInitialized();
return this.resolver.getInstanceSettings()?.experimentalFeatures || false;
}
/**
* Update instance settings (admin operation)
*/
async updateInstanceSettings(updates) {
this.ensureInitialized();
const current = this.resolver.getInstanceSettings();
if (!current) {
throw new Error('Instance settings not loaded');
}
const updated = {
...current,
...(updates.experimentalFeatures !== undefined ? { experimentalFeatures: updates.experimentalFeatures } : {}),
storage: {
...current.storage,
...(updates.storage || {}),
},
git: {
...current.git,
...(updates.git || {}),
},
logging: {
...current.logging,
...(updates.logging || {}),
},
dev: {
...current.dev,
...(updates.dev || {}),
},
hugo: {
...current.hugo,
...(updates.hugo || {}),
},
};
await this.store.writeInstanceSettings(updated);
await this.resolver.reload();
}
// ============================================================
// Site Settings Methods
// ============================================================
/**
* Get settings for a site
*/
async getSiteSettings(siteKey) {
this.ensureInitialized();
return this.store.readSiteSettings(siteKey);
}
/**
* Update settings for a site (FUTURE - not currently used in simplified architecture)
*/
async updateSiteSettings(siteKey, updates) {
this.ensureInitialized();
const current = await this.store.readSiteSettings(siteKey);
if (!current) {
throw new Error(`Site settings not found for: ${siteKey}`);
}
const updated = {
...current,
settings: {
...(current.settings || {}),
...updates,
},
};
await this.store.writeSiteSettings(updated);
}
/**
* List all sites with settings
*/
async listSitesWithSettings() {
this.ensureInitialized();
return this.store.listSiteConfigs();
}
// ============================================================
// Property Inspection (about:config style)
// ============================================================
/**
* Get all configuration properties with metadata
* Useful for debugging and advanced configuration UI
*/
async getAllPropertyMetadata() {
this.ensureInitialized();
return this.resolver.getAllPropertyMetadata();
}
// ============================================================
// User Management (for multi-user scenarios)
// ============================================================
/**
* Get the current user ID
*/
getCurrentUserId() {
this.ensureInitialized();
return this.resolver.getUserId();
}
/**
* Switch to a different user
*/
async switchUser(userId) {
this.ensureInitialized();
await this.resolver.setUserId(userId);
}
/**
* List all user configs
*/
async listUsers() {
this.ensureInitialized();
return this.store.listUserConfigs();
}
// ============================================================
// Low-level Access
// ============================================================
/**
* Get the config store (for migration purposes)
*/
getStore() {
return this.store;
}
/**
* Get the environment override layer
*/
getEnvLayer() {
return this.envLayer;
}
/**
* Get the config directory path
*/
getConfigDir() {
return this.store.getConfigDir();
}
/**
* Force reload all configuration
*/
async reload() {
this.ensureInitialized();
await this.resolver.reload();
}
}
/**
* Create a UnifiedConfigService instance
*/
export function createUnifiedConfigService(options = {}) {
return new UnifiedConfigService(options);
}
|