UNPKG

@wttp/site

Version:

Web3 Transfer Protocol (WTTP) - Site Contracts and deployment tools

153 lines 5.04 kB
import fs from "fs"; import path from "path"; // Default ignore patterns that should always be applied const DEFAULT_IGNORE_PATTERNS = [ ".git/", ".git/**", ".env", ".env.*", ".DS_Store", "Thumbs.db", "*.log", ".npm/", ".yarn/", ".cache/", ".temp/", ".tmp/", "*.swp", "*.swo", "*~", ".vscode/", ".idea/", "*.wttpignore" ]; export class WTTPIgnore { constructor(baseDir, options = {}) { this.patterns = []; this.baseDir = path.resolve(baseDir); // Always include default patterns unless explicitly disabled if (options.includeDefaults !== false) { this.patterns.push(...DEFAULT_IGNORE_PATTERNS); } // Add custom patterns if provided if (options.customPatterns) { this.patterns.push(...options.customPatterns); } // Load .wttpignore file if it exists this.loadIgnoreFile(); } /** * Load patterns from .wttpignore file */ loadIgnoreFile() { const ignoreFilePath = path.join(this.baseDir, ".wttpignore"); if (fs.existsSync(ignoreFilePath)) { try { const content = fs.readFileSync(ignoreFilePath, "utf-8"); const filePatterns = this.parseIgnoreFile(content); this.patterns.push(...filePatterns); console.log(`📋 Loaded ${filePatterns.length} patterns from .wttpignore`); } catch (error) { console.warn(`⚠️ Warning: Could not read .wttpignore file: ${error}`); } } } /** * Parse .wttpignore file content into patterns */ parseIgnoreFile(content) { return content .split(/\r?\n/) .map(line => line.trim()) .filter(line => line.length > 0 && !line.startsWith("#")) .map(line => { // Handle negation patterns (starting with !) if (line.startsWith("!")) { return line; // Keep negation as-is for now } return line; }); } /** * Check if a file/directory should be ignored */ shouldIgnore(filePath) { const relativePath = path.relative(this.baseDir, path.resolve(filePath)); const normalizedPath = relativePath.replace(/\\/g, "/"); // Don't ignore if the path goes outside the base directory if (normalizedPath.startsWith("../")) { return false; } let ignored = false; for (const pattern of this.patterns) { if (pattern.startsWith("!")) { // Negation pattern - if it matches, don't ignore const negatedPattern = pattern.slice(1); if (this.matchPattern(normalizedPath, negatedPattern)) { ignored = false; } } else { // Regular ignore pattern if (this.matchPattern(normalizedPath, pattern)) { ignored = true; } } } return ignored; } /** * Match a file path against a pattern (supports wildcards and directory patterns) */ matchPattern(filePath, pattern) { // Handle directory patterns (ending with /) if (pattern.endsWith("/")) { const dirPattern = pattern.slice(0, -1); // Check if the path starts with this directory return filePath === dirPattern || filePath.startsWith(dirPattern + "/"); } // Handle patterns with /** if (pattern.includes("/**")) { const basePattern = pattern.replace("/**", ""); return filePath === basePattern || filePath.startsWith(basePattern + "/"); } // Handle simple wildcards if (pattern.includes("*")) { const regexPattern = pattern .replace(/\./g, "\\.") .replace(/\*/g, "[^/]*") .replace(/\?/g, "[^/]"); const regex = new RegExp(`^${regexPattern}$`); return regex.test(filePath) || regex.test(path.basename(filePath)); } // Exact match or basename match return filePath === pattern || path.basename(filePath) === pattern; } /** * Get all patterns currently loaded */ getPatterns() { return [...this.patterns]; } /** * Filter an array of file paths, removing ignored ones */ filterPaths(paths) { return paths.filter(filePath => !this.shouldIgnore(filePath)); } } /** * Create a WTTPIgnore instance for a directory */ export function createWTTPIgnore(baseDir, options) { return new WTTPIgnore(baseDir, options); } /** * Quick function to check if a single file should be ignored */ export function shouldIgnoreFile(filePath, baseDir, options) { const ignore = new WTTPIgnore(baseDir, options); return ignore.shouldIgnore(filePath); } //# sourceMappingURL=wttpIgnore.js.map