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 | 4x 4x 35x 35x 35x 35x 35x 6x 6x 35x 35x 35x 35x 5x 8x 8x 8x 5x 3x 3x 4x 2x 2x 2x 2x 42x 42x 1x 1x 41x 41x 41x 42x 41x 41x 41x 41x 81x 81x 41x 3x 8x 4x 5x 5x 5x 1x 1x 1x | /**
* ConfigResolver - Simplified 2-layer configuration resolution
*
* Resolves configuration values through 2 layers:
* 1. App Defaults (lowest priority) - hardcoded in source
* 2. User Preferences (highest priority) - from user_prefs_ELECTRON.json
*
* Instance settings are read directly without layering.
* Environment variables can override instance settings.
*/
import type { InstanceSettings, UserConfig, ConfigLayer, ConfigPropertyMetadata } from '@quiqr/types';
import { ConfigStore } from './config-store.js';
import { EnvOverrideLayer } from './env-override-layer.js';
/**
* App-level defaults (hardcoded fallbacks)
*/
const APP_DEFAULT_INSTANCE: InstanceSettings = {
storage: {
type: 'fs',
dataFolder: '~/Quiqr',
},
git: {
binaryPath: undefined,
},
logging: {
retention: 30,
logRetentionDays: 30,
logLevel: 'info',
},
experimentalFeatures: false,
dev: {
localApi: false,
showCurrentUser: false,
disablePartialCache: false,
},
hugo: {
serveDraftMode: false,
disableAutoHugoServe: false,
},
};
const APP_DEFAULT_USER_PREFS = {
interfaceStyle: 'quiqr10-light' as const,
};
/**
* Result of a resolved value, including metadata about its source
*/
export interface ResolvedValue<T> {
value: T;
source: ConfigLayer;
path: string;
}
/**
* ConfigResolver resolves configuration values through layers
*/
export class ConfigResolver {
private store: ConfigStore;
private envLayer: EnvOverrideLayer;
private instanceSettings: InstanceSettings | null = null;
private userConfig: UserConfig | null = null;
private currentUserId: string = 'ELECTRON';
constructor(store: ConfigStore, envLayer: EnvOverrideLayer) {
this.store = store;
this.envLayer = envLayer;
}
/**
* Initialize the resolver by loading configs
*/
async initialize(userId: string = 'ELECTRON'): Promise<void> {
this.currentUserId = userId;
await this.reload();
}
/**
* Reload all configuration from files
*/
async reload(): Promise<void> {
this.instanceSettings = await this.store.readInstanceSettings();
this.userConfig = await this.store.readUserConfig(this.currentUserId);
}
/**
* Initialize the resolver synchronously
*/
initializeSync(userId: string = 'ELECTRON'): void {
this.currentUserId = userId;
this.reloadSync();
}
/**
* Reload all configuration synchronously
*/
reloadSync(): void {
this.instanceSettings = this.store.readInstanceSettingsSync();
this.userConfig = this.store.readUserConfigSync(this.currentUserId);
}
/**
* Get the current user ID
*/
getUserId(): string {
return this.currentUserId;
}
/**
* Set the current user ID and reload their config
*/
async setUserId(userId: string): Promise<void> {
this.currentUserId = userId;
this.userConfig = await this.store.readUserConfig(userId);
}
/**
* Resolve a user preference through 2 layers
*
* Resolution order (highest to lowest priority):
* 1. User preferences (from user_prefs_ELECTRON.json)
* 2. App defaults (hardcoded)
*/
resolvePreference(key: string): ResolvedValue<unknown> {
const path = `user.preferences.${key}`;
// Check user preferences
const userPrefs = this.userConfig?.preferences || {};
if (key in userPrefs && userPrefs[key as keyof typeof userPrefs] !== undefined) {
return {
value: userPrefs[key as keyof typeof userPrefs],
source: 'user',
path,
};
}
// Fall back to app defaults
const appDefault = APP_DEFAULT_USER_PREFS[key as keyof typeof APP_DEFAULT_USER_PREFS];
return {
value: appDefault,
source: 'app-default',
path,
};
}
/**
* Get the effective value of a preference (without metadata)
*/
getEffectivePreference(key: string): unknown {
return this.resolvePreference(key).value;
}
/**
* Get all effective preferences merged
*/
getEffectivePreferences(): Record<string, unknown> {
const result: Record<string, unknown> = { ...APP_DEFAULT_USER_PREFS };
// Merge user preferences
const userPrefs = this.userConfig?.preferences || {};
Object.assign(result, userPrefs);
return result;
}
/**
* Resolve an instance setting with environment override support
*/
resolveInstanceSetting(path: string): ResolvedValue<unknown> {
const configPath = path;
// Check environment override
if (this.envLayer.hasOverride(configPath)) {
const override = this.envLayer.getOverride(configPath);
return {
value: override?.value,
source: 'user', // Environment is user-level override
path: configPath,
};
}
// Get value from instance settings
const value = this.getNestedValue(this.instanceSettings, path);
Eif (value !== undefined) {
return {
value,
source: 'user',
path: configPath,
};
}
// Fall back to app default
const defaultValue = this.getNestedValue(APP_DEFAULT_INSTANCE, path);
return {
value: defaultValue,
source: 'app-default',
path: configPath,
};
}
/**
* Get effective instance setting value
*/
getEffectiveInstanceSetting(path: string): unknown {
return this.resolveInstanceSetting(path).value;
}
/**
* Get nested value from object using dot notation
*/
private getNestedValue(obj: unknown, path: string): unknown {
Iif (!obj || typeof obj !== 'object') return undefined;
const parts = path.split('.');
let current: any = obj;
for (const part of parts) {
if (current && typeof current === 'object' && part in current) {
current = current[part];
} else E{
return undefined;
}
}
return current;
}
/**
* Get metadata for all configuration properties
* This enables the "about:config" style inspection
*/
async getAllPropertyMetadata(): Promise<ConfigPropertyMetadata[]> {
const metadata: ConfigPropertyMetadata[] = [];
// Add user preferences
const prefKeys = Object.keys(APP_DEFAULT_USER_PREFS);
for (const key of prefKeys) {
const resolved = this.resolvePreference(key);
metadata.push({
path: resolved.path,
value: resolved.value,
source: resolved.source,
type: typeof resolved.value as 'string' | 'number' | 'boolean' | 'object',
description: `User preference: ${key}`,
});
}
// Add instance settings
const instancePaths = [
'storage.type',
'storage.dataFolder',
'logging.logRetentionDays',
'logging.logLevel',
'experimentalFeatures',
'dev.localApi',
'dev.showCurrentUser',
'dev.disablePartialCache',
'hugo.serveDraftMode',
'hugo.disableAutoHugoServe',
];
for (const path of instancePaths) {
const resolved = this.resolveInstanceSetting(path);
metadata.push({
path: `instance.${path}`,
value: resolved.value,
source: resolved.source,
type: typeof resolved.value as 'string' | 'number' | 'boolean' | 'object',
description: `Instance setting: ${path}`,
});
}
return metadata;
}
/**
* Get the raw instance settings (for direct access)
*/
getInstanceSettings(): InstanceSettings | null {
return this.instanceSettings;
}
/**
* Get the raw user config (for direct access)
*/
getUserConfig(): UserConfig | null {
return this.userConfig;
}
/**
* Get user state (non-preference fields from user config)
*/
getUserState<K extends keyof UserConfig>(key: K): UserConfig[K] | undefined {
return this.userConfig?.[key];
}
/**
* Save user preference
*/
async saveUserPreference(key: string, value: unknown): Promise<void> {
Iif (!this.userConfig) {
throw new Error('User config not loaded');
}
// Update in-memory config
this.userConfig.preferences = {
...this.userConfig.preferences,
[key]: value,
};
// Save to file
await this.store.saveUserConfig(this.currentUserId, this.userConfig);
}
/**
* Save user state field
*/
async saveUserState<K extends keyof UserConfig>(key: K, value: UserConfig[K]): Promise<void> {
Iif (!this.userConfig) {
throw new Error('User config not loaded');
}
// Update in-memory config
this.userConfig[key] = value;
// Save to file
await this.store.saveUserConfig(this.currentUserId, this.userConfig);
}
}
|