Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created December 26, 2024 09:30
Show Gist options
  • Save Armenvardanyan95/f9cbe4999fbfaa370290d7fc331172ba to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/f9cbe4999fbfaa370290d7fc331172ba to your computer and use it in GitHub Desktop.
const fs = require('fs');
const path = require('path');
async function findMissingImports(projectRoot, directiveName, directiveClass) {
const missingImportFiles = [];
const searchDirectory = async (dir) => {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
await searchDirectory(filePath); // Recursive call for subdirectories
} else if (file.endsWith('.component.html')) {
const tsFilePath = filePath.replace('.component.html', '.component.ts');
// Check if the corresponding .ts file exists
if (!fs.existsSync(tsFilePath)) continue;
// 1. Check if the directive is used in the HTML file
const htmlContent = fs.readFileSync(filePath, 'utf-8');
const directiveUsed = htmlContent.includes(directiveName);
if (!directiveUsed) continue;
// 2. Check if the directive class is imported in the .ts file
const tsContent = fs.readFileSync(tsFilePath, 'utf-8');
const importRegex = new RegExp(`import\\s+\\{.*\\b${directiveClass}\\b.*\\}\\s+from\\s+.*`);
const importPresent = importRegex.test(tsContent);
// 3. If the directive is used but the import is missing, add to the list
if (directiveUsed && !importPresent) {
missingImportFiles.push(tsFilePath);
}
}
}
};
await searchDirectory(projectRoot);
return missingImportFiles;
}
// --- Configuration ---
const PROJECT_ROOT = './'; // Replace with your project path
const DIRECTIVE_NAME = "<app-razor-multiselect";
const DIRECTIVE_CLASS = "RazorMultiselectComponent";
// --- Run the script ---
findMissingImports(PROJECT_ROOT, DIRECTIVE_NAME, DIRECTIVE_CLASS)
.then((missingFiles) => {
if (missingFiles.length > 0) {
console.log('Component files missing the import:');
missingFiles.forEach((file) => console.log(file));
} else {
console.log('No component files found missing the import.');
}
})
.catch((err) => {
console.error('An error occurred:', err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment