1- const path_module = require ( 'path' ) ;
2- const emojic = require ( 'emojic' ) ;
3- const chalk = require ( 'chalk' ) ;
4- const glob = require ( 'glob' ) ;
5- const directory = path_module . join ( __dirname , '..' , 'src' , 'i18n' ) ;
1+ import path from 'path' ;
2+ import { fileURLToPath } from 'url' ;
3+ import emojic from 'emojic' ;
4+ import chalk from 'chalk' ;
5+ import glob from 'glob' ;
6+
7+ const __filename = fileURLToPath ( import . meta. url ) ;
8+ const __dirname = path . dirname ( __filename ) ;
9+
10+ const directory = path . join ( __dirname , '..' , 'src' , 'i18n' ) ;
611
712/**
813 * Flattens an object recursively so that any nested objects are referred to on the root object using
@@ -30,7 +35,7 @@ const directory = path_module.join(__dirname, '..', 'src', 'i18n');
3035const flattenObject = ( obj , cumulative = [ ] ) => {
3136 let keys = { } ;
3237
33- for ( key of Object . keys ( obj ) ) {
38+ for ( const key of Object . keys ( obj ) ) {
3439 if ( typeof obj [ key ] === 'object' ) {
3540 const subKeys = flattenObject ( obj [ key ] , cumulative . concat ( key ) ) ;
3641 keys = { ...keys , ...subKeys } ;
@@ -56,7 +61,7 @@ const compareKeys = (obj1, obj2) => {
5661 total : Object . keys ( obj1 ) . length
5762 } ;
5863
59- for ( key of Object . keys ( obj1 ) ) {
64+ for ( const key of Object . keys ( obj1 ) ) {
6065 if ( ! ( key in obj2 ) ) {
6166 result . missing . push ( { key, text : obj1 [ key ] } ) ;
6267 } else {
@@ -72,22 +77,22 @@ const compareKeys = (obj1, obj2) => {
7277 * @param {string } reference The reference object. A lang file that has been loaded and flattened.
7378 * @param {string } path The full module path of the module to compare to the reference.
7479 */
75- const validateLangFile = async ( reference , path , verbose ) => {
76- console . log ( `Processing ${ chalk . green ( path_module . relative ( process . cwd ( ) , path ) ) } ` ) ;
80+ const validateLangFile = async ( reference , filePath , verbose ) => {
81+ console . log ( `Processing ${ chalk . green ( path . relative ( process . cwd ( ) , filePath ) ) } ` ) ;
7782
7883 const stats = {
7984 coverage : 0 ,
8085 total : 0 ,
8186 missing : 0
8287 } ;
8388
84- const lang = await import ( path ) ;
89+ const lang = await import ( filePath ) ;
8590 const langFlattened = flattenObject ( lang . default ) ;
8691
8792 const result = compareKeys ( reference , langFlattened ) ;
8893
8994 if ( verbose ) {
90- for ( key of Object . keys ( reference ) ) {
95+ for ( const key of Object . keys ( reference ) ) {
9196 if ( key in langFlattened ) {
9297 console . log ( chalk . green ( `${ key } : ${ langFlattened [ key ] } ` ) ) ;
9398 } else {
@@ -103,10 +108,10 @@ const validateLangFile = async (reference, path, verbose) => {
103108 if ( result . missing . length ) {
104109 console . log ( `${ emojic . x } Found ${ result . missing . length } missing keys` ) ;
105110
106- for ( missing of result . missing ) {
111+ for ( const missing of result . missing ) {
107112 console . log (
108113 chalk . red (
109- `Missing translation for ${ path_module . basename ( path ) } -> ${ missing . key } = "${
114+ `Missing translation for ${ path . basename ( filePath ) } -> ${ missing . key } = "${
110115 missing . text
111116 } "`
112117 )
@@ -123,7 +128,7 @@ const validateLangFile = async (reference, path, verbose) => {
123128
124129const run = async ( ) => {
125130 // Load the 'en' lang file to act as the reference for all others
126- const en = await import ( path_module . join ( directory , 'en.js' ) ) ;
131+ const en = await import ( path . join ( directory , 'en.js' ) ) ;
127132 const enBenchmark = flattenObject ( en . default , [ ] ) ;
128133
129134 const args = process . argv . slice ( 2 ) ;
@@ -132,13 +137,13 @@ const run = async () => {
132137 const verbose = args . includes ( '-v' ) ;
133138
134139 // Grab all the module files we want to compare to
135- const modules = glob . sync ( path_module . join ( __dirname , '..' , 'src' , 'i18n' , filePattern ) ) ;
140+ const modules = glob . sync ( path . join ( __dirname , '..' , 'src' , 'i18n' , filePattern ) ) ;
136141 let files = 0 ;
137142 let coverage = 0 ;
138143 let total = 0 ;
139144 let missing = 0 ;
140145
141- for ( file of modules ) {
146+ for ( const file of modules ) {
142147 const stats = await validateLangFile ( enBenchmark , file , verbose ) ;
143148 console . log ( '' ) ;
144149
0 commit comments