( inputFiles: string[], opts: Opts )
| 119 | } |
| 120 | |
| 121 | async function compileWithCustomFormatter( |
| 122 | inputFiles: string[], |
| 123 | opts: Opts |
| 124 | ): Promise<string> { |
| 125 | if (typeof opts.format === 'string') { |
| 126 | await warn(CUSTOM_FORMATTER_FILE_DEPRECATION_WARNING) |
| 127 | } |
| 128 | |
| 129 | const formatter = await resolveCustomFormatter(opts.format) |
| 130 | const files = globSync(inputFiles, { |
| 131 | followSymbolicLinks: opts.followLinks ?? true, |
| 132 | }) |
| 133 | |
| 134 | if (!files.length) { |
| 135 | throw new Error('No translation files found matching the patterns') |
| 136 | } |
| 137 | |
| 138 | const messages: NativeCompileMessage[] = [] |
| 139 | await Promise.all( |
| 140 | files.map(async file => { |
| 141 | opts.signal?.throwIfAborted() |
| 142 | const content = await readFile(file, { |
| 143 | encoding: 'utf8', |
| 144 | signal: opts.signal, |
| 145 | }) |
| 146 | const compiled = formatter.compile(JSON.parse(content)) |
| 147 | for (const id of Object.keys(compiled)) { |
| 148 | messages.push({ |
| 149 | id, |
| 150 | message: compiled[id], |
| 151 | sourceFile: file, |
| 152 | }) |
| 153 | } |
| 154 | }) |
| 155 | ) |
| 156 | |
| 157 | const serialized = compileMessagesWithNative(messages, { |
| 158 | ast: opts.ast, |
| 159 | ignoreTag: opts.ignoreTag, |
| 160 | pseudoLocale: opts.pseudoLocale, |
| 161 | skipErrors: opts.skipErrors, |
| 162 | }) |
| 163 | |
| 164 | if (formatter.compareMessages) { |
| 165 | return ( |
| 166 | stringify(JSON.parse(serialized), { |
| 167 | space: 2, |
| 168 | cmp: formatter.compareMessages, |
| 169 | }) ?? '' |
| 170 | ) |
| 171 | } |
| 172 | |
| 173 | return serialized |
| 174 | } |
| 175 | |
| 176 | async function resolveCustomFormatter( |
| 177 | format: string | Formatter<unknown> | undefined |
no test coverage detected