46 lines
1.8 KiB
JavaScript
46 lines
1.8 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const dirPath = './src/app/restaurant-types';
|
|
|
|
function walkDir(dir, callback) {
|
|
fs.readdirSync(dir).forEach(f => {
|
|
let p = path.join(dir, f);
|
|
let isDirectory = fs.statSync(p).isDirectory();
|
|
isDirectory ? walkDir(p, callback) : callback(p);
|
|
});
|
|
}
|
|
|
|
walkDir(dirPath, function(filePath) {
|
|
if (filePath.endsWith('.tsx')) {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
let original = content;
|
|
|
|
if (content.includes('randomuser.me')) {
|
|
const importStatement = `\nimport User1 from '@/assets/restaurant/Join 2,460+ Restaurants/1.jpg';\nimport User2 from '@/assets/restaurant/Join 2,460+ Restaurants/2.jpg';\nimport User3 from '@/assets/restaurant/Join 2,460+ Restaurants/3.jpg';\n`;
|
|
|
|
if (!content.includes('User1 from')) {
|
|
// Find a good place to insert imports
|
|
if (content.includes("import React")) {
|
|
content = content.replace(/(import React.*?from 'react'.*?\n)/, `$1${importStatement}`);
|
|
} else {
|
|
content = content.replace(/(import .*?\n)/, `$1${importStatement}`);
|
|
}
|
|
}
|
|
|
|
// Replace the mapping logic.
|
|
content = content.replace(/\{\[1,\s*2,\s*3\]\.map\(\([a-zA-Z]+\) => \(/g, '{[User1, User2, User3].map((imgSrc, i) => (');
|
|
|
|
// Regex to match the Image tag spanning multiple lines or single line
|
|
content = content.replace(/<Image\s+src=\{`https:\/\/randomuser\.me[^`]+`\}\s+alt="User"\s+fill\s+className="object-cover"\s*\/>/g, '<Image src={imgSrc} alt="User" fill className="object-cover" />');
|
|
|
|
if (content !== original) {
|
|
fs.writeFileSync(filePath, content);
|
|
console.log(`Updated ${filePath}`);
|
|
} else {
|
|
console.log(`Failed to update ${filePath} - regex mismatch`);
|
|
}
|
|
}
|
|
}
|
|
});
|