UNPKG

kt-extendscript-builder

Version:

Vite based builder for transpile TypeScript to ExtendScript

153 lines (152 loc) 6.54 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; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PonyfillCollector = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const basePonyfills_1 = require("../ponyfills/basePonyfills"); class PonyfillCollector { /** * Collects ponyfills from specified paths and search locations * @param userPonyfillsPaths Path(s) to custom ponyfill files * @param searchPaths Path(s) to search for ponyfill files * @returns Combined list of base and custom ponyfills * @throws Error if loading or searching fails */ collect(userPonyfillsPaths, searchPaths) { let collectedPonyfills = [...basePonyfills_1.basePonyfills]; if (userPonyfillsPaths) { const paths = Array.isArray(userPonyfillsPaths) ? userPonyfillsPaths : [userPonyfillsPaths]; for (const ponyfillsPath of paths) { collectedPonyfills = this.mergePonyfills(collectedPonyfills, this.loadPonyfills(ponyfillsPath)); } } if (searchPaths && searchPaths.length > 0) { const paths = Array.isArray(searchPaths) ? searchPaths : [searchPaths]; const foundPaths = this.searchPonyfillFiles(paths); for (const ponyfillPath of foundPaths) { collectedPonyfills = this.mergePonyfills(collectedPonyfills, this.loadPonyfills(ponyfillPath)); } } return collectedPonyfills; } /** * Loads ponyfills from a file * @param ponyfillsPath Path to ponyfills file * @returns Array of ponyfill items * @throws Error if file cannot be loaded or parsed */ loadPonyfills(ponyfillsPath) { if (!ponyfillsPath) return []; const absolutePath = path_1.default.resolve(process.cwd(), ponyfillsPath); // Siempre relativo a cwd try { const ponyfillModule = Promise.resolve(`${absolutePath}`).then(s => __importStar(require(s))); // Puede lanzar error si falla return this.selectPonyfills(ponyfillModule); } catch (error) { throw new Error(`Error loading ponyfills from ${ponyfillsPath}: ${error}`); } } /** * Searches recursively for ponyfill files in multiple paths * @param searchPaths Directories to search * @returns Array of ponyfill file paths * @throws Error if directory cannot be read */ searchPonyfillFiles(searchPaths) { const collectedPaths = []; for (const searchPath of searchPaths) { this.searchDirectory(searchPath, collectedPaths); } return collectedPaths; } searchDirectory(currentPath, store) { const files = fs_1.default.readdirSync(currentPath); for (const file of files) { const filePath = path_1.default.join(currentPath, file); const stat = fs_1.default.statSync(filePath); const isPonyfill = file.toLowerCase().includes('ponyfill'); const isTypeScript = file.endsWith('.ts'); if (stat.isDirectory()) { this.searchDirectory(filePath, store); } else if (stat.isFile() && isPonyfill && isTypeScript) { store.push(filePath); } } } /** * Selects valid ponyfills from a module * @param ponyfills Module containing ponyfills * @returns Array of valid ponyfill items */ selectPonyfills(ponyfills) { if (Array.isArray(ponyfills)) return this.validatePonyfills(ponyfills); if ((ponyfills === null || ponyfills === void 0 ? void 0 : ponyfills.default) && Array.isArray(ponyfills.default)) return this.validatePonyfills(ponyfills.default); const foundPonyfills = []; for (const key in ponyfills) { if (key.toLowerCase().includes('ponyfills') && Array.isArray(ponyfills[key])) { foundPonyfills.push(...this.validatePonyfills(ponyfills[key])); } } return foundPonyfills; } /** * Validates ponyfill items * @param ponyfills Array of potential ponyfill items * @returns Array of valid ponyfill items */ validatePonyfills(ponyfills) { return ponyfills.filter((ponyfill) => (ponyfill === null || ponyfill === void 0 ? void 0 : ponyfill.find) && (ponyfill === null || ponyfill === void 0 ? void 0 : ponyfill.replace) && (ponyfill === null || ponyfill === void 0 ? void 0 : ponyfill.inject)); } /** * Merges ponyfills, avoiding duplicates based on find property * @param existing Existing ponyfills * @param newPonyfills New ponyfills to add * @returns Merged array of ponyfills */ mergePonyfills(existing, newPonyfills) { const seen = new Set(existing.map((p) => p.find)); const uniqueNew = newPonyfills.filter((p) => !seen.has(p.find)); return [...existing, ...uniqueNew]; } } exports.PonyfillCollector = PonyfillCollector;