forked from lingui/js-lingui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractFromFiles.ts
More file actions
147 lines (123 loc) · 3.63 KB
/
extractFromFiles.ts
File metadata and controls
147 lines (123 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { ExtractedMessage, LinguiConfigNormalized } from "@lingui/conf"
import pico from "picocolors"
import path from "path"
import extract from "../extractors"
import { ExtractedCatalogType, MessageOrigin } from "../types"
import { prettyOrigin } from "../utils"
import { Pool, spawn, Worker } from "threads"
import type { ExtractWorkerFunction } from "../../workers/extractWorker"
function mergePlaceholders(
prev: Record<string, string[]>,
next: Record<string, string>
) {
const res = { ...prev }
// Handle case where next is null or undefined
if (!next) return res
Object.entries(next).forEach(([key, value]) => {
if (!res[key]) {
res[key] = []
}
if (!res[key].includes(value)) {
res[key].push(value)
}
res[key].sort()
})
return res
}
export async function extractFromFiles(
paths: string[],
config: LinguiConfigNormalized
) {
const messages: ExtractedCatalogType = {}
let catalogSuccess = true
if (config.experimental?.multiThread) {
catalogSuccess = await extractFromFilesWithWorkers(paths, config, messages)
} else {
for (const filename of paths) {
const fileSuccess = await extract(
filename,
(next: ExtractedMessage) => {
mergeExtractedMessage(next, messages, config)
},
config
)
catalogSuccess &&= fileSuccess
}
}
if (!catalogSuccess) return undefined
return messages
}
function mergeExtractedMessage(
next: ExtractedMessage,
messages: ExtractedCatalogType,
config: LinguiConfigNormalized
) {
if (!messages[next.id]) {
messages[next.id] = {
message: next.message,
context: next.context,
placeholders: {},
comments: [],
origin: [],
}
}
const prev = messages[next.id]
// there might be a case when filename was not mapped from sourcemaps
const filename = next.origin[0]
? path.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/")
: ""
const origin: MessageOrigin = [filename, next.origin[1]]
if (prev.message && next.message && prev.message !== next.message) {
throw new Error(
`Encountered different default translations for message ${pico.yellow(
next.id
)}` +
`\n${pico.yellow(prettyOrigin(prev.origin))} ${prev.message}` +
`\n${pico.yellow(prettyOrigin([origin]))} ${next.message}`
)
}
messages[next.id] = {
...prev,
message: prev.message ?? next.message,
comments: next.comment
? [...prev.comments, next.comment].sort()
: prev.comments,
origin: (
[...prev.origin, [filename, next.origin[1]]] as MessageOrigin[]
).sort((a, b) => a[0].localeCompare(b[0])),
placeholders: mergePlaceholders(prev.placeholders, next.placeholders),
}
}
async function extractFromFilesWithWorkers(
paths: string[],
config: LinguiConfigNormalized,
messages: ExtractedCatalogType
): Promise<boolean> {
const pool = Pool(() =>
spawn<ExtractWorkerFunction>(new Worker("../../workers/extractWorker"))
)
let catalogSuccess = true
try {
if (!config.resolvedConfigPath) {
throw new Error(
"Multithreading is only supported when lingui config loaded from file system, not passed by API"
)
}
paths.map((filename) =>
pool.queue(async (worker) => {
const result = await worker(filename, config.resolvedConfigPath)
if (!result.success) {
catalogSuccess = false
} else {
result.messages.forEach((message) => {
mergeExtractedMessage(message, messages, config)
})
}
})
)
await pool.completed(true)
} finally {
await pool.terminate()
}
return catalogSuccess
}