- credentials.js (2 deps: credentialManager, asyncHandler) - backups.js (2 deps: backupManager, asyncHandler) - license.js (2 deps: licenseManager, asyncHandler) - errorlogs.js (3 deps: ERROR_LOG_FILE, auditLogger, asyncHandler) - themes.js (0 deps! Standalone route) Total: 9 routes refactored so far
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { success } = require('../response-helpers');
|
|
|
|
/**
|
|
* Themes routes factory
|
|
* Note: This route does not use asyncHandler - uses synchronous fs operations
|
|
* @returns {express.Router}
|
|
*/
|
|
module.exports = function() {
|
|
const router = express.Router();
|
|
const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(process.env.SERVICES_FILE || '/app/services.json'), 'themes');
|
|
|
|
// Ensure themes directory exists
|
|
if (!fs.existsSync(THEMES_DIR)) {
|
|
fs.mkdirSync(THEMES_DIR, { recursive: true });
|
|
}
|
|
|
|
function readAllThemes() {
|
|
const themes = {};
|
|
try {
|
|
const files = fs.readdirSync(THEMES_DIR).filter(f => f.endsWith('.json'));
|
|
for (const file of files) {
|
|
const slug = path.basename(file, '.json');
|
|
const data = JSON.parse(fs.readFileSync(path.join(THEMES_DIR, file), 'utf8'));
|
|
themes[slug] = data;
|
|
}
|
|
} catch (e) {
|
|
console.error('[Themes] Failed to read themes:', e.message);
|
|
}
|
|
return themes;
|
|
}
|
|
|
|
// Get all user themes
|
|
router.get('/themes', (req, res) => {
|
|
success(res, { themes: readAllThemes() });
|
|
});
|
|
|
|
// Save a theme (create or update)
|
|
router.post('/themes/:slug', (req, res) => {
|
|
const { slug } = req.params;
|
|
const { name, colors, lightBg } = req.body;
|
|
|
|
if (!slug || !name || !colors) {
|
|
return res.status(400).json({ success: false, error: 'Missing slug, name, or colors' });
|
|
}
|
|
|
|
if (!/^[a-z0-9-]+$/.test(slug)) {
|
|
return res.status(400).json({ success: false, error: 'Invalid slug format' });
|
|
}
|
|
|
|
const themeData = { name, ...colors };
|
|
if (lightBg) themeData.lightBg = true;
|
|
fs.writeFileSync(path.join(THEMES_DIR, slug + '.json'), JSON.stringify(themeData, null, 2), 'utf8');
|
|
|
|
success(res, { message: name + ' theme saved' });
|
|
});
|
|
|
|
// Delete a theme
|
|
router.delete('/themes/:slug', (req, res) => {
|
|
const { slug } = req.params;
|
|
const filePath = path.join(THEMES_DIR, slug + '.json');
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
return res.status(404).json({ success: false, error: 'Theme not found' });
|
|
}
|
|
|
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
const name = data.name || slug;
|
|
fs.unlinkSync(filePath);
|
|
|
|
success(res, { message: name + ' theme deleted' });
|
|
});
|
|
|
|
return router;
|
|
};
|