All files / backend/dist/config app-config.js

0% Statements 0/54
0% Branches 0/3
0% Functions 0/32
0% Lines 0/54

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                                                                                                                                                                                                                                                                                                                                                                                                             
/**
 * 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 } 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 {
    config;
    configPath;
    configFile;
    constructor(userDataPath, configFileName = '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
     */
    loadConfig() {
        let loadedConfig = {};
        try {
            if (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 = {
            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() {
        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() {
        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() {
        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, workspaceKey, sitePath) {
        this.config.lastOpenedSite = { siteKey, workspaceKey, sitePath };
    }
    setLastOpenedPublishTargetForSite(siteKey, publishKey) {
        this.config.lastOpenedPublishTargetForSite[siteKey] = publishKey;
    }
    setPrefKey(prefKey, prefValue) {
        this.config.prefs[prefKey] = prefValue;
    }
    setCurrentUsername(username) {
        this.config.currentUsername = username;
    }
    setSkipWelcomeScreen(skip) {
        this.config.skipWelcomeScreen = skip;
    }
    setExperimentalFeatures(toggle) {
        this.config.experimentalFeatures = toggle;
    }
    setDisablePartialCache(toggle) {
        this.config.disablePartialCache = toggle;
    }
    setDevLocalApi(toggle) {
        this.config.devLocalApi = toggle;
    }
    setDevDisableAutoHugoServe(toggle) {
        this.config.devDisableAutoHugoServe = toggle;
    }
    setHugoServeDraftMode(toggle) {
        this.config.hugoServeDraftMode = toggle;
    }
    setDevShowCurrentUser(toggle) {
        this.config.devShowCurrentUser = toggle;
    }
    setSitesListingView(view) {
        this.config.sitesListingView = view;
    }
    // ============================================================================
    // Generic accessors for readConfKey compatibility
    // ============================================================================
    /**
     * Get a config value by key (for backward compatibility with readConfKey API)
     */
    get(key) {
        return this.config[key];
    }
    /**
     * Set a config value by key
     */
    set(key, value) {
        this.config[key] = value;
    }
    /**
     * Get the entire config object (for debugging/serialization)
     */
    getAll() {
        return { ...this.config };
    }
}