const fs = require('fs');
|
const path = require('path');
|
|
function findVueFiles(dir, fileList = []) {
|
const files = fs.readdirSync(dir);
|
files.forEach((file) => {
|
const filePath = path.join(dir, file);
|
if (fs.statSync(filePath).isDirectory()) {
|
findVueFiles(filePath, fileList);
|
} else if (filePath.endsWith('.vue')) {
|
fileList.push(filePath);
|
}
|
});
|
return fileList;
|
}
|
|
const allVueFiles = findVueFiles(path.join(__dirname, 'src'));
|
let modifiedCount = 0;
|
|
allVueFiles.forEach((file) => {
|
let content = fs.readFileSync(file, 'utf8');
|
|
if (content.includes('src="/src/assets/element-img/no-data.png"')) {
|
// 1. Replace all occurrences
|
content = content.replace(/src="\/src\/assets\/element-img\/no-data\.png"/g, ':src="noDataImg"');
|
|
// 2. Check if already imported
|
if (!content.includes('noDataImg') || !content.includes('@/assets/element-img/no-data.png')) {
|
const scriptSetupMatch = content.match(/<script\s+setup[^>]*>/);
|
if (scriptSetupMatch) {
|
const insertIndex = scriptSetupMatch.index + scriptSetupMatch[0].length;
|
const importStr = `\nimport noDataImg from '@/assets/element-img/no-data.png'`;
|
content = content.slice(0, insertIndex) + importStr + content.slice(insertIndex);
|
} else {
|
console.log(`Warning: No <script setup> found in ${file}`);
|
}
|
}
|
|
fs.writeFileSync(file, content, 'utf8');
|
modifiedCount++;
|
console.log(`Modified: ${file}`);
|
}
|
});
|
|
console.log(`Total files modified: ${modifiedCount}`);
|