Skip to content

Commit 57530e4

Browse files
committed
Make translation sorting script work on directories and remove CLI flags.
1 parent 990d113 commit 57530e4

2 files changed

Lines changed: 38 additions & 58 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"test:update-all-snapshots": "yarn test:update-all-desktop-snapshots && yarn test:update-all-mobile-snapshots",
3333
"lint": "yarn run eslint && yarn run prettier-styles && yarn run stylelint",
3434
"format": "yarn run sort-translations && yarn run eslint-fix && yarn run prettier-styles-fix && yarn run stylelint-fix",
35-
"sort-translations": "node scripts/sort-translations.js --fix",
35+
"sort-translations": "node scripts/sort-translations.mjs",
3636
"eslint": "eslint .",
3737
"eslint-fix": "eslint . --fix",
3838
"prettier-styles": "prettier sass/**/*.scss app/**/*.scss digitransit-component/**/*.scss --check",

scripts/sort-translations.mjs

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import fs from 'fs';
55
import path from 'path';
66

7-
const TRANSLATIONS_FILE = path.resolve(
7+
const TRANSLATIONS_DIR = path.resolve(
88
import.meta.dirname,
9-
'../app/translations.mjs',
9+
'../app/translations',
1010
);
1111
const PRINT_WIDTH = 80;
12-
const VALID_FLAGS = new Set(['--check', '--fix']);
1312

1413
function needsQuoting(key) {
1514
return !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key);
@@ -32,81 +31,62 @@ function formatValue(value) {
3231
return `'${escaped}'`;
3332
}
3433

35-
function sortedTranslationsFileContent(translations) {
34+
function sortedTranslationsFileContent(lang, translations) {
3635
const lines = [];
3736
lines.push('/* eslint sort-keys: "error" */');
38-
lines.push('const translations = {');
37+
lines.push('export default {');
38+
lines.push(` ${formatKey(lang)}: {`);
3939

40-
const sortedLangs = Object.keys(translations).sort();
41-
for (let i = 0; i < sortedLangs.length; i++) {
42-
const lang = sortedLangs[i];
43-
lines.push(` ${formatKey(lang)}: {`);
44-
45-
const keys = Object.keys(translations[lang]).sort();
46-
for (let j = 0; j < keys.length; j++) {
47-
const key = keys[j];
48-
const formattedKey = formatKey(key);
49-
const formattedValue = formatValue(translations[lang][key]);
50-
const singleLine = ` ${formattedKey}: ${formattedValue},`;
51-
if (singleLine.length <= PRINT_WIDTH) {
52-
lines.push(singleLine);
53-
} else {
54-
lines.push(` ${formattedKey}:`);
55-
lines.push(` ${formattedValue},`);
56-
}
40+
const keys = Object.keys(translations).sort();
41+
for (let j = 0; j < keys.length; j++) {
42+
const key = keys[j];
43+
const formattedKey = formatKey(key);
44+
const formattedValue = formatValue(translations[key]);
45+
const singleLine = ` ${formattedKey}: ${formattedValue},`;
46+
if (singleLine.length <= PRINT_WIDTH) {
47+
lines.push(singleLine);
48+
} else {
49+
lines.push(` ${formattedKey}:`);
50+
lines.push(` ${formattedValue},`);
5751
}
58-
lines.push(' },');
5952
}
6053

54+
lines.push(' },');
6155
lines.push('};');
6256
lines.push('');
63-
lines.push('export default translations;');
64-
lines.push('');
6557

6658
return lines.join('\n');
6759
}
6860

6961
async function main() {
7062
try {
7163
console.log('---------- Running sort-translations.mjs script ----------');
72-
const flag = process.argv[2];
73-
if (!VALID_FLAGS.has(flag)) {
74-
console.error(
75-
'Missing or invalid mode. Use --check to verify sorting or --fix to write sorted translations.',
76-
);
77-
process.exitCode = 1;
78-
return;
79-
}
8064

81-
const translations = (await import(TRANSLATIONS_FILE)).default;
82-
const current = fs.readFileSync(TRANSLATIONS_FILE, 'utf-8');
83-
const sorted = sortedTranslationsFileContent(translations);
65+
const files = fs
66+
.readdirSync(TRANSLATIONS_DIR)
67+
.filter(f => f.endsWith('.js'))
68+
.sort();
8469

85-
if (flag === '--check') {
86-
if (current === sorted) {
87-
console.log('CHECK mode - Translations are sorted:', TRANSLATIONS_FILE);
88-
} else {
70+
for (let i = 0; i < files.length; i++) {
71+
const file = files[i];
72+
const filePath = path.join(TRANSLATIONS_DIR, file);
73+
console.log('Processing file', filePath);
74+
// eslint-disable-next-line no-await-in-loop
75+
const module = await import(filePath);
76+
const langData = module.default;
77+
const lang = Object.keys(langData)[0];
78+
const sorted = sortedTranslationsFileContent(lang, langData[lang]);
79+
80+
try {
81+
fs.writeFileSync(filePath, sorted, 'utf-8');
82+
console.log('Sorted translations written to', filePath);
83+
} catch (error) {
84+
const message = error instanceof Error ? error.message : String(error);
8985
console.error(
90-
'CHECK mode - Translations are not sorted:',
91-
TRANSLATIONS_FILE,
86+
`Failed to write sorted translations to ${filePath}: ${message}`,
9287
);
9388
process.exitCode = 1;
9489
}
95-
return;
96-
}
97-
98-
try {
99-
fs.writeFileSync(TRANSLATIONS_FILE, sorted, 'utf-8');
100-
console.log(
101-
'FIX mode - Sorted translations written to',
102-
TRANSLATIONS_FILE,
103-
);
104-
} catch (error) {
105-
const message = error instanceof Error ? error.message : String(error);
106-
console.error(
107-
`FIX - Failed to write sorted translations to ${TRANSLATIONS_FILE}: ${message}`,
108-
);
109-
process.exitCode = 1;
11090
}
11191
} catch (error) {
11292
const message = error instanceof Error ? error.message : String(error);

0 commit comments

Comments
 (0)