pixel-forge
Version:
A comprehensive generator for social media previews, favicons, and visual assets across all platforms
126 lines (125 loc) • 4.18 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigValidator = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
class ConfigValidator {
constructor(config) {
this.errors = [];
this.config = config;
}
/**
* Validate the entire configuration
*/
async validate() {
this.errors = [];
// Validate required fields
this.validateRequired();
// Validate colors
this.validateColors();
// Validate output configuration
await this.validateOutput();
// Validate social preview configuration
if (this.config.socialPreview) {
this.validateSocialPreview();
}
return {
isValid: this.errors.length === 0,
errors: this.errors
};
}
/**
* Validate required fields
*/
validateRequired() {
if (!this.config.appName) {
this.errors.push('appName is required');
}
if (!this.config.output?.path) {
this.errors.push('output.path is required');
}
}
/**
* Validate color formats
*/
validateColors() {
const colorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
if (this.config.themeColor && !colorRegex.test(this.config.themeColor)) {
this.errors.push('themeColor must be a valid hex color (e.g., #ff0000)');
}
if (this.config.backgroundColor && !colorRegex.test(this.config.backgroundColor)) {
this.errors.push('backgroundColor must be a valid hex color (e.g., #ff0000)');
}
}
/**
* Validate output configuration
*/
async validateOutput() {
const { output } = this.config;
// Validate quality
if (output.quality !== undefined) {
if (output.quality < 1 || output.quality > 100) {
this.errors.push('output.quality must be between 1 and 100');
}
}
// Validate format
if (output.format && !['png', 'jpeg', 'webp'].includes(output.format)) {
this.errors.push('output.format must be one of: png, jpeg, webp');
}
// Validate output path
try {
const outputPath = path_1.default.resolve(output.path);
await fs_1.promises.access(path_1.default.dirname(outputPath));
}
catch (_error) {
this.errors.push(`Output directory ${output.path} is not accessible`);
}
// Validate prefix
if (output.prefix) {
if (!output.prefix.startsWith('/')) {
this.errors.push('output.prefix must start with /');
}
}
}
/**
* Validate social preview configuration
*/
validateSocialPreview() {
const { socialPreview } = this.config;
// Early return if socialPreview is undefined
if (!socialPreview)
return;
if (socialPreview.template && !['basic', 'gradient', 'custom'].includes(socialPreview.template)) {
this.errors.push('socialPreview.template must be one of: basic, gradient, custom');
}
// Validate text lengths
if (socialPreview.title && socialPreview.title.length > 100) {
this.errors.push('socialPreview.title must be less than 100 characters');
}
if (socialPreview.description && socialPreview.description.length > 200) {
this.errors.push('socialPreview.description must be less than 200 characters');
}
}
/**
* Get default configuration
*/
static getDefaultConfig() {
return {
platforms: {
social: true,
favicon: true,
pwa: true,
apple: true,
android: true,
windows: true
},
socialPreview: {
template: 'basic'
}
};
}
}
exports.ConfigValidator = ConfigValidator;