duplicateDetector.ts 3.37 KB
Newer Older
1
2
#!/usr/bin/env node

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
3
4
5
6
7
const fastGlob = require('fast-glob');
const fileStream = require('fs');
const path = require('path');
const readlineSync = require('readline-sync');

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
8
9
const rootDirectory = process.cwd();

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class II18n {
  public filePath: string;
  public key: string;
  public value: string;

  public static toString = (i18n: II18n) => {
    let namespace = '';
    if (path.dirname(i18n.filePath) === './locales/en') {
      namespace = 'common:';
    } else if (path.dirname(i18n.filePath) === './src') {
      namespace = '';
    } else {
      namespace = path.basename(path.dirname(i18n.filePath)) + '.';
    }
    return `${i18n.filePath}: ${i18n.key} ..... t('${namespace}${i18n.key}')`;
  }
}

const duplicates = new Map<string, II18n[]>();

async function main() {
  console.log('Running...');
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
32
  const dupes = fastGlob([`${rootDirectory}/locales/en/common.json`, `${rootDirectory}/src/**/translations.json`, `${rootDirectory}/src/translations.json`])
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
33
    .then((paths) => paths.filter((path) => !path.includes('node_modules')).map((path) => testFile(path)));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

  console.log('Awawiting results...', dupes);
  await dupes;

  // console.log(duplicates);

  const duplicateCount = [...duplicates.values() ].filter((d) => d.length > 1).length;
  console.log(`Found ${color(bold(duplicateCount), COLOR_BG_RED)} duplicate translations.`);


  for (const value of duplicates.keys()) {
    // console.log(`The val ${value}`);
    const d = duplicates.get(value);
    if (d.length > 1) {
      await handleDuplicate(value, d);
    }
  }
}

const testFile = (path) => {
  const content = JSON.parse(fileStream.readFileSync(path, 'utf8'));
  testDuplicate(content, path);
};

const testDuplicate = (json, filePath, currentPath = undefined) => {
  Object.entries(json).map(([key, value]) => {
    if (typeof value === 'string') {
      const str = value as string;
      if (duplicates.get(str)) {
        duplicates.get(str).push({ filePath, key: (currentPath ?  `${currentPath}.` : '') + key, value: str });
      } else {
        duplicates.set(str, [ { filePath, key: (currentPath ?  `${currentPath}.` : '') + key, value: str } ]);
      }
    } else {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
68
69
70
      if (value) {
        testDuplicate(value, filePath, (currentPath ? `${currentPath}.` : '') + key);
      }
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    }
  });
};


const prettyOutput = (value: string, duplicates: II18n[]) => {
  console.log(`WARN: Found ${color(bold(duplicates.length), COLOR_BG_RED)} duplicates for ${color(bold(value), COLOR_FG_GREEN)}`);
  duplicates.forEach(async (duplicate) => {
    console.log(II18n.toString(duplicate));
  });
  console.log('');
};

function handleDuplicate(value: string, duplicates: II18n[]) {
  console.log('\n\n');
  prettyOutput(value, duplicates);
  const answer = readlineSync.question(`What with ${color(bold(value), COLOR_FG_GREEN)}: `);
  if (answer) {
    console.log(`Doing ${color(answer, COLOR_FG_GREEN)}`);
  }
}

// Terminal colors
const bold = (txt) => color(`${txt}`, 1);
const color = (txt, color: number) => `${COLOR(color)}${txt}${COLOR_RESET}`;

const COLOR_RESET = '\x1b[0m';
const COLOR_BRIGHT = '\x1b[1m';
const COLOR = (color: number) => `\x1b[${color}m`;
const COLOR_FG_GREEN = 32;
const COLOR_FG_YELLOW = 33;
const BgBlack = 40;
const COLOR_BG_RED = 41;
const COLOR_BG_GREEN = 42;
const COLOR_BG_YELLOW = 43;
const BgBlue = 44;
const BgMagenta = 45;
const BgCyan = 46;
const BgWhite = 47;

main().catch((e) => {
  console.log(e);
  process.exit(-1);
});