|
| 1 | +const fs = require("node:fs"); |
| 2 | +const path = require("node:path"); |
| 3 | + |
| 4 | +const CL = require("../libs/ColorLogger.js"); |
| 5 | + |
| 6 | +/** |
| 7 | + * 検査対象の設定 |
| 8 | + */ |
| 9 | +const config = { |
| 10 | + includeExtensions: [".js", ".ts", ".json", ".md", "bat", ".txt", ".html", ".css"], // 検査対象ファイルの拡張子 |
| 11 | + excludeFiles: ["checkIllegalStrings.js", "package-lock.json", "LICENSE"], // 除外するファイル |
| 12 | + excludeDirs: ["node_modules", ".git", "dist"], // 除外するフォルダ |
| 13 | + illegalPatterns: [/[\u3099\u309A]/], // 違法文字列(正規表現) |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * ファイルを検査する |
| 18 | + */ |
| 19 | +function checkFile(filePath, baseDir) { |
| 20 | + const content = fs.readFileSync(filePath, "utf-8"); |
| 21 | + const lines = content.split(/\r?\n/); |
| 22 | + |
| 23 | + let found = false; |
| 24 | + |
| 25 | + lines.forEach((line, idx) => { |
| 26 | + for (const pattern of config.illegalPatterns) { |
| 27 | + const match = line.match(pattern); |
| 28 | + if (match) { |
| 29 | + if (!found) { |
| 30 | + console.log(`┃┣🚨 ${CL.brightRed("違法文字列を検出")}: ${path.relative(baseDir, filePath)}`); |
| 31 | + found = true; |
| 32 | + } |
| 33 | + const highlight = line.replace(pattern, (m) => `${CL.RESET}${CL.red(m)}${CL.STYLE.dim}`); // ハイライト |
| 34 | + console.log(`┃┃ [${idx + 1}行目] ${CL.STYLE.dim}${highlight}${CL.RESET}`); |
| 35 | + console.log(`┃┃ → マッチ: ${CL.brightCyan(match[0])}`); |
| 36 | + console.log(`┃┃ → パターン: ${CL.brightMagenta(pattern)}`); |
| 37 | + } |
| 38 | + } |
| 39 | + }); |
| 40 | + return found; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * ディレクトリを再帰的に探索 |
| 45 | + */ |
| 46 | +function checkIllegalStrings(dirPath, baseDir = dirPath) { |
| 47 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 48 | + |
| 49 | + let found = false; |
| 50 | + |
| 51 | + for (const entry of entries) { |
| 52 | + const fullPath = path.join(dirPath, entry.name); |
| 53 | + |
| 54 | + // 除外処理 |
| 55 | + if (entry.isDirectory()) { |
| 56 | + if (config.excludeDirs.includes(entry.name)) continue; |
| 57 | + found = checkIllegalStrings(fullPath, baseDir) || found; |
| 58 | + } else { |
| 59 | + if (config.excludeFiles.includes(entry.name)) continue; |
| 60 | + const ext = path.extname(entry.name).toLowerCase(); |
| 61 | + if (!config.includeExtensions.includes(ext)) continue; |
| 62 | + found = checkFile(fullPath, baseDir) || found; |
| 63 | + } |
| 64 | + } |
| 65 | + return found; |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = checkIllegalStrings; |
0 commit comments