11import chalk from "chalk"
2+ import chokidar from "chokidar"
23import program from "commander"
34
45import { getConfig , LinguiConfig } from "@lingui/conf"
@@ -15,6 +16,7 @@ export type CliExtractOptions = {
1516 overwrite : boolean
1617 locale : string
1718 prevFormat : string | null
19+ watch ?: boolean
1820}
1921
2022export default function command (
@@ -33,6 +35,7 @@ export default function command(
3335 process . env . LINGUI_EXTRACT = "1"
3436
3537 options . verbose && console . error ( "Extracting messages from source files…" )
38+
3639 const catalogs = getCatalogs ( config )
3740 const catalogStats : { [ path : string ] : AllCatalogsType } = { }
3841 catalogs . forEach ( ( catalog ) => {
@@ -51,16 +54,19 @@ export default function command(
5154 console . log ( )
5255 } )
5356
54- console . error (
55- `(use "${ chalk . yellow (
56- helpRun ( "extract" )
57- ) } " to update catalogs with new messages)`
58- )
59- console . error (
60- `(use "${ chalk . yellow (
61- helpRun ( "compile" )
62- ) } " to compile catalogs for production)`
63- )
57+ if ( ! options . watch ) {
58+ console . error (
59+ `(use "${ chalk . yellow (
60+ helpRun ( "extract" )
61+ ) } " to update catalogs with new messages)`
62+ )
63+ console . error (
64+ `(use "${ chalk . yellow (
65+ helpRun ( "compile" )
66+ ) } " to compile catalogs for production)`
67+ )
68+ }
69+
6470 return true
6571}
6672
@@ -75,6 +81,7 @@ if (require.main === module) {
7581 "--convert-from <format>" ,
7682 "Convert from previous format of message catalogs"
7783 )
84+ . option ( "--watch" , "Enables Watch Mode" )
7885 // Obsolete options
7986 . option (
8087 "--babelOptions" ,
@@ -127,14 +134,46 @@ if (require.main === module) {
127134 }
128135
129136 if ( hasErrors ) process . exit ( 1 )
137+
138+ const extract = ( filePath ?: string ) =>
139+ command ( config , {
140+ verbose : program . watch || program . verbose || false ,
141+ clean : program . watch ? false : program . clean || false ,
142+ overwrite : program . watch || program . overwrite || false ,
143+ locale : program . locale ,
144+ watch : program . watch || false ,
145+ files : filePath ? [ filePath ] : undefined ,
146+ prevFormat,
147+ } )
148+
130149
131- const result = command ( config , {
132- verbose : program . verbose || false ,
133- clean : program . clean || false ,
134- overwrite : program . overwrite || false ,
135- locale : program . locale ,
136- prevFormat ,
137- } )
150+ // Check if Watch Mode is enabled
151+ if ( program . watch ) {
152+ console . info ( chalk . bold ( "Initializing Watch Mode..." ) )
153+
154+ const catalogs = getCatalogs ( config )
155+ let paths = [ ] ;
156+ let ignored = [ ] ;
138157
139- if ( ! result ) process . exit ( 1 )
158+ catalogs . forEach ( ( catalog ) => {
159+ paths . push ( ...catalog . include ) ;
160+ ignored . push ( ...catalog . exclude ) ;
161+ } )
162+
163+ const watcher = chokidar . watch ( paths , {
164+ ignored : [ '/(^|[\/\\])\../' , ...ignored ] ,
165+ persistent : true ,
166+ } ) ;
167+
168+ const onReady = ( ) => {
169+ console . info ( chalk . green . bold ( "Watcher is ready!" ) )
170+ watcher . on ( 'add' , ( path ) => extract ( path ) ) . on ( 'change' , ( path ) => extract ( path ) ) ;
171+ } ;
172+
173+ watcher . on ( 'ready' , ( ) => onReady ( ) ) ;
174+ } else {
175+ const result = extract ( ) ;
176+
177+ if ( ! result ) process . exit ( 1 )
178+ }
140179}
0 commit comments