11#!/usr/bin/env node
22
3- import fs from 'node:fs'
3+ import fs from 'node:fs' ;
44import path from 'node:path' ;
5- import meow from 'meow' ;
6- import chalk from 'chalk' ;
75import { parse } from '@babel/parser' ;
6+ import chalk from 'chalk' ;
7+ import meow from 'meow' ;
8+ import pkg from '../package.json' with { type : 'json' } ;
89import { launchApp } from './app.js' ;
9- import pkg from '../package.json' with { type : "json" } ;
1010
1111const CLI_NAME = Object . keys ( pkg . bin ) [ 0 ] ;
1212const CMDS = [ 'edit' , 'check-missing-translations' ] ;
13- const getCommand = cmd => CMDS . includes ( cmd ) ? cmd : CMDS [ 0 ] ;
13+ const getCommand = ( cmd ) => ( CMDS . includes ( cmd ) ? cmd : CMDS [ 0 ] ) ;
1414
15- const cli = meow ( `
15+ const cli = meow (
16+ `
1617 Usage:
1718 $ ${ CLI_NAME } <cmd> <entry> <locale> [db]
1819
@@ -30,37 +31,38 @@ const cli = meow(`
3031
3132 Example:
3233 $ ${ CLI_NAME } edit ./index.js es
33- ` , {
34- importMeta : import . meta,
35- } ) ;
36-
34+ ` ,
35+ {
36+ importMeta : import . meta,
37+ } ,
38+ ) ;
3739
3840const args = [ ...cli . input ] ;
39- const flags = { ...cli . flags , root : cli . flags . root || process . env . PWD }
41+ const flags = { ...cli . flags , root : cli . flags . root || process . env . PWD } ;
4042
4143const flag = {
42- error : msg => chalk . red ( `\n${ msg } \nTry \`${ CLI_NAME } --help\` for more informations.\n` )
44+ error : ( msg ) => chalk . red ( `\n${ msg } \nTry \`${ CLI_NAME } --help\` for more informations.\n` ) ,
4345} ;
4446
4547if ( ! CMDS . includes ( args [ 0 ] ) ) {
4648 args . unshift ( CMDS [ 0 ] ) ;
4749}
4850
4951const options = {
50- cmd : args [ 0 ] , // command with default fallback
51- entry : args [ 1 ] , // entry file
52- locale : args [ 2 ] , // locale to translate to
53- db : args [ 3 ] || './i18n.db.json' // db file
52+ cmd : args [ 0 ] , // command with default fallback
53+ entry : args [ 1 ] , // entry file
54+ locale : args [ 2 ] , // locale to translate to
55+ db : args [ 3 ] || './i18n.db.json' , // db file
5456} ;
5557
5658if ( ! options . entry ) {
5759 console . log ( `${ flag . error ( `Missing <entry> argument.` ) } ` ) ;
58- process . exit ( 1 ) ;
60+ process . exit ( 1 ) ;
5961}
6062
6163if ( options . cmd === CMDS [ 0 ] && ! options . locale ) {
6264 console . log ( `${ flag . error ( `Missing <locale> argument.` ) } ` ) ;
63- process . exit ( 1 ) ;
65+ process . exit ( 1 ) ;
6466}
6567
6668const parserOptions = {
@@ -89,16 +91,16 @@ const parserOptions = {
8991 'objectRestSpread' ,
9092 'optionalCatchBinding' ,
9193 'optionalChaining' ,
92- 'throwExpressions'
93- ]
94+ 'throwExpressions' ,
95+ ] ,
9496} ;
9597
9698const NODE_PATH = process . env . NODE_PATH || '' ;
9799const fileCache = [ ] ;
98100const indexedFiles = [ ] ;
99101const db = {
100102 fileSystem : JSON . parse ( fs . readFileSync ( options . db ) || { } ) ,
101- indexed : { }
103+ indexed : { } ,
102104} ;
103105
104106traverseFiles ( options . entry ) ;
@@ -112,7 +114,7 @@ if (options.cmd === CMDS[0]) {
112114if ( options . cmd === CMDS [ 1 ] ) {
113115 if ( hasMissingTranslations ( db ) ) {
114116 console . log ( `Missing translations\n` ) ;
115- process . exit ( 1 ) ;
117+ process . exit ( 1 ) ;
116118 }
117119}
118120
@@ -124,21 +126,19 @@ console.log(`
124126 Missing: ${ Object . keys ( db . indexed ) . length - Object . keys ( db . fileSystem ) . length }
125127` ) ;
126128
127-
128-
129129function traverseFiles ( file ) {
130130 const code = fs . readFileSync ( file ) . toString ( ) ;
131131 const ast = parse ( code , parserOptions ) ;
132132 const basePath = path . dirname ( file ) ;
133- ast . program . body . forEach ( node => traverseNode ( node , basePath ) ) ;
133+ ast . program . body . forEach ( ( node ) => traverseNode ( node , basePath ) ) ;
134134}
135135
136136function traverseNode ( node , basePath ) {
137137 switch ( node . type ) {
138138 case 'ImportDeclaration' :
139139 let filePath = node . source . value ;
140140 let dirPath = NODE_PATH ;
141- const isRelativePath = filePath . startsWith ( '.' )
141+ const isRelativePath = filePath . startsWith ( '.' ) ;
142142 if ( flags . rootAlias && filePath . startsWith ( flags . rootAlias ) ) {
143143 filePath = filePath . replace ( new RegExp ( `^${ flags . rootAlias } \/?` ) , '' ) ;
144144 dirPath = flags . root ;
@@ -158,19 +158,21 @@ function traverseNode(node, basePath) {
158158 break ;
159159 case 'TaggedTemplateExpression' :
160160 if ( node . tag . name === 'i18n' ) {
161- const strings = node . quasi . quasis . map ( quasi => quasi . value . raw ) ;
161+ const strings = node . quasi . quasis . map ( ( quasi ) => quasi . value . raw ) ;
162162 const key = strings . join ( '\x01' ) ;
163- const translation = db . fileSystem [ key ] && db . fileSystem [ key ] [ options . locale || 'default' ] || strings . slice ( ) ;
163+ const translation =
164+ ( db . fileSystem [ key ] && db . fileSystem [ key ] [ options . locale || 'default' ] ) ||
165+ strings . slice ( ) ;
164166 // Skip empty strings & already existing keys
165167 if ( ! db . indexed [ key ] && translation . join ( '' ) !== '' ) {
166168 ( db . indexed [ key ] = { } ) [ options . locale || 'default' ] = translation ;
167169 }
168170 }
169171 break ;
170172 default :
171- for ( let key in node ) {
173+ for ( const key in node ) {
172174 if ( typeof node [ key ] === 'object' ) {
173- traverseNode ( ( node [ key ] || { } ) , basePath ) ;
175+ traverseNode ( node [ key ] || { } , basePath ) ;
174176 }
175177 }
176178 }
@@ -181,5 +183,5 @@ function hasMissingTranslations(db) {
181183 // TODO: check for missing language translations
182184 const totalIndexedDBKeys = Object . keys ( db . indexed ) . length ;
183185 const totalFileSystemDBKeys = Object . keys ( db . fileSystem ) . length ;
184- return ( totalIndexedDBKeys !== totalFileSystemDBKeys ) ;
186+ return totalIndexedDBKeys !== totalFileSystemDBKeys ;
185187}
0 commit comments