Two cleanups in one pass for the v1.14.0 'works on any platform' theme: 1. Response helpers — merged src/utils/responses.js and the root-level response-helpers.js into a single module at src/utils/responses.js. The old module had a richer set (created, noContent, validationError, unauthorized, forbidden, notFound, conflict) and is now re-exported from the new location. Updated 15 routes to import from src/utils/responses and deleted the root response-helpers.js. 2. Error logger — error-handler.js now uses the unified src/utils/logging.js#logError (same one src/app.js uses), so all errors go to one log file with one rotation policy. Removed the dead asyncHandler export (the real one is in src/utils/async-handler.js and is used everywhere). Deleted the legacy error-logger.js. Both are invisible to users — same HTTP response shapes, same log file path, same error format. Internal-only refactor.
82 lines
2.5 KiB
JavaScript
82 lines
2.5 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { success } = require('../src/utils/responses');
|
|
const { ValidationError, NotFoundError } = require('../errors');
|
|
const platformPaths = require('../platform-paths');
|
|
|
|
/**
|
|
* Themes routes factory
|
|
* @param {Object} deps - Explicit dependencies
|
|
* @param {Function} deps.asyncHandler - Async route handler wrapper
|
|
* @param {Object} deps.log - Logger instance
|
|
* @returns {express.Router}
|
|
*/
|
|
module.exports = function({ asyncHandler, log }) {
|
|
const router = express.Router();
|
|
const THEMES_DIR = process.env.THEMES_DIR || path.join(path.dirname(platformPaths.servicesFile), '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) {
|
|
log.error('themes', 'Failed to read themes', { error: 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', asyncHandler(async (req, res) => {
|
|
const { slug } = req.params;
|
|
const { name, colors, lightBg } = req.body;
|
|
|
|
if (!slug || !name || !colors) {
|
|
throw new ValidationError('Missing slug, name, or colors');
|
|
}
|
|
|
|
if (!/^[a-z0-9-]+$/.test(slug)) {
|
|
throw new ValidationError('Invalid slug format (use lowercase letters, numbers, and hyphens only)', 'slug');
|
|
}
|
|
|
|
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', asyncHandler(async (req, res) => {
|
|
const { slug } = req.params;
|
|
const filePath = path.join(THEMES_DIR, slug + '.json');
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
throw new NotFoundError(`Theme ${slug}`);
|
|
}
|
|
|
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
const name = data.name || slug;
|
|
fs.unlinkSync(filePath);
|
|
|
|
success(res, { message: name + ' theme deleted' });
|
|
}));
|
|
|
|
return router;
|
|
};
|