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 | 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.
*/
/**
* App-level defaults (hardcoded fallbacks)
*/
const APP_DEFAULT_INSTANCE = {
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',
};
/**
* ConfigResolver resolves configuration values through layers
*/
export class ConfigResolver {
store;
envLayer;
instanceSettings = null;
userConfig = null;
currentUserId = 'ELECTRON';
constructor(store, envLayer) {
this.store = store;
this.envLayer = envLayer;
}
/**
* Initialize the resolver by loading configs
*/
async initialize(userId = 'ELECTRON') {
this.currentUserId = userId;
await this.reload();
}
/**
* Reload all configuration from files
*/
async reload() {
this.instanceSettings = await this.store.readInstanceSettings();
this.userConfig = await this.store.readUserConfig(this.currentUserId);
}
/**
* Initialize the resolver synchronously
*/
initializeSync(userId = 'ELECTRON') {
this.currentUserId = userId;
this.reloadSync();
}
/**
* Reload all configuration synchronously
*/
reloadSync() {
this.instanceSettings = this.store.readInstanceSettingsSync();
this.userConfig = this.store.readUserConfigSync(this.currentUserId);
}
/**
* Get the current user ID
*/
getUserId() {
return this.currentUserId;
}
/**
* Set the current user ID and reload their config
*/
async setUserId(userId) {
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) {
const path = `user.preferences.${key}`;
// Check user preferences
const userPrefs = this.userConfig?.preferences || {};
if (key in userPrefs && userPrefs[key] !== undefined) {
return {
value: userPrefs[key],
source: 'user',
path,
};
}
// Fall back to app defaults
const appDefault = APP_DEFAULT_USER_PREFS[key];
return {
value: appDefault,
source: 'app-default',
path,
};
}
/**
* Get the effective value of a preference (without metadata)
*/
getEffectivePreference(key) {
return this.resolvePreference(key).value;
}
/**
* Get all effective preferences merged
*/
getEffectivePreferences() {
const result = { ...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) {
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);
if (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) {
return this.resolveInstanceSetting(path).value;
}
/**
* Get nested value from object using dot notation
*/
getNestedValue(obj, path) {
if (!obj || typeof obj !== 'object')
return undefined;
const parts = path.split('.');
let current = obj;
for (const part of parts) {
if (current && typeof current === 'object' && part in current) {
current = current[part];
}
else {
return undefined;
}
}
return current;
}
/**
* Get metadata for all configuration properties
* This enables the "about:config" style inspection
*/
async getAllPropertyMetadata() {
const metadata = [];
// 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,
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,
description: `Instance setting: ${path}`,
});
}
return metadata;
}
/**
* Get the raw instance settings (for direct access)
*/
getInstanceSettings() {
return this.instanceSettings;
}
/**
* Get the raw user config (for direct access)
*/
getUserConfig() {
return this.userConfig;
}
/**
* Get user state (non-preference fields from user config)
*/
getUserState(key) {
return this.userConfig?.[key];
}
/**
* Save user preference
*/
async saveUserPreference(key, value) {
if (!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(key, value) {
if (!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);
}
}
|