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 | 1x 1x 1x | /**
* ConfigStore - File-based configuration storage
*
* Provides low-level file operations for configuration files.
* Handles reading, writing, and watching config files in ~/.config/quiqr/
*/
import path from 'path';
import fs from 'fs-extra';
import { instanceSettingsSchema, userConfigSchema, siteSettingsSchema } from '@quiqr/types';
/**
* Default instance settings
*/
const DEFAULT_INSTANCE_SETTINGS = {
storage: { type: 'fs', dataFolder: '~/Quiqr' },
git: { binaryPath: undefined },
logging: { logRetentionDays: 30, retention: 30, logLevel: 'info' },
experimentalFeatures: false,
dev: { localApi: false, showCurrentUser: false, disablePartialCache: false },
hugo: { serveDraftMode: false, disableAutoHugoServe: false },
};
/**
* Default user config factory
* For Electron edition, userId is 'ELECTRON'
* Empty preferences allows ConfigResolver to properly fall back to app defaults
*/
const getDefaultUserConfig = (userId) => ({
userId,
preferences: {}, // Empty - app defaults handled by ConfigResolver
lastOpenedSite: { siteKey: null, workspaceKey: null, sitePath: null },
lastOpenedPublishTargetForSite: {},
skipWelcomeScreen: false,
sitesListingView: 'all',
});
/**
* Default site settings factory
*/
const getDefaultSiteSettings = (siteKey) => ({
siteKey,
settings: {},
});
/**
* ConfigStore manages individual configuration files
*/
export class ConfigStore {
configDir;
constructor(configDir) {
this.configDir = configDir;
}
/**
* Ensure the config directory exists
*/
async ensureConfigDir() {
await fs.ensureDir(this.configDir);
}
/**
* Ensure the config directory exists (synchronous)
*/
ensureConfigDirSync() {
fs.ensureDirSync(this.configDir);
}
/**
* Get the path to a config file
*/
getFilePath(type, identifier) {
switch (type) {
case 'instance':
return path.join(this.configDir, 'instance_settings.json');
case 'user':
// Default to 'ELECTRON' for Electron single-user edition
const userId = identifier || 'ELECTRON';
return path.join(this.configDir, `user_prefs_${userId}.json`);
case 'site':
if (!identifier)
throw new Error('Site key required for site config');
return path.join(this.configDir, `site_settings_${identifier}.json`);
default:
throw new Error(`Unknown config type: ${type}`);
}
}
/**
* Read instance settings
*/
async readInstanceSettings() {
const filePath = this.getFilePath('instance');
try {
if (await fs.pathExists(filePath)) {
const content = await fs.readFile(filePath, 'utf8');
const parsed = JSON.parse(content);
// Merge with defaults and validate
const merged = this.deepMerge(DEFAULT_INSTANCE_SETTINGS, parsed);
return instanceSettingsSchema.parse(merged);
}
}
catch (err) {
console.warn(`Failed to read instance settings:`, err);
}
return { ...DEFAULT_INSTANCE_SETTINGS };
}
/**
* Read instance settings (synchronous)
*/
readInstanceSettingsSync() {
const filePath = this.getFilePath('instance');
try {
if (fs.pathExistsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
const parsed = JSON.parse(content);
// Merge with defaults and validate
const merged = this.deepMerge(DEFAULT_INSTANCE_SETTINGS, parsed);
return instanceSettingsSchema.parse(merged);
}
}
catch (err) {
console.warn(`Failed to read instance settings:`, err);
}
return { ...DEFAULT_INSTANCE_SETTINGS };
}
/**
* Write instance settings
*/
async writeInstanceSettings(settings) {
await this.ensureConfigDir();
const filePath = this.getFilePath('instance');
await fs.writeFile(filePath, JSON.stringify(settings, null, 2), 'utf8');
}
/**
* Read user config
*/
async readUserConfig(userId = 'default') {
const filePath = this.getFilePath('user', userId);
const defaults = getDefaultUserConfig(userId);
try {
if (await fs.pathExists(filePath)) {
const content = await fs.readFile(filePath, 'utf8');
const parsed = JSON.parse(content);
// Merge with defaults and validate
const merged = this.deepMerge(defaults, parsed);
return userConfigSchema.parse(merged);
}
}
catch (err) {
console.warn(`Failed to read user config for ${userId}:`, err);
}
return defaults;
}
/**
* Read user config (synchronous)
*/
readUserConfigSync(userId = 'ELECTRON') {
const filePath = this.getFilePath('user', userId);
const defaults = getDefaultUserConfig(userId);
try {
if (fs.pathExistsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
const parsed = JSON.parse(content);
// Merge with defaults and validate
const merged = this.deepMerge(defaults, parsed);
return userConfigSchema.parse(merged);
}
}
catch (err) {
console.warn(`Failed to read user config for ${userId}:`, err);
}
return defaults;
}
/**
* Write user config
*/
async writeUserConfig(config, userId = 'ELECTRON') {
await this.ensureConfigDir();
const filePath = this.getFilePath('user', userId);
await fs.writeFile(filePath, JSON.stringify({ ...config, userId }, null, 2), 'utf8');
}
/**
* Save user config (alias for writeUserConfig)
*/
async saveUserConfig(userId, config) {
await this.writeUserConfig(config, userId);
}
/**
* Read site settings (FUTURE - not currently used in simplified architecture)
*/
async readSiteSettings(siteKey) {
const filePath = this.getFilePath('site', siteKey);
const defaults = getDefaultSiteSettings(siteKey);
try {
if (await fs.pathExists(filePath)) {
const content = await fs.readFile(filePath, 'utf8');
const parsed = JSON.parse(content);
// Merge with defaults and validate
const merged = this.deepMerge(defaults, parsed);
const validated = siteSettingsSchema?.parse(merged);
return validated || merged;
}
}
catch (err) {
console.warn(`Failed to read site settings for ${siteKey}:`, err);
}
return defaults;
}
/**
* Write site settings (FUTURE - not currently used in simplified architecture)
*/
async writeSiteSettings(settings) {
await this.ensureConfigDir();
const filePath = this.getFilePath('site', settings.siteKey);
await fs.writeFile(filePath, JSON.stringify(settings, null, 2), 'utf8');
}
/**
* Check if a config file exists
*/
async exists(type, identifier) {
const filePath = this.getFilePath(type, identifier);
return fs.pathExists(filePath);
}
/**
* Delete a config file
*/
async delete(type, identifier) {
const filePath = this.getFilePath(type, identifier);
if (await fs.pathExists(filePath)) {
await fs.remove(filePath);
}
}
/**
* List all user config files
*/
async listUserConfigs() {
await this.ensureConfigDir();
const files = await fs.readdir(this.configDir);
const userFiles = files.filter(f => f.startsWith('user_prefs_') && f.endsWith('.json'));
return userFiles.map(f => f.replace('user_prefs_', '').replace('.json', ''));
}
/**
* List all site config files
*/
async listSiteConfigs() {
await this.ensureConfigDir();
const files = await fs.readdir(this.configDir);
const siteFiles = files.filter(f => f.startsWith('site_settings_') && f.endsWith('.json'));
return siteFiles.map(f => f.replace('site_settings_', '').replace('.json', ''));
}
/**
* Get the config directory path
*/
getConfigDir() {
return this.configDir;
}
/**
* Deep merge two objects, with source overriding target
*/
deepMerge(target, source) {
const result = { ...target };
for (const key of Object.keys(source)) {
const sourceValue = source[key];
const targetValue = result[key];
if (sourceValue !== null &&
typeof sourceValue === 'object' &&
!Array.isArray(sourceValue) &&
targetValue !== null &&
typeof targetValue === 'object' &&
!Array.isArray(targetValue)) {
result[key] = this.deepMerge(targetValue, sourceValue);
}
else if (sourceValue !== undefined) {
result[key] = sourceValue;
}
}
return result;
}
}
|