@@ -13,6 +13,11 @@ export type ExtractCLIOptions = Omit<OptionsSchema, 'overrideIdFn'> & {
13
13
ignore ?: GlobOptions [ 'ignore' ] ;
14
14
} ;
15
15
16
+ export type ExtractOptions = OptionsSchema & {
17
+ idInterpolationPattern ?: string ;
18
+ ignore ?: GlobOptions [ 'ignore' ] ;
19
+ } ;
20
+
16
21
function getBabelConfig (
17
22
reactIntlOptions : ExtractCLIOptions ,
18
23
extraBabelOptions : Partial < babel . TransformOptions > = { }
@@ -80,20 +85,15 @@ function getReactIntlMessages(
80
85
}
81
86
}
82
87
83
- export default async function extract (
88
+ export async function extract (
84
89
files : readonly string [ ] ,
85
- { outFile , idInterpolationPattern, ...extractOpts } : ExtractCLIOptions
90
+ { idInterpolationPattern, ...babelOpts } : ExtractOptions
86
91
) {
87
- let babelOpts : OptionsSchema = extractOpts ;
88
- if ( outFile ) {
89
- babelOpts . messagesDir = undefined ;
90
- }
91
- const printMessagesToStdout = babelOpts . messagesDir == null && ! outFile ;
92
92
let extractedMessages : Record < string , ExtractedMessageDescriptor > = { } ;
93
93
94
94
if ( files . length > 0 ) {
95
95
for ( const file of files ) {
96
- if ( idInterpolationPattern ) {
96
+ if ( ! babelOpts . overrideIdFn && idInterpolationPattern ) {
97
97
babelOpts = {
98
98
...babelOpts ,
99
99
overrideIdFn : ( id , defaultMessage , description ) =>
@@ -109,16 +109,14 @@ export default async function extract(
109
109
}
110
110
const babelResult = extractSingleFile ( file , babelOpts ) ;
111
111
const singleFileExtractedMessages = getReactIntlMessages ( babelResult ) ;
112
- // Aggregate result when we have to output to a single file
113
- if ( outFile || printMessagesToStdout ) {
114
- Object . assign ( extractedMessages , singleFileExtractedMessages ) ;
115
- }
112
+
113
+ Object . assign ( extractedMessages , singleFileExtractedMessages ) ;
116
114
}
117
115
} else {
118
116
if ( files . length === 0 && process . stdin . isTTY ) {
119
117
warn ( 'Reading source file from TTY.' ) ;
120
118
}
121
- if ( idInterpolationPattern ) {
119
+ if ( ! babelOpts . overrideIdFn && idInterpolationPattern ) {
122
120
babelOpts = {
123
121
...babelOpts ,
124
122
overrideIdFn : ( id , defaultMessage , description ) =>
@@ -137,10 +135,22 @@ export default async function extract(
137
135
stdinSource ,
138
136
getBabelConfig ( babelOpts )
139
137
) ;
140
- if ( printMessagesToStdout ) {
141
- extractedMessages = getReactIntlMessages ( babelResult ) ;
142
- }
138
+
139
+ extractedMessages = getReactIntlMessages ( babelResult ) ;
140
+ }
141
+ return Object . values ( extractedMessages ) ;
142
+ }
143
+
144
+ export default async function extractAndWrite (
145
+ files : readonly string [ ] ,
146
+ opts : ExtractCLIOptions
147
+ ) {
148
+ const { outFile, ...extractOpts } = opts ;
149
+ if ( outFile ) {
150
+ extractOpts . messagesDir = undefined ;
143
151
}
152
+ const extractedMessages = extract ( files , extractOpts ) ;
153
+ const printMessagesToStdout = extractOpts . messagesDir == null && ! outFile ;
144
154
if ( outFile ) {
145
155
outputJSONSync ( outFile , Object . values ( extractedMessages ) , {
146
156
spaces : 2 ,
0 commit comments