44import fs from 'fs' ;
55import 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) ;
1111const PRINT_WIDTH = 80 ;
12- const VALID_FLAGS = new Set ( [ '--check' , '--fix' ] ) ;
1312
1413function needsQuoting ( key ) {
1514 return ! / ^ [ a - z A - Z _ $ ] [ a - z A - Z 0 - 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
6961async 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