|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import fs from 'fs/promises'; |
| 3 | +import { glob } from 'glob'; |
| 4 | +import { FIGMA_ROOT_URL } from './CONSTANTS.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Process a single file to fix Figma URLs with detailed debugging |
| 8 | + */ |
| 9 | +export async function processFigmaFile(filePath) { |
| 10 | + try { |
| 11 | + console.log(`\nProcessing file: ${filePath}`); |
| 12 | + const content = await fs.readFile(filePath, 'utf8'); |
| 13 | + let modified = false; |
| 14 | + |
| 15 | + // Find all figma.connect calls with string literals as second parameter |
| 16 | + // Updated regex to be more flexible with whitespace and formatting |
| 17 | + const figmaConnectRegex = /figma\.connect\(\s*[^,]+,\s*(['"])([^'"]+)\1/g; |
| 18 | + let match; |
| 19 | + let matchCount = 0; |
| 20 | + |
| 21 | + // Test the content for any figma.connect calls |
| 22 | + const hasConnect = content.includes('figma.connect'); |
| 23 | + console.log(`Contains figma.connect calls: ${hasConnect}`); |
| 24 | + |
| 25 | + if (!hasConnect) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + // Process all matches |
| 30 | + while ((match = figmaConnectRegex.exec(content)) !== null) { |
| 31 | + matchCount++; |
| 32 | + const [fullMatch, quotes, url] = match; |
| 33 | + console.log(`\nMatch #${matchCount} found: ${fullMatch}`); |
| 34 | + console.log(`URL extracted: ${url}`); |
| 35 | + |
| 36 | + // Only process if the URL doesn't already have the correct root |
| 37 | + const needsUpdate = !url.startsWith(FIGMA_ROOT_URL); |
| 38 | + console.log(`URL needs update: ${needsUpdate}`); |
| 39 | + |
| 40 | + if (needsUpdate) { |
| 41 | + // Extract node ID from current URL |
| 42 | + let nodeId = null; |
| 43 | + |
| 44 | + // Try to extract from node-id parameter |
| 45 | + const nodeIdMatch = url.match(/node-id=([^&]+)/); |
| 46 | + if (nodeIdMatch) { |
| 47 | + nodeId = nodeIdMatch[1]; |
| 48 | + console.log(`Found node-id in URL parameter: ${nodeId}`); |
| 49 | + } else { |
| 50 | + // Try to extract from end of URL (format: digits-digits) |
| 51 | + const pathParts = url.split('/'); |
| 52 | + const lastPart = pathParts[pathParts.length - 1]; |
| 53 | + if (/^\d+-\d+$/.test(lastPart)) { |
| 54 | + nodeId = lastPart; |
| 55 | + console.log(`Found node-id at end of URL: ${nodeId}`); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Only update if we successfully extracted a node ID |
| 60 | + if (nodeId) { |
| 61 | + const newUrl = `${FIGMA_ROOT_URL}${nodeId}`; |
| 62 | + console.log(`New URL will be: ${newUrl}`); |
| 63 | + |
| 64 | + // Create new content by replacing the old URL with the new one |
| 65 | + const updatedContent = content.replace(fullMatch, fullMatch.replace(url, newUrl)); |
| 66 | + |
| 67 | + // Check if replacement actually changed anything |
| 68 | + if (updatedContent !== content) { |
| 69 | + console.log(`Successfully updated URL in content`); |
| 70 | + await fs.writeFile(filePath, updatedContent, 'utf8'); |
| 71 | + console.log(`Updated file: ${filePath}`); |
| 72 | + modified = true; |
| 73 | + } else { |
| 74 | + console.log(`Warning: Replacement had no effect on content`); |
| 75 | + } |
| 76 | + } else { |
| 77 | + console.log(`Could not extract node ID from URL: ${url}`); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + console.log(`Total matches found: ${matchCount}`); |
| 83 | + return modified; |
| 84 | + } catch (error) { |
| 85 | + console.error(`Error processing ${filePath}:`, error); |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// Simple test function that processes one file |
| 91 | +export async function testProcessFile(filePath) { |
| 92 | + console.log('Running test on file:', filePath); |
| 93 | + const result = await processFigmaFile(filePath); |
| 94 | + console.log('Processing result:', result); |
| 95 | +} |
| 96 | + |
| 97 | +// If this file is run directly, execute the fix |
| 98 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 99 | + const testFile = process.argv[2]; |
| 100 | + |
| 101 | + if (testFile) { |
| 102 | + // Test a specific file if provided |
| 103 | + testProcessFile(testFile); |
| 104 | + } else { |
| 105 | + // Otherwise, find and process all files |
| 106 | + console.log('Finding all .figma.tsx files...'); |
| 107 | + glob('**/*.figma.tsx', { |
| 108 | + ignore: ['**/node_modules/**', '**/dist/**'] |
| 109 | + }).then((files) => { |
| 110 | + console.log(`Found ${files.length} files to process`); |
| 111 | + if (files.length > 0) { |
| 112 | + testProcessFile(files[0]); // Test with the first file found |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments