4
4
readdirSync ,
5
5
readFileSync ,
6
6
writeFileSync ,
7
+ unlinkSync ,
7
8
} from 'fs' ;
8
9
import {
9
10
parse ,
@@ -15,18 +16,25 @@ import {
15
16
import Logger from './logger.js' ;
16
17
import toTypescriptObject from './to-typescript-object.js' ;
17
18
import loadKeys from './loadKeys.js' ;
18
- import { unlinkSync } from "node:fs" ;
19
19
20
- export default ( logger : Logger , cwd : string , shouldSplit = false , isVerbatimModuleSyntax = false ) => {
21
- if ( existsSync ( `${ cwd } /${ TARGET_DIR } ` , ) ) {
22
- for ( const file of readdirSync ( `${ cwd } /${ TARGET_DIR } ` , 'utf8' , ) ) {
23
- unlinkSync ( `${ cwd } /${ TARGET_DIR } /${ file } ` , ) ;
20
+ export default (
21
+ logger : Logger , cwd : string ,
22
+ shouldSplit = false ,
23
+ isVerbatimModuleSyntax = false ,
24
+ isWithoutTranslationFile = false ,
25
+ isStrictTyping = false ,
26
+ // eslint-disable-next-line max-params
27
+ ) => {
28
+ if ( existsSync ( `${ cwd } /${ TARGET_DIR } ` , ) ) {
29
+ for ( const file of readdirSync ( `${ cwd } /${ TARGET_DIR } ` , 'utf8' , ) ) {
30
+ unlinkSync ( `${ cwd } /${ TARGET_DIR } /${ file } ` , ) ;
24
31
}
25
32
}
26
33
const yamlFiles = readdirSync ( `${ cwd } /${ ORIGIN_DIRECTORY } ` , 'utf8' , )
27
34
. filter ( ( file , ) => file . endsWith ( '.yml' , ) , ) ;
28
35
29
36
const files = [ ] ;
37
+ // eslint-disable-next-line complexity
30
38
yamlFiles . forEach ( ( yamlFile , ) => {
31
39
const lang = yamlFile . replace ( '.yml' , '' , ) ;
32
40
const yamlPath = `${ ORIGIN_DIRECTORY } /${ yamlFile } ` ;
@@ -39,22 +47,45 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
39
47
40
48
const content = readFileSync ( yamlPath , 'utf8' , ) ;
41
49
const data = parse ( content , ) ;
42
- if ( shouldSplit ) {
50
+ const typeName = isStrictTyping ? 'langType' : 'Partial<langType>' ;
51
+ if ( shouldSplit && typeof data [ Object . keys ( data , ) . pop ( ) ] !== 'string' ) {
43
52
for ( const key of Object . keys ( data , ) ) {
44
53
writeFileSync (
45
54
`${ TARGET_DIR } /${ lang } -${ key } .ts` ,
46
- `/* eslint max-len:0 */\nconst lang = ${ toTypescriptObject ( data [ key ] ) } ;\n\nexport default lang;\n` ,
55
+ isVerbatimModuleSyntax
56
+ ? `/* eslint max-len:0 */\nimport {\n lang as langType,\n} from './type-${ key } .js';\nconst lang: ${ typeName } = ${ toTypescriptObject ( data [ key ] , ) } ;\n\nexport default lang;\n`
57
+ : `/* eslint max-len:0 */\nimport langType from './type-${ key } .js';\nconst lang: ${ typeName } = ${ toTypescriptObject ( data [ key ] , ) } ;\n\nexport default lang;\n` ,
47
58
'utf8' ,
48
59
) ;
49
60
files . push ( `${ lang } -${ key } ` , ) ;
61
+ if ( lang === 'en' ) {
62
+ writeFileSync (
63
+ `${ TARGET_DIR } /type-${ key } .ts` ,
64
+ isVerbatimModuleSyntax
65
+ ? `/* eslint max-len:0 */\ntype ln = ${ toTypescriptObject ( data [ key ] , ) . replace ( / : ' .* ?' , / ug, ': string,' , ) } ;\n\nexport type lang = ln;\n`
66
+ : `/* eslint max-len:0 */\ntype lang = ${ toTypescriptObject ( data [ key ] , ) . replace ( / : ' .* ?' , / ug, ': string,' , ) } ;\n\nexport default lang;\n` ,
67
+ 'utf8' ,
68
+ ) ;
69
+ }
50
70
}
51
71
} else {
52
72
writeFileSync (
53
73
`${ TARGET_DIR } /${ lang } .ts` ,
54
- `/* eslint max-len:0 */\nconst lang = ${ toTypescriptObject ( data ) } ;\n\nexport default lang;\n` ,
74
+ isVerbatimModuleSyntax
75
+ ? `/* eslint max-len:0 */\nimport {\n lang as langType,\n} from './type.js';\nconst lang: ${ typeName } = ${ toTypescriptObject ( data , ) } ;\n\nexport default lang;\n`
76
+ : `/* eslint max-len:0 */\nimport langType from './type.js';\nconst lang: ${ typeName } = ${ toTypescriptObject ( data , ) } ;\n\nexport default lang;\n` ,
55
77
'utf8' ,
56
78
) ;
57
79
files . push ( `${ lang } ` , ) ;
80
+ if ( lang === 'en' ) {
81
+ writeFileSync (
82
+ `${ TARGET_DIR } /type.ts` ,
83
+ isVerbatimModuleSyntax
84
+ ? `/* eslint max-len:0 */\ntype ln = ${ toTypescriptObject ( data , ) . replace ( / : ' .* ?' , / ug, ': string,' , ) } ;\n\nexport type lang = ln;\n`
85
+ : `/* eslint max-len:0 */\ntype lang = ${ toTypescriptObject ( data , ) . replace ( / : ' .* ?' , / ug, ': string,' , ) } ;\n\nexport default lang;\n` ,
86
+ 'utf8' ,
87
+ ) ;
88
+ }
58
89
}
59
90
if ( lang === 'en' ) {
60
91
const keys = loadKeys ( data , ) ;
@@ -78,20 +109,22 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
78
109
) ;
79
110
writeFileSync (
80
111
TARGET_DIR + '/files.ts' ,
81
- `const files = ${ toTypescriptObject ( files ) } ;\nexport default files;\n` ,
112
+ `const files = ${ toTypescriptObject ( files , ) } ;\nexport default files;\n` ,
82
113
'utf8' ,
83
114
) ;
84
- let fileImporter = '' ;
85
- let fileExporter = 'const translations = {' ;
86
- for ( const f of files ) {
87
- const v = f . replace ( / - / gu, '_' , ) ;
88
- fileImporter += `import ${ v } from './${ f } .js';\n` ;
89
- fileExporter += `\n '${ f } ': ${ v } ,` ;
115
+ if ( ! isWithoutTranslationFile ) {
116
+ let fileImporter = '' ;
117
+ let fileExporter = 'const translations = {' ;
118
+ for ( const f of files ) {
119
+ const v = f . replace ( / - / gu, '_' , ) ;
120
+ fileImporter += `import ${ v } from './${ f } .js';\n` ;
121
+ fileExporter += `\n '${ f } ': ${ v } ,` ;
122
+ }
123
+ fileExporter += '\n};' ;
124
+ writeFileSync (
125
+ TARGET_DIR + '/translations.ts' ,
126
+ `${ fileImporter } ${ fileExporter } \nexport default translations;\n` ,
127
+ 'utf8' ,
128
+ ) ;
90
129
}
91
- fileExporter += '\n};' ;
92
- writeFileSync (
93
- TARGET_DIR + '/translations.ts' ,
94
- `${ fileImporter } ${ fileExporter } \nexport default translations;\n` ,
95
- 'utf8' ,
96
- ) ;
97
130
} ;
0 commit comments