UNPKG

kist

Version:

Package Pipeline Processor

115 lines (114 loc) 5.24 kB
"use strict"; // ============================================================================ // Imports // ============================================================================ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SvgSpriteAction = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const svg_sprite_1 = __importDefault(require("svg-sprite")); const Action_1 = require("../../core/pipeline/Action"); const svgsprite_config_js_1 = __importDefault(require("./svgsprite.config.js")); // ============================================================================ // Classes // ============================================================================ /** * SvgSpriteAction compiles multiple SVG files into a single sprite sheet, * making it more efficient to manage and use SVG assets in web applications. */ class SvgSpriteAction extends Action_1.Action { // Constructor // ======================================================================== /** * Constructs an instance with merged default and custom configurations. * @param {svgSprite.Config} customConfig - Optional custom configuration * for svg-sprite. */ constructor(customConfig = {}) { super(); this.config = Object.assign(Object.assign({}, SvgSpriteAction.defaultConfig), customConfig); } // Methods // ======================================================================== /** * Executes the SVG sprite generation process. * @param options - Options including source directory and output directory. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const { sourceDir, outputDir } = options; if (!sourceDir || !outputDir) { throw new Error("Both 'sourceDir' and 'outputDir' must be specified."); } this.logInfo(`Generating SVG sprite from: ${sourceDir}`); try { yield this.generateSprite(sourceDir, outputDir); this.logInfo(`SVG sprite successfully generated in: ${outputDir}`); } catch (error) { this.logError("Error generating SVG sprite:", error); throw error; } }); } /** * Generates an SVG sprite from all SVG files in the specified directory. * @param sourceDir - Directory containing source SVG files. * @param outputDir - Directory where the generated sprite will be saved. */ generateSprite(sourceDir, outputDir) { return __awaiter(this, void 0, void 0, function* () { const files = fs_1.default.readdirSync(sourceDir); const sprite = new svg_sprite_1.default(this.config); for (const file of files) { if (path_1.default.extname(file) === ".svg") { const svgPath = path_1.default.resolve(sourceDir, file); const content = fs_1.default.readFileSync(svgPath, "utf8"); sprite.add(svgPath, null, content); } } sprite.compile((error, result) => { if (error) { throw error; } for (const mode in result) { for (const resource in result[mode]) { const outputPath = path_1.default.resolve(outputDir, result[mode][resource].path); fs_1.default.mkdirSync(path_1.default.dirname(outputPath), { recursive: true, }); fs_1.default.writeFileSync(outputPath, result[mode][resource].contents); } } }); }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { return "Generates an SVG sprite from a directory of SVG files."; } } exports.SvgSpriteAction = SvgSpriteAction; /** * Default configuration for SVG sprite generation. */ SvgSpriteAction.defaultConfig = svgsprite_config_js_1.default; // ============================================================================ // Export // ============================================================================ exports.default = SvgSpriteAction;