({
rootPath,
sourceFilePaths,
sourceLocale,
format,
outputPath: output,
logger,
useSourceMaps,
useLegacyIds,
duplicateMessageHandling,
formatOptions = {},
fileSystem: fs,
}: ExtractTranslationsOptions)
| 77 | } |
| 78 | |
| 79 | export function extractTranslations({ |
| 80 | rootPath, |
| 81 | sourceFilePaths, |
| 82 | sourceLocale, |
| 83 | format, |
| 84 | outputPath: output, |
| 85 | logger, |
| 86 | useSourceMaps, |
| 87 | useLegacyIds, |
| 88 | duplicateMessageHandling, |
| 89 | formatOptions = {}, |
| 90 | fileSystem: fs, |
| 91 | }: ExtractTranslationsOptions) { |
| 92 | const basePath = fs.resolve(rootPath); |
| 93 | const extractor = new MessageExtractor(fs, logger, {basePath, useSourceMaps}); |
| 94 | |
| 95 | const messages: ɵParsedMessage[] = []; |
| 96 | for (const file of sourceFilePaths) { |
| 97 | messages.push(...extractor.extractMessages(file)); |
| 98 | } |
| 99 | |
| 100 | const diagnostics = checkDuplicateMessages(fs, messages, duplicateMessageHandling, basePath); |
| 101 | if (diagnostics.hasErrors) { |
| 102 | throw new Error(diagnostics.formatDiagnostics('Failed to extract messages')); |
| 103 | } |
| 104 | |
| 105 | const outputPath = fs.resolve(rootPath, output); |
| 106 | const serializer = getSerializer( |
| 107 | format, |
| 108 | sourceLocale, |
| 109 | fs.dirname(outputPath), |
| 110 | useLegacyIds, |
| 111 | formatOptions, |
| 112 | fs, |
| 113 | diagnostics, |
| 114 | ); |
| 115 | const translationFile = serializer.serialize(messages); |
| 116 | fs.ensureDir(fs.dirname(outputPath)); |
| 117 | fs.writeFile(outputPath, translationFile); |
| 118 | |
| 119 | if (diagnostics.messages.length) { |
| 120 | logger.warn(diagnostics.formatDiagnostics('Messages extracted with warnings')); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | function getSerializer( |
| 125 | format: string, |
no test coverage detected
searching dependent graphs…