UNPKG

@quasarbright/projection

Version:

A static site generator that creates a beautiful, interactive gallery to showcase your coding projects. Features search, filtering, tags, responsive design, and an admin UI.

157 lines 6.06 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigLoader = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const config_1 = require("../types/config"); const errors_1 = require("../utils/errors"); /** * Handles loading, merging, and validating configuration from various sources */ class ConfigLoader { constructor(cwd = process.cwd()) { this.cwd = cwd; } /** * Load configuration from: * 1. Explicit configPath option * 2. projection.config.json in cwd * 3. Default configuration */ async load(options = {}) { let config = {}; // Priority 1: Explicit config path if (options.configPath) { config = await this.loadFromFile(options.configPath); } else { // Priority 2: projection.config.json const jsonConfigPath = path.join(this.cwd, 'projection.config.json'); if (fs.existsSync(jsonConfigPath)) { config = await this.loadFromFile(jsonConfigPath); } } // Merge with defaults const mergedConfig = this.mergeWithDefaults(config); // Validate this.validate(mergedConfig); return mergedConfig; } /** * Load configuration from a JSON file */ async loadFromFile(filePath) { const absolutePath = path.isAbsolute(filePath) ? filePath : path.join(this.cwd, filePath); if (!fs.existsSync(absolutePath)) { throw new errors_1.ProjectionError(`Configuration file not found: ${filePath}`, errors_1.ErrorCodes.FILE_NOT_FOUND, { path: absolutePath }); } try { const ext = path.extname(absolutePath); if (ext === '.json') { // Load JSON config const content = fs.readFileSync(absolutePath, 'utf-8'); return JSON.parse(content); } else { throw new errors_1.ProjectionError(`Unsupported config file format: ${ext}. Only .json is supported.`, errors_1.ErrorCodes.CONFIG_ERROR, { path: absolutePath, supportedFormats: ['.json'] }); } } catch (error) { if (error instanceof errors_1.ProjectionError) { throw error; } throw new errors_1.ProjectionError(`Failed to parse configuration file: ${filePath}`, errors_1.ErrorCodes.PARSE_ERROR, { path: absolutePath, originalError: error.message }); } } /** * Merge user config with defaults */ mergeWithDefaults(userConfig) { return { ...config_1.DEFAULT_CONFIG, ...userConfig }; } /** * Validate configuration and throw errors for invalid values */ validate(config) { const errors = []; // Validate required fields if (!config.title || typeof config.title !== 'string' || config.title.trim() === '') { errors.push('title must be a non-empty string'); } if (!config.description || typeof config.description !== 'string') { errors.push('description must be a string'); } if (!config.baseUrl || typeof config.baseUrl !== 'string') { errors.push('baseUrl must be a string'); } // Validate optional fields if (config.dynamicBackgrounds !== undefined) { if (!Array.isArray(config.dynamicBackgrounds)) { errors.push('dynamicBackgrounds must be an array'); } else { config.dynamicBackgrounds.forEach((bg, index) => { if (typeof bg !== 'string') { errors.push(`dynamicBackgrounds[${index}] must be a string`); } }); } } if (config.customStyles !== undefined && typeof config.customStyles !== 'string') { errors.push('customStyles must be a string'); } if (config.customScripts !== undefined && typeof config.customScripts !== 'string') { errors.push('customScripts must be a string'); } if (config.output !== undefined && typeof config.output !== 'string') { errors.push('output must be a string'); } if (errors.length > 0) { throw new errors_1.ProjectionError('Invalid configuration', errors_1.ErrorCodes.CONFIG_ERROR, { errors }); } } /** * Get default configuration */ static getDefaults() { return { ...config_1.DEFAULT_CONFIG }; } } exports.ConfigLoader = ConfigLoader; //# sourceMappingURL=config.js.map