@@ -8,30 +8,12 @@ import { dataI18nKeyStrategy } from './strategies/data-i18n-key';
88import { ejsStrategy } from './strategies/ejs' ;
99import { appleStrategy } from './strategies/apple' ;
1010import { androidStrategy } from './strategies/android' ;
11- import { StrategyConfig } from './strategies/strategy-config' ;
12-
13- function findFiles ( dir : string , exts : string [ ] , excludeDirs : string [ ] ) : string [ ] {
14- let results : string [ ] = [ ] ;
15- const list = fs . readdirSync ( dir ) ;
16- for ( const file of list ) {
17- const filePath = path . join ( dir , file ) ;
18- const stat = fs . statSync ( filePath ) ;
19- if ( stat && stat . isDirectory ( ) ) {
20- if ( excludeDirs . includes ( file ) ) continue ;
21- results = results . concat ( findFiles ( filePath , exts , excludeDirs ) ) ;
22- } else {
23- if ( exts . some ( ext => filePath . endsWith ( ext ) ) ) {
24- results . push ( filePath ) ;
25- }
26- }
27- }
28- return results ;
29- }
11+ import glob from 'glob' ;
3012
3113const STRATEGY_MAP : Record < string , any > = {
3214 'i18next' : i18nextStrategy ,
3315 'react-intl' : reactIntlStrategy ,
34- 'html- data-attributes ' : dataI18nKeyStrategy ,
16+ 'data-i18n-key ' : dataI18nKeyStrategy ,
3517 'ejs' : ejsStrategy ,
3618 'apple' : appleStrategy ,
3719 'android' : androidStrategy
@@ -41,22 +23,24 @@ const program = new Command();
4123
4224program
4325 . option ( '--config <file>' , 'Path to strategy config JSON file' )
44- . option ( '--searchDir <dir>' , '(Optional) Choose where to search. Default: ./' , './' )
45- . option ( '--output <file>' , '(Optional) Choose where to save results. Default: ./extraction.json' , './extraction.json' )
26+ . option ( '--strategy <name>' , '(Optional) Extraction strategy to use (overrides config file)' )
27+ . option ( '--src <pattern>' , '(Optional) Glob pattern for files to search. Default: ./' , './' )
28+ . option ( '--out <file>' , '(Optional) Choose where to save results. Default: ./extraction.json' , './extraction.json' )
4629 . action ( ( options ) => {
47- const { config, searchDir , output } = options ;
30+ const { config, strategy : cliStrategy , src , out } = options ;
4831 if ( ! config ) {
4932 console . error ( 'Config file is required.' ) ;
5033 process . exit ( 1 ) ;
5134 }
5235
53- let userConfig : StrategyConfig = { } ;
36+ let userConfig : CliConfig = { } ;
5437 if ( config && require ( 'fs' ) . existsSync ( config ) ) {
55- userConfig = JSON . parse ( require ( 'fs' ) . readFileSync ( config , 'utf-8' ) ) as StrategyConfig ;
38+ userConfig = JSON . parse ( require ( 'fs' ) . readFileSync ( config , 'utf-8' ) ) as CliConfig ;
5639 }
57- const strategyName = userConfig ?. strategy || "" ;
40+ // Prefer CLI --strategy over config file
41+ const strategyName = cliStrategy || userConfig ?. strategy || "" ;
5842 if ( ! strategyName || ! STRATEGY_MAP [ strategyName ] ) {
59- console . error ( 'Unknown or missing strategy in config file . Given strategy:' , strategyName ) ;
43+ console . error ( 'Unknown or missing strategy. Given strategy:' , strategyName ) ;
6044 console . error ( 'Available strategies:' , Object . keys ( STRATEGY_MAP ) . join ( ', ' ) ) ;
6145 process . exit ( 1 ) ;
6246 }
@@ -66,14 +50,13 @@ program
6650 process . exit ( 1 ) ;
6751 }
6852 const defaultConfig = strategy . getDefaultConfig ( ) ;
69- const effectiveConfig = { ...defaultConfig , ...userConfig } ;
53+ const effectiveConfig = { ...defaultConfig , ...userConfig , strategy : strategyName } ;
7054
71- const includeExtensions = effectiveConfig . includeExtensions || [ ] ;
72- const excludeDirs = effectiveConfig . excludeDirs || [ ] ;
73- const files = findFiles ( searchDir , includeExtensions , excludeDirs ) ;
55+ // Use glob to find files
56+ const files = glob . sync ( src , { ignore : effectiveConfig . excludeDirs || [ ] } ) ;
7457 const results = strategy ( files , effectiveConfig ) ;
75- fs . writeFileSync ( path . resolve ( output ) , JSON . stringify ( results , null , 2 ) ) ;
76- console . log ( 'Extraction complete. Output saved to' , output ) ;
58+ fs . writeFileSync ( path . resolve ( out ) , JSON . stringify ( results , null , 2 ) ) ;
59+ console . log ( 'Extraction complete. Output saved to' , out ) ;
7760 } ) ;
7861
7962program . parse ( process . argv ) ;
0 commit comments