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 | 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x | /**
* Application Configuration Manager
*
* Replaces global.pogoconf with a proper class-based configuration manager.
* Handles loading, saving, and accessing application configuration and preferences.
*/
import path from 'path';
import fs from 'fs-extra';
import { appConfigSchema, type AppConfig as AppConfigType } from '@quiqr/types';
/**
* AppConfig manages the application's persistent configuration
* Loads from and saves to a JSON file in the user data directory
*/
export class AppConfig {
private config: AppConfigType;
private configPath: string;
private configFile: string;
constructor(userDataPath: string, configFileName: string = 'quiqr-app-config.json') {
this.configPath = userDataPath;
this.configFile = path.join(userDataPath, configFileName);
this.config = this.loadConfig();
}
/**
* Load configuration from file, or create default if not exists
*/
private loadConfig(): AppConfigType {
let loadedConfig: Partial<AppConfigType> = {};
try {
Iif (fs.existsSync(this.configFile)) {
const fileContent = fs.readFileSync(this.configFile, 'utf8');
loadedConfig = JSON.parse(fileContent);
}
} catch (err) {
console.warn('Could not load config file, using defaults:', err);
}
// Merge with defaults
const defaultConfig: AppConfigType = {
lastOpenedSite: { siteKey: null, workspaceKey: null, sitePath: null },
prefs: {
dataFolder: '~/Quiqr',
interfaceStyle: 'quiqr10-light',
logRetentionDays: 30, // Default retention period
},
lastOpenedPublishTargetForSite: {},
skipWelcomeScreen: false,
experimentalFeatures: false,
disablePartialCache: false,
devLocalApi: false,
devDisableAutoHugoServe: false,
hugoServeDraftMode: false,
devShowCurrentUser: false,
sitesListingView: 'all',
currentUsername: null,
};
const mergedConfig = { ...defaultConfig, ...loadedConfig };
// Validate with Zod schema
try {
return appConfigSchema.parse(mergedConfig);
} catch (err) {
console.warn('Config validation failed, using defaults:', err);
return defaultConfig;
}
}
/**
* Save configuration to file
*/
async save(): Promise<void> {
try {
await fs.ensureDir(path.dirname(this.configFile));
await fs.writeFile(this.configFile, JSON.stringify(this.config, null, 2), 'utf8');
} catch (err) {
console.error('Could not save config:', err);
throw err;
}
}
/**
* Synchronous save (for use in non-async contexts)
*/
saveSync(): void {
try {
fs.ensureDirSync(path.dirname(this.configFile));
fs.writeFileSync(this.configFile, JSON.stringify(this.config, null, 2), 'utf8');
} catch (err) {
console.error('Could not save config:', err);
throw err;
}
}
/**
* Reset state to defaults
*/
resetStateToDefault(): void {
this.config.lastOpenedSite = { siteKey: null, workspaceKey: null, sitePath: null };
this.config.lastOpenedPublishTargetForSite = {};
}
// ============================================================================
// Getters
// ============================================================================
get lastOpenedSite() {
return this.config.lastOpenedSite;
}
get lastOpenedPublishTargetForSite() {
return this.config.lastOpenedPublishTargetForSite;
}
get prefs() {
return this.config.prefs;
}
get currentUsername() {
return this.config.currentUsername;
}
get skipWelcomeScreen() {
return this.config.skipWelcomeScreen;
}
get experimentalFeatures() {
return this.config.experimentalFeatures;
}
get disablePartialCache() {
return this.config.disablePartialCache;
}
get devLocalApi() {
return this.config.devLocalApi;
}
get devDisableAutoHugoServe() {
return this.config.devDisableAutoHugoServe;
}
get hugoServeDraftMode() {
return this.config.hugoServeDraftMode;
}
get devShowCurrentUser() {
return this.config.devShowCurrentUser;
}
get sitesListingView() {
return this.config.sitesListingView;
}
// ============================================================================
// Setters
// ============================================================================
setLastOpenedSite(siteKey: string | null, workspaceKey: string | null, sitePath: string | null): void {
this.config.lastOpenedSite = { siteKey, workspaceKey, sitePath };
}
setLastOpenedPublishTargetForSite(siteKey: string, publishKey: string): void {
this.config.lastOpenedPublishTargetForSite[siteKey] = publishKey;
}
setPrefKey(prefKey: string, prefValue: string | boolean): void {
this.config.prefs[prefKey] = prefValue;
}
setCurrentUsername(username: string | null): void {
this.config.currentUsername = username;
}
setSkipWelcomeScreen(skip: boolean): void {
this.config.skipWelcomeScreen = skip;
}
setExperimentalFeatures(toggle: boolean): void {
this.config.experimentalFeatures = toggle;
}
setDisablePartialCache(toggle: boolean): void {
this.config.disablePartialCache = toggle;
}
setDevLocalApi(toggle: boolean): void {
this.config.devLocalApi = toggle;
}
setDevDisableAutoHugoServe(toggle: boolean): void {
this.config.devDisableAutoHugoServe = toggle;
}
setHugoServeDraftMode(toggle: boolean): void {
this.config.hugoServeDraftMode = toggle;
}
setDevShowCurrentUser(toggle: boolean): void {
this.config.devShowCurrentUser = toggle;
}
setSitesListingView(view: string): void {
this.config.sitesListingView = view;
}
// ============================================================================
// Generic accessors for readConfKey compatibility
// ============================================================================
/**
* Get a config value by key (for backward compatibility with readConfKey API)
*/
get<K extends keyof AppConfigType>(key: K): AppConfigType[K] {
return this.config[key];
}
/**
* Set a config value by key
*/
set<K extends keyof AppConfigType>(key: K, value: AppConfigType[K]): void {
this.config[key] = value;
}
/**
* Get the entire config object (for debugging/serialization)
*/
getAll(): AppConfigType {
return { ...this.config };
}
}
|