|
1 | 1 | #!/usr/bin/env node |
2 | | - |
| 2 | +/* eslint-disable @typescript-eslint/no-require-imports, no-console */ |
3 | 3 | /** |
4 | 4 | * Check that i18n locale files are in sync with extracted messages. |
5 | | - * Runs `pnpm i18n:extract` and compares en.json; exits 1 if they differ. |
| 5 | + * Runs extract script and compares en.json files; exits 1 if they differ. |
6 | 6 | */ |
7 | 7 | const { execSync } = require('child_process'); |
8 | 8 | const fs = require('fs'); |
9 | 9 | const path = require('path'); |
10 | 10 |
|
11 | | -const localePath = path.join( |
12 | | - __dirname, |
13 | | - '..', |
14 | | - 'src', |
15 | | - 'i18n', |
16 | | - 'locale', |
17 | | - 'en.json' |
18 | | -); |
19 | | -const backupPath = `${localePath}.bak`; |
| 11 | +const targets = [ |
| 12 | + path.join(__dirname, '..', 'src', 'i18n', 'locale', 'en.json'), |
| 13 | + path.join(__dirname, '..', 'server', 'i18n', 'locale', 'en.json'), |
| 14 | +]; |
| 15 | + |
| 16 | +const backups = targets.map((p) => `${p}.bak`); |
20 | 17 |
|
21 | 18 | try { |
22 | | - fs.copyFileSync(localePath, backupPath); |
| 19 | + targets.forEach((p, i) => fs.copyFileSync(p, backups[i])); |
23 | 20 | execSync('pnpm i18n:extract', { stdio: 'inherit' }); |
24 | | - const original = fs.readFileSync(backupPath, 'utf8'); |
25 | | - const extracted = fs.readFileSync(localePath, 'utf8'); |
26 | | - fs.unlinkSync(backupPath); |
27 | 21 |
|
28 | | - if (original !== extracted) { |
29 | | - console.error( |
30 | | - "i18n messages are out of sync. Please run 'pnpm i18n:extract' and commit the changes." |
31 | | - ); |
| 22 | + let outOfSync = false; |
| 23 | + for (let i = 0; i < targets.length; i++) { |
| 24 | + const original = fs.readFileSync(backups[i], 'utf8'); |
| 25 | + const extracted = fs.readFileSync(targets[i], 'utf8'); |
| 26 | + fs.unlinkSync(backups[i]); |
| 27 | + |
| 28 | + if (original !== extracted) { |
| 29 | + console.error( |
| 30 | + `i18n messages are out of sync for ${path.basename(path.dirname(path.dirname(targets[i])))}. Please run 'pnpm i18n:extract' and commit the changes.` |
| 31 | + ); |
| 32 | + outOfSync = true; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if (outOfSync) { |
32 | 37 | process.exit(1); |
33 | 38 | } |
34 | 39 | } catch (err) { |
35 | | - if (fs.existsSync(backupPath)) { |
36 | | - fs.unlinkSync(backupPath); |
37 | | - } |
| 40 | + backups.forEach((b) => { |
| 41 | + if (fs.existsSync(b)) fs.unlinkSync(b); |
| 42 | + }); |
38 | 43 | throw err; |
39 | 44 | } |
0 commit comments