Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clementtalleu/07c3564cedb21b1fa7717eaf798d88c4 to your computer and use it in GitHub Desktop.
Save clementtalleu/07c3564cedb21b1fa7717eaf798d88c4 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
// Fonction pour scanner les fichiers PHP dans un dossier
function scanDirectory(directory) {
const files = fs.readdirSync(directory);
files.forEach(file => {
const fullPath = path.join(directory, file);
if (fs.lstatSync(fullPath).isDirectory()) {
scanDirectory(fullPath); // Récursion dans les sous-dossiers
} else if (path.extname(fullPath) === '.php') {
scanFile(fullPath);
}
});
}
// Fonction pour scanner un fichier PHP à la recherche des blocs SQL
function scanFile(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
const lines = content.split('\n');
let insideSQLBlock = false;
let sqlQuery = '';
let sqlStartLine = 0;
lines.forEach((line, index) => {
if (line.includes(`<<<'SQL'`)) {
insideSQLBlock = true;
sqlStartLine = index + 1; // Ligne où le bloc commence
} else if (insideSQLBlock && line.includes('SQL')) {
insideSQLBlock = false;
// sqlQuery += line.replace('SQL', '').trim();
runSQLLint(sqlQuery.trim(), filePath, sqlStartLine);
sqlQuery = '';
} else if (insideSQLBlock) {
sqlQuery += line.trim() + ' ';
}
});
}
// Fonction pour exécuter squawk sur la requête SQL trouvée
function runSQLLint(sqlQuery, filePath, line) {
const command = `echo "${sqlQuery}" | squawk`;
exec(command, (error, stdout, stderr) => {
if (stdout.includes('syntax error at or near ":"')){
return;
}
if (error) {
console.log(`Erreur lors de l'exécution de squawk sur ${filePath} à la ligne ${line}:`)
console.error(stdout);
// console.error(`Erreur lors de l'exécution de squawk sur ${filePath} à la ligne ${line}: ${stdout}`);
} else if (stdout) {
// If there's a linting message, display it
// console.log(`Linting result pour le fichier ${filePath} - Ligne: ${line}\n${stdout}`);
}
});
}
// Dossier source à scanner
const srcDirectory = path.join(__dirname, 'src');
// Lancer le scan du dossier
scanDirectory(srcDirectory);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment