44 readdirSync ,
55 readFileSync ,
66 writeFileSync ,
7+ unlinkSync ,
78} from 'fs' ;
89import {
910 parse ,
@@ -15,18 +16,25 @@ import {
1516import Logger from './logger.js' ;
1617import toTypescriptObject from './to-typescript-object.js' ;
1718import loadKeys from './loadKeys.js' ;
18- import { unlinkSync } from "node:fs" ;
1919
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 } ` , ) ;
2431 }
2532 }
2633 const yamlFiles = readdirSync ( `${ cwd } /${ ORIGIN_DIRECTORY } ` , 'utf8' , )
2734 . filter ( ( file , ) => file . endsWith ( '.yml' , ) , ) ;
2835
2936 const files = [ ] ;
37+ // eslint-disable-next-line complexity
3038 yamlFiles . forEach ( ( yamlFile , ) => {
3139 const lang = yamlFile . replace ( '.yml' , '' , ) ;
3240 const yamlPath = `${ ORIGIN_DIRECTORY } /${ yamlFile } ` ;
@@ -39,22 +47,45 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
3947
4048 const content = readFileSync ( yamlPath , 'utf8' , ) ;
4149 const data = parse ( content , ) ;
42- if ( shouldSplit ) {
50+ const typeName = isStrictTyping ? 'langType' : 'Partial<langType>' ;
51+ if ( shouldSplit && typeof data [ Object . keys ( data , ) . pop ( ) ] !== 'string' ) {
4352 for ( const key of Object . keys ( data , ) ) {
4453 writeFileSync (
4554 `${ 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` ,
4758 'utf8' ,
4859 ) ;
4960 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+ }
5070 }
5171 } else {
5272 writeFileSync (
5373 `${ 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` ,
5577 'utf8' ,
5678 ) ;
5779 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+ }
5889 }
5990 if ( lang === 'en' ) {
6091 const keys = loadKeys ( data , ) ;
@@ -78,20 +109,22 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
78109 ) ;
79110 writeFileSync (
80111 TARGET_DIR + '/files.ts' ,
81- `const files = ${ toTypescriptObject ( files ) } ;\nexport default files;\n` ,
112+ `const files = ${ toTypescriptObject ( files , ) } ;\nexport default files;\n` ,
82113 'utf8' ,
83114 ) ;
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+ ) ;
90129 }
91- fileExporter += '\n};' ;
92- writeFileSync (
93- TARGET_DIR + '/translations.ts' ,
94- `${ fileImporter } ${ fileExporter } \nexport default translations;\n` ,
95- 'utf8' ,
96- ) ;
97130} ;
0 commit comments