|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import ignore from "ignore"; |
| 4 | + |
| 5 | +function isKebabCase(filename: string): boolean { |
| 6 | + const basename = path.basename(filename, path.extname(filename)); |
| 7 | + return /^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+)?$/.test(basename); |
| 8 | +} |
| 9 | + |
| 10 | +function checkTsFiles(dir: string, ig: ignore.Ignore, invalidFiles: string[] = []): string[] { |
| 11 | + const files = fs.readdirSync(dir); |
| 12 | + |
| 13 | + for (const file of files) { |
| 14 | + const fullPath = path.join(dir, file); |
| 15 | + const relativePath = path.relative(process.cwd(), fullPath); |
| 16 | + const stat = fs.statSync(fullPath); |
| 17 | + |
| 18 | + if (ig.ignores(relativePath)) { |
| 19 | + continue; // Skip ignored files/folders |
| 20 | + } |
| 21 | + |
| 22 | + if (stat.isDirectory()) { |
| 23 | + checkTsFiles(fullPath, ig, invalidFiles); |
| 24 | + } else if (file.endsWith(".ts")) { |
| 25 | + if (!isKebabCase(file)) { |
| 26 | + invalidFiles.push(relativePath); |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return invalidFiles; |
| 32 | +} |
| 33 | + |
| 34 | +function main() { |
| 35 | + const projectRoot = process.cwd(); |
| 36 | + const ig = ignore().add(fs.readFileSync(path.join(projectRoot, ".gitignore")).toString()); |
| 37 | + const invalidFiles = checkTsFiles(projectRoot, ig); |
| 38 | + |
| 39 | + if (invalidFiles.length > 0) { |
| 40 | + console.error("The following files do not follow kebab-case naming:"); |
| 41 | + invalidFiles.forEach((file) => console.error(`- ${file}`)); |
| 42 | + process.exit(1); |
| 43 | + } else { |
| 44 | + console.log("All .ts files follow kebab-case naming!"); |
| 45 | + process.exit(0); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +main(); |
0 commit comments