1
+ const fs = require ( 'fs' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const PropertiesReader = require ( 'properties-reader' ) ;
4
+ const messageBundle = path . normalize ( process . argv [ 2 ] ) ;
5
+ const outputFile = path . normalize ( `${ process . argv [ 3 ] } /i18n-defaults.js` ) ;
6
+
7
+ if ( ! messageBundle || ! outputFile ) {
8
+ return ;
9
+ }
10
+
11
+ const properties = PropertiesReader ( messageBundle ) . _properties ;
12
+
13
+ /*
14
+ * Returns the single text funtion to enable single export.
15
+ *
16
+ * Example:
17
+ * const ARIA_LABEL_CARD_CONTENT = {
18
+ * key: "ARIA_LABEL_CARD_CONTENT",
19
+ * defaultText: "Card Content",
20
+ * };
21
+ */
22
+ const getTextMethods = ( properties ) => {
23
+ return Object . keys ( properties ) . map ( prop => `const ${ prop } = {key: "${ prop } ", defaultText: "${ properties [ prop ] } "};` ) . join ( '' ) ;
24
+ }
25
+
26
+ /*
27
+ * Returns the content of i18n-defaults.js,
28
+ * combining the single text methods and the export statement at the end of the file.
29
+ *
30
+ * Example:
31
+ * export {
32
+ * ARIA_LABEL_CARD_CONTENT,
33
+ * }
34
+ */
35
+ const getOutputFileContent = ( properties ) => {
36
+ return `${ getTextMethods ( properties ) } export {${ Object . keys ( properties ) . join ( ) } }; //eslint-disable-line` ;
37
+ }
38
+
39
+ // Writes the i18n-defaults.js
40
+ fs . writeFile ( outputFile , getOutputFileContent ( properties ) , function ( err ) {
41
+ if ( err ) {
42
+ return console . log ( err ) ;
43
+ }
44
+
45
+ console . log ( `The ${ outputFile } file has been created` ) ;
46
+ } ) ;
0 commit comments