UNPKG

ucbuilder

Version:

:Shree Ganeshay Namah: new way app design

127 lines 4.79 kB
// import fs from "fs"; // import path from "path"; export class UcDependencyScanner { path; fs; constructor(path, fs) { this.path = path; this.fs = fs; } // ----------------------------- // MASTER REGEX (named groups) // ----------------------------- static PATTERN = new RegExp([ // x-from String.raw `(?<xfrom>x-from)\s*=\s*"(?<xfrom_path>.*?)"`, // JS import String.raw `(?<js_import>import\s+[\s\S]*?)\s+from\s+["'](?<js_path>.*?)["'];`, // SCSS @use / @import String.raw `(?<scss_type>@use|@import)\s+["'](?<scss_path>.*?)["'];`, // {: path } String.raw `(?<bracket_open>{:)(?<bracket_path>.*?)}`, ].join("|"), "gi"); // ------------------------------------------------------ // NEW: Iterate each match and return IUcDepEntry in callback // ------------------------------------------------------ ReplaceAll(content, baseDir, callback) { let match; content = content.replace(UcDependencyScanner.PATTERN, (m, g) => { console.log(arguments); return m; if (g.xfrom_path) { return callback(this._buildEntry(baseDir, g.xfrom_path, "x-from", match[0])); } if (g.js_path) { return callback(this._buildEntry(baseDir, g.js_path, "import", match[0])); } if (g.scss_path && g.scss_type) { return callback(this._buildEntry(baseDir, g.scss_path, g.scss_type, match[0])); } if (g.bracket_path) { return callback(this._buildEntry(baseDir, g.bracket_path, "{:", match[0])); } return m; }); return content; } // ------------------------------------------------------ // NEW: Iterate each match and return IUcDepEntry in callback // ------------------------------------------------------ forEachMatch(content, baseDir, callback) { let match; while ((match = UcDependencyScanner.PATTERN.exec(content)) !== null) { const g = match.groups; if (g.xfrom_path) { callback(this._buildEntry(baseDir, g.xfrom_path, "x-from", match[0])); } if (g.js_path) { callback(this._buildEntry(baseDir, g.js_path, "import", match[0])); } if (g.scss_path && g.scss_type) { callback(this._buildEntry(baseDir, g.scss_path, g.scss_type, match[0])); } if (g.bracket_path) { callback(this._buildEntry(baseDir, g.bracket_path, "{:", match[0])); } } } // ----------------------------- // Scan single file // ----------------------------- scan(filePath) { const absFile = this.path.resolve(filePath); const baseDir = this.path.dirname(absFile); if (!this.fs.existsSync(absFile)) { throw new Error(`File not found: ${absFile}`); } const content = this.fs.readFileSync(absFile, "utf8"); const results = []; let match; while ((match = UcDependencyScanner.PATTERN.exec(content)) !== null) { const g = match.groups; if (g.xfrom_path) { results.push(this._buildEntry(baseDir, g.xfrom_path, "x-from", match[0])); } if (g.js_path) { results.push(this._buildEntry(baseDir, g.js_path, "import", match[0])); } if (g.scss_path && g.scss_type) { results.push(this._buildEntry(baseDir, g.scss_path, g.scss_type, match[0])); } if (g.bracket_path) { results.push(this._buildEntry(baseDir, g.bracket_path, "{:", match[0])); } } return results; } // ----------------------------- // Recursively scan entire tree // ----------------------------- scanTree(entryFile, visited = new Set()) { const abs = this.path.resolve(entryFile); if (visited.has(abs)) return []; visited.add(abs); const firstLevel = this.scan(abs); let tree = [...firstLevel]; for (const dep of firstLevel) { if (this.fs.existsSync(dep.abs)) { tree = tree.concat(this.scanTree(dep.abs, visited)); } } return tree; } // ----------------------------- // Build typed entry // ----------------------------- _buildEntry(baseDir, raw, type, match) { const cleaned = raw.trim(); return { type, match, raw: cleaned, abs: this.path.resolve(baseDir, cleaned) }; } } //# sourceMappingURL=fileStates.js.map