UNPKG

@usefui/svgjsx

Version:

Open Source Command Line Interfaces to generate JSX Icon Components from SVGs.

98 lines 3.95 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.appendFile = exports.writeFile = exports.readSvgFile = exports.findSvgFiles = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const result_1 = require("../utils/result"); const isNotSvgFile = (filePath) => path.extname(filePath).toLowerCase() !== ".svg"; const isNotAccessibleFile = (filePath) => { try { fs.accessSync(filePath, fs.constants.R_OK); return false; } catch { return true; } }; const isEmptyFile = (content) => content.trim().length === 0; const isInvalidSvgContent = (content) => !content.includes("<svg") || !content.includes("</svg>"); const walkDirectorySync = (dirPath) => { const result = []; const walk = (currentPath) => { try { const items = fs.readdirSync(currentPath); for (const item of items) { const itemPath = path.join(currentPath, item); const stat = fs.statSync(itemPath); if (stat.isDirectory()) { walk(itemPath); } else if (!isNotSvgFile(itemPath) && !isNotAccessibleFile(itemPath)) { result.push(itemPath); } } } catch (error) { console.warn(`⚠️ Cannot read directory: ${currentPath}`); } }; walk(dirPath); return result; }; const findSvgFiles = (sourceDir) => (0, result_1.tryCatch)(() => walkDirectorySync(sourceDir)); exports.findSvgFiles = findSvgFiles; const readSvgFile = (filePath) => { if (isNotAccessibleFile(filePath)) { return (0, result_1.failure)(new Error(`Cannot read file: ${filePath}`)); } return (0, result_1.tryCatch)(() => { const content = fs.readFileSync(filePath, "utf8"); const name = path.basename(filePath); if (isEmptyFile(content)) { throw new Error(`File is empty: ${filePath}`); } if (isInvalidSvgContent(content)) { throw new Error(`File does not contain valid SVG content: ${filePath}`); } return { path: filePath, name, content }; }); }; exports.readSvgFile = readSvgFile; const writeFile = (filePath, content) => (0, result_1.tryCatch)(() => fs.writeFileSync(filePath, content, "utf8")); exports.writeFile = writeFile; const appendFile = (filePath, content) => (0, result_1.tryCatch)(() => fs.appendFileSync(filePath, content, "utf8")); exports.appendFile = appendFile; //# sourceMappingURL=file-operations.js.map