![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/scripts/ |
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const glob = require('glob');
// Common build error patterns to fix
const errorPatterns = [
// Duplicate useState imports
{
name: 'Duplicate useState imports',
pattern: /import \{ useState \} from 'react';\s*\nimport \{ useState, ([^}]+) \} from 'react';/g,
replacement: (match, otherImports) => `import { useState, ${otherImports} } from 'react';`,
files: ['**/*.tsx', '**/*.jsx']
},
// Duplicate useEffect imports
{
name: 'Duplicate useEffect imports',
pattern: /import \{ useEffect \} from 'react';\s*\nimport \{ ([^}]+), useEffect \} from 'react';/g,
replacement: (match, otherImports) => `import { ${otherImports}, useEffect } from 'react';`,
files: ['**/*.tsx', '**/*.jsx']
},
// Chakra UI Button isLoading prop (should be disabled + text change)
{
name: 'Chakra UI Button isLoading prop',
pattern: /<Button[^>]*isLoading=\{([^}]+)\}[^>]*>([^<]*)<\/Button>/g,
replacement: (match, loadingVar, buttonText) => {
return `<Button disabled={${loadingVar}}>${loadingVar} ? 'Loading...' : '${buttonText}'</Button>`;
},
files: ['**/*.tsx', '**/*.jsx']
},
// Missing language prop in components
{
name: 'Missing language prop in component calls',
pattern: /<(\w+)[^>]*language=\{language\}[^>]*\/>/g,
replacement: (match, componentName) => {
// This is a complex one - we'll need to check if the component expects language prop
return match; // For now, just return as is
},
files: ['**/*.tsx', '**/*.jsx']
},
// Undefined language variable in JSX
{
name: 'Undefined language variable in JSX',
pattern: /\{language === 'fr' \? ([^:]+) : ([^}]+)\}/g,
replacement: (match, frText, enText) => {
// Replace with static text for now
return `'${frText.trim()}'`;
},
files: ['**/*.tsx', '**/*.jsx']
}
];
// Files to scan
const scanPaths = [
'src/**/*.tsx',
'src/**/*.jsx',
'src/**/*.ts',
'src/**/*.js'
];
// Files to exclude
const excludePatterns = [
'**/node_modules/**',
'**/.next/**',
'**/deployment/**',
'**/*.d.ts',
'**/test-results/**'
];
function fixDuplicateImports(content) {
let modified = false;
// Fix duplicate useState imports
const useStatePattern = /import \{ useState \} from 'react';\s*\nimport \{ useState, ([^}]+) \} from 'react';/g;
if (content.match(useStatePattern)) {
content = content.replace(useStatePattern, (match, otherImports) => {
modified = true;
return `import { useState, ${otherImports} } from 'react';`;
});
}
// Fix duplicate useEffect imports
const useEffectPattern = /import \{ useEffect \} from 'react';\s*\nimport \{ ([^}]+), useEffect \} from 'react';/g;
if (content.match(useEffectPattern)) {
content = content.replace(useEffectPattern, (match, otherImports) => {
modified = true;
return `import { ${otherImports}, useEffect } from 'react';`;
});
}
// Fix duplicate React imports
const reactPattern = /import React from 'react';\s*\nimport \{ ([^}]+) \} from 'react';/g;
if (content.match(reactPattern)) {
content = content.replace(reactPattern, (match, otherImports) => {
modified = true;
return `import React, { ${otherImports} } from 'react';`;
});
}
return { content, modified };
}
function fixChakraUIProps(content) {
let modified = false;
// Fix isLoading prop in Chakra UI Button
const isLoadingPattern = /<Button([^>]*)isLoading=\{([^}]+)\}([^>]*)>([^<]*)<\/Button>/g;
if (content.match(isLoadingPattern)) {
content = content.replace(isLoadingPattern, (match, beforeProps, loadingVar, afterProps, buttonText) => {
modified = true;
return `<Button${beforeProps}disabled={${loadingVar}}${afterProps}>{${loadingVar} ? 'Loading...' : '${buttonText.trim()}'}</Button>`;
});
}
return { content, modified };
}
function fixLanguageIssues(content) {
let modified = false;
// Fix undefined language variable in JSX expressions
const languagePattern = /\{language === 'fr' \? ([^:]+) : ([^}]+)\}/g;
if (content.match(languagePattern)) {
content = content.replace(languagePattern, (match, frText, enText) => {
modified = true;
// Use the French text as default for now
return `'${frText.trim()}'`;
});
}
return { content, modified };
}
function addMissingLanguageState(content) {
let modified = false;
// Check if component uses language but doesn't have language state
if (content.includes('language ===') && !content.includes('const [language, setLanguage]')) {
// Add language state after component declaration
const componentPattern = /(const\s+\w+\s*:\s*React\.FC\s*=\s*\(\)\s*=>\s*{)/;
if (content.match(componentPattern)) {
const languageState = `
const [language, setLanguage] = useState<'fr' | 'en'>('fr');
const handleLanguageToggle = () => {
const newLang = language === 'fr' ? 'en' : 'fr';
setLanguage(newLang);
};`;
content = content.replace(componentPattern, (match) => {
modified = true;
return match + languageState;
});
}
}
return { content, modified };
}
function fixMalformedHref(content) {
let modified = false;
// Replace href=''/something'' or href=''/something'' with href="/something"
const malformedHrefPattern = /href=''\/([^']*)''/g;
if (content.match(malformedHrefPattern)) {
content = content.replace(malformedHrefPattern, (match, path) => {
modified = true;
return `href="/${path}"`;
});
}
return { content, modified };
}
function fixFile(filePath) {
try {
let content = fs.readFileSync(filePath, 'utf8');
let totalModified = false;
console.log(`\nš Processing ${filePath}:`);
// Fix malformed hrefs
const hrefResult = fixMalformedHref(content);
if (hrefResult.modified) {
content = hrefResult.content;
console.log(` ā
Fixed malformed href attributes`);
totalModified = true;
}
// Fix duplicate imports
const importResult = fixDuplicateImports(content);
if (importResult.modified) {
content = importResult.content;
console.log(` ā
Fixed duplicate imports`);
totalModified = true;
}
// Fix Chakra UI props
const chakraResult = fixChakraUIProps(content);
if (chakraResult.modified) {
content = chakraResult.content;
console.log(` ā
Fixed Chakra UI props`);
totalModified = true;
}
// Fix language issues
const languageResult = fixLanguageIssues(content);
if (languageResult.modified) {
content = languageResult.content;
console.log(` ā
Fixed language issues`);
totalModified = true;
}
// Add missing language state
const stateResult = addMissingLanguageState(content);
if (stateResult.modified) {
content = stateResult.content;
console.log(` ā
Added missing language state`);
totalModified = true;
}
if (totalModified) {
fs.writeFileSync(filePath, content);
console.log(` š¾ Updated ${filePath}`);
return true;
} else {
console.log(` ā ļø No changes needed`);
return false;
}
} catch (error) {
console.log(` ā Error processing ${filePath}: ${error.message}`);
return false;
}
}
function main() {
console.log('š§ Fixing common build errors...\n');
const allFiles = [];
scanPaths.forEach(pattern => {
const files = glob.sync(pattern, { ignore: excludePatterns });
allFiles.push(...files);
});
console.log(`š Found ${allFiles.length} files to scan\n`);
let totalFixed = 0;
let totalErrors = 0;
allFiles.forEach(filePath => {
if (fixFile(filePath)) {
totalFixed++;
} else {
totalErrors++;
}
});
console.log('\nš SUMMARY:');
console.log(` - Files processed: ${allFiles.length}`);
console.log(` - Files fixed: ${totalFixed}`);
console.log(` - Files with errors: ${totalErrors}`);
if (totalFixed > 0) {
console.log('\nā
Build errors fixed!');
console.log('š” Next steps:');
console.log(' 1. Run "npm run build" to check for remaining errors');
console.log(' 2. If there are still errors, run this script again');
console.log(' 3. For complex errors, manual fixes may be needed');
} else {
console.log('\nā ļø No files needed fixing');
}
}
if (require.main === module) {
main();
}
module.exports = { fixFile, fixDuplicateImports, fixChakraUIProps, fixLanguageIssues };