|
1 | 1 | /*
|
2 |
| - Copyright (C) 2023 Alexander Emanuelsson (alexemanuelol) |
| 2 | + Copyright (C) 2025 Alexander Emanuelsson (alexemanuelol) |
3 | 3 |
|
4 | 4 | This program is free software: you can redistribute it and/or modify
|
5 | 5 | it under the terms of the GNU General Public License as published by
|
|
18 | 18 |
|
19 | 19 | */
|
20 | 20 |
|
21 |
| -/* eslint-disable */ |
| 21 | +import * as fs from 'fs'; |
| 22 | +import * as path from 'path'; |
22 | 23 |
|
23 |
| -const Path = require('path'); |
24 |
| -const Fs = require('fs'); |
25 |
| - |
26 |
| -const root = Path.join(__dirname, '..'); |
27 |
| -const enJsonPath = Path.join(root, 'languages/en.json'); |
28 |
| -const allFiles = []; |
| 24 | +const root = path.join(__dirname, '..'); |
| 25 | +const languageFilePath = path.join(root, 'src/languages/en.json'); |
| 26 | +const allFiles: string[] = []; |
29 | 27 |
|
30 | 28 | const ignoredDirs = [
|
31 | 29 | 'languages',
|
32 | 30 | 'resources',
|
33 |
| - 'docs' |
| 31 | + 'staticFiles', |
| 32 | + 'node_modules', |
| 33 | + 'templates' |
34 | 34 | ];
|
35 | 35 |
|
36 |
| -function recursiveSearchOfAllFiles(path) { |
37 |
| - Fs.readdirSync(path).forEach(file => { |
38 |
| - const filePath = Path.join(path, file); |
| 36 | +function recursiveSearchOfAllFiles(currentPath: string) { |
| 37 | + const files = fs.readdirSync(currentPath); |
| 38 | + |
| 39 | + for (const file of files) { |
| 40 | + const filePath = path.join(currentPath, file); |
| 41 | + const stat = fs.lstatSync(filePath); |
39 | 42 |
|
40 |
| - if (Fs.lstatSync(filePath).isDirectory()) { |
41 |
| - if (!ignoredDirs.includes(file)) { |
| 43 | + /* Only include files in src or the index.ts file */ |
| 44 | + if (stat.isDirectory()) { |
| 45 | + const relativeDir = path.relative(root, filePath); |
| 46 | + if (!ignoredDirs.some(dir => relativeDir.includes(dir))) { |
42 | 47 | recursiveSearchOfAllFiles(filePath);
|
43 | 48 | }
|
| 49 | + } else { |
| 50 | + /* Include index.ts explicitly and all files in the src directory */ |
| 51 | + if (file === 'index.ts' || filePath.includes(path.join(root, 'src'))) { |
| 52 | + allFiles.push(filePath); |
| 53 | + } |
44 | 54 | }
|
45 |
| - else { |
46 |
| - allFiles.push(filePath); |
47 |
| - } |
48 |
| - }) |
| 55 | + } |
49 | 56 | }
|
| 57 | + |
50 | 58 | recursiveSearchOfAllFiles(root);
|
51 | 59 |
|
52 |
| -const phrases = JSON.parse(Fs.readFileSync(enJsonPath, 'utf8')); |
| 60 | +/* Read the phrases from the language file */ |
| 61 | +const phrases = JSON.parse(fs.readFileSync(languageFilePath, 'utf8')); |
53 | 62 | const phrasesKeys = Object.keys(phrases);
|
54 | 63 | const nbrOfPhrases = phrasesKeys.length;
|
55 | 64 |
|
56 | 65 | let counter = 1;
|
57 |
| -const unusedPhrases = []; |
| 66 | +const unusedPhrases: string[] = []; |
| 67 | + |
58 | 68 | for (const phrase of phrasesKeys) {
|
59 |
| - console.log(`Phrase (${counter}/${nbrOfPhrases})`); |
| 69 | + console.log(`Checking phrase (${counter}/${nbrOfPhrases}): "${phrase}"`); |
60 | 70 |
|
61 | 71 | let used = false;
|
62 | 72 | for (const file of allFiles) {
|
63 |
| - const data = Fs.readFileSync(file, { encoding: 'utf8', flag: 'r' }); |
| 73 | + const data = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' }); |
| 74 | + |
| 75 | + /* Check if the phrase is used in any of the three cases */ |
64 | 76 | if (data.includes(`'${phrase}'`) || data.includes(`"${phrase}"`) || data.includes(`\`${phrase}\``)) {
|
65 | 77 | used = true;
|
66 | 78 | break;
|
|
0 commit comments