| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import fs from 'fs-extra';
- import path from 'path';
- import { fileURLToPath } from 'url';
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = path.dirname(__filename);
- const CONFIG_FILE = path.join(__dirname, 'config.json');
- // Default config
- const DEFAULT_CONFIG = {
- activeTheme: 'default',
- postWidth: 'max-w-4xl'
- };
- // Initialize config file
- async function initializeConfig() {
- try {
- if (!(await fs.pathExists(CONFIG_FILE))) {
- await fs.writeJSON(CONFIG_FILE, DEFAULT_CONFIG, { spaces: 2 });
- console.log('⚙️ Initialized config system');
- }
- } catch (error) {
- console.error('Error initializing config:', error);
- }
- }
- // Load config data
- async function loadConfig() {
- try {
- if (await fs.pathExists(CONFIG_FILE)) {
- return await fs.readJSON(CONFIG_FILE);
- }
- return DEFAULT_CONFIG;
- } catch (error) {
- console.error('Error loading config:', error);
- return DEFAULT_CONFIG;
- }
- }
- // Save config data
- async function saveConfig(configData) {
- try {
- await fs.writeJSON(CONFIG_FILE, configData, { spaces: 2 });
- } catch (error) {
- console.error('Error saving config:', error);
- throw error;
- }
- }
- // Initialize on load
- await initializeConfig();
- // Config getters/setters
- export async function getConfig() {
- return await loadConfig();
- }
- export async function updateConfig(updates) {
- const currentConfig = await loadConfig();
- const newConfig = { ...currentConfig, ...updates };
- await saveConfig(newConfig);
- return newConfig;
- }
- export async function getActiveThemeId() {
- const config = await loadConfig();
- return config.activeTheme;
- }
- export async function setActiveThemeId(themeId) {
- return await updateConfig({ activeTheme: themeId });
- }
- export async function getPostWidth() {
- const config = await loadConfig();
- return config.postWidth;
- }
- export async function setPostWidth(width) {
- return await updateConfig({ postWidth: width });
- }
|