kist
Version:
Package Pipeline Processor
187 lines (186 loc) • 8.53 kB
JavaScript
;
// ============================================================================
// Imports
// ============================================================================
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;
};
})();
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SvgPackagerAction = void 0;
const fs = __importStar(require("fs/promises"));
const glob = __importStar(require("glob"));
const path = __importStar(require("path"));
const svgo_1 = __importStar(require("svgo"));
const Action_1 = require("../../core/pipeline/Action");
// ============================================================================
// Classes
// ============================================================================
/**
* SvgPackagerAction is responsible for optimizing and packaging SVG files.
* It reads SVG files from a specified directory, optimizes them using SVGO,
* and then outputs them as TypeScript files and JSON indexes.
*/
class SvgPackagerAction extends Action_1.Action {
// Methods
// ========================================================================
/**
* Executes the SVG processing action.
* @param options - The options specifying input and output directories.
* @returns A Promise that resolves when the action completes successfully.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const { svgoConfigPath = "./config/svgo.config.js", inputDirectory = "src/icons", outputDirectory = "dist/icons", tsOutputDirectory = "dist/ts", jsonOutputDirectory = "dist", } = options;
this.logInfo(`Processing SVG files from ${inputDirectory}...`);
const iconNames = [];
try {
const svgFiles = glob.sync(`${inputDirectory}/**/*.svg`);
for (const file of svgFiles) {
const iconName = this.sanitizeFileName(path.basename(file, ".svg"));
iconNames.push(iconName);
const svgContent = yield this.readSvgFile(file);
const optimizedSvg = yield this.optimizeSvg(svgoConfigPath, svgContent);
yield this.writeFiles(iconName, optimizedSvg, outputDirectory, tsOutputDirectory);
}
yield this.writeIconsJson(iconNames, jsonOutputDirectory);
this.logInfo(`Successfully processed ${svgFiles.length} SVG files.`);
}
catch (error) {
this.logError("Error processing SVG files.", error);
throw error;
}
});
}
/**
* Reads the content of an SVG file.
* @param filePath The path to the SVG file.
* @returns The content of the SVG file.
*/
readSvgFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
return fs.readFile(filePath, "utf8");
});
}
/**
* Sanitizes a file name to be a valid TypeScript identifier.
* @param fileName The original file name.
* @returns A sanitized version of the file name.
*/
sanitizeFileName(fileName) {
return fileName.replace(/[^a-zA-Z0-9_]/g, "_");
}
/**
* Optimizes SVG content using SVGO.
* @param svgoConfigPath Path to the SVGO configuration file.
* @param svgContent The raw SVG content.
* @returns The optimized SVG content.
*/
optimizeSvg(svgoConfigPath, svgContent) {
return __awaiter(this, void 0, void 0, function* () {
const config = yield (0, svgo_1.loadConfig)(svgoConfigPath);
const result = yield svgo_1.default.optimize(svgContent, Object.assign({}, config));
return result.data.trim();
});
}
/**
* Writes optimized SVG content to files (SVG & TypeScript).
* @param iconName The name of the icon.
* @param svgContent The optimized SVG content.
* @param outputDirectory The directory for SVG output.
* @param tsOutputDirectory The directory for TypeScript output.
*/
writeFiles(iconName, svgContent, outputDirectory, tsOutputDirectory) {
return __awaiter(this, void 0, void 0, function* () {
yield this.writeSvgFile(iconName, svgContent, outputDirectory);
yield this.writeTypeScriptFile(iconName, svgContent, tsOutputDirectory);
});
}
/**
* Writes the optimized SVG content to an output file.
* @param iconName The name of the icon.
* @param svgContent The SVG content to be written.
* @param outputDirectory The directory to output the SVG file.
*/
writeSvgFile(iconName, svgContent, outputDirectory) {
return __awaiter(this, void 0, void 0, function* () {
const outputPath = path.join(outputDirectory, `${iconName}.svg`);
yield fs.writeFile(outputPath, svgContent);
});
}
/**
* Creates a TypeScript file from the optimized SVG content.
* @param iconName The name of the icon.
* @param svgContent The optimized SVG content.
* @param tsOutputDirectory The directory for TypeScript output.
*/
writeTypeScriptFile(iconName, svgContent, tsOutputDirectory) {
return __awaiter(this, void 0, void 0, function* () {
const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`;
const outputPath = path.join(tsOutputDirectory, `${iconName}.ts`);
yield fs.writeFile(outputPath, tsContent);
});
}
/**
* Writes a JSON file containing the names of processed icons.
* @param iconNames An array of processed icon names.
* @param jsonOutputDirectory The directory to output the JSON file.
*/
writeIconsJson(iconNames, jsonOutputDirectory) {
return __awaiter(this, void 0, void 0, function* () {
const jsonContent = JSON.stringify(iconNames, null, 2);
const outputPath = path.join(jsonOutputDirectory, "icons.json");
yield fs.writeFile(outputPath, jsonContent);
});
}
/**
* Provides a description of the action.
* @returns A string description of the action.
*/
describe() {
return "Optimizes and packages SVG files into multiple formats (SVG, TypeScript, JSON).";
}
}
exports.SvgPackagerAction = SvgPackagerAction;
// ============================================================================
// Export
// ============================================================================
exports.default = SvgPackagerAction;