Skip to content

Commit 489fb3a

Browse files
author
Timofei Iatsenko
authored
feat: full concurrency for extract / compile commands (#2299)
1 parent 30e5242 commit 489fb3a

13 files changed

Lines changed: 225 additions & 172 deletions

File tree

packages/cli/src/api/__snapshots__/catalog.test.ts.snap

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,13 @@ exports[`Catalog collect should extract messages from source files 1`] = `
141141
},
142142
Hello World: {
143143
comments: [
144-
Hello comment,
145144
Comment A,
146145
Comment A again,
146+
Hello comment,
147147
],
148148
context: undefined,
149149
message: undefined,
150150
origin: [
151-
[
152-
collect/componentA/index.js,
153-
1,
154-
],
155151
[
156152
collect/componentA/componentA.js,
157153
2,
@@ -160,6 +156,10 @@ exports[`Catalog collect should extract messages from source files 1`] = `
160156
collect/componentA/componentA.js,
161157
3,
162158
],
159+
[
160+
collect/componentA/index.js,
161+
1,
162+
],
163163
],
164164
placeholders: {},
165165
},
@@ -194,17 +194,13 @@ exports[`Catalog collect should extract only files passed on options 1`] = `
194194
},
195195
Hello World: {
196196
comments: [
197-
Hello comment,
198197
Comment A,
199198
Comment A again,
199+
Hello comment,
200200
],
201201
context: undefined,
202202
message: undefined,
203203
origin: [
204-
[
205-
collect/componentA/index.js,
206-
1,
207-
],
208204
[
209205
collect/componentA/componentA.js,
210206
2,
@@ -213,6 +209,10 @@ exports[`Catalog collect should extract only files passed on options 1`] = `
213209
collect/componentA/componentA.js,
214210
3,
215211
],
212+
[
213+
collect/componentA/index.js,
214+
1,
215+
],
216216
],
217217
placeholders: {},
218218
},
@@ -362,9 +362,9 @@ exports[`Catalog make should collect and write catalogs 2`] = `
362362
},
363363
Hello World: {
364364
comments: [
365-
Hello comment,
366365
Comment A,
367366
Comment A again,
367+
Hello comment,
368368
js-lingui-explicit-id,
369369
],
370370
context: null,
@@ -374,10 +374,6 @@ exports[`Catalog make should collect and write catalogs 2`] = `
374374
},
375375
obsolete: false,
376376
origin: [
377-
[
378-
collect/componentA/index.js,
379-
1,
380-
],
381377
[
382378
collect/componentA/componentA.js,
383379
2,
@@ -386,6 +382,10 @@ exports[`Catalog make should collect and write catalogs 2`] = `
386382
collect/componentA/componentA.js,
387383
3,
388384
],
385+
[
386+
collect/componentA/index.js,
387+
1,
388+
],
389389
],
390390
translation: ,
391391
},
@@ -429,9 +429,9 @@ exports[`Catalog make should collect and write catalogs 2`] = `
429429
},
430430
Hello World: {
431431
comments: [
432-
Hello comment,
433432
Comment A,
434433
Comment A again,
434+
Hello comment,
435435
js-lingui-explicit-id,
436436
],
437437
context: null,
@@ -441,10 +441,6 @@ exports[`Catalog make should collect and write catalogs 2`] = `
441441
},
442442
obsolete: false,
443443
origin: [
444-
[
445-
collect/componentA/index.js,
446-
1,
447-
],
448444
[
449445
collect/componentA/componentA.js,
450446
2,
@@ -453,6 +449,10 @@ exports[`Catalog make should collect and write catalogs 2`] = `
453449
collect/componentA/componentA.js,
454450
3,
455451
],
452+
[
453+
collect/componentA/index.js,
454+
1,
455+
],
456456
],
457457
translation: ,
458458
},
@@ -606,9 +606,9 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
606606
},
607607
Hello World: {
608608
comments: [
609-
Hello comment,
610609
Comment A,
611610
Comment A again,
611+
Hello comment,
612612
js-lingui-explicit-id,
613613
],
614614
context: null,
@@ -618,10 +618,6 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
618618
},
619619
obsolete: false,
620620
origin: [
621-
[
622-
collect/componentA/index.js,
623-
1,
624-
],
625621
[
626622
collect/componentA/componentA.js,
627623
2,
@@ -630,6 +626,10 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
630626
collect/componentA/componentA.js,
631627
3,
632628
],
629+
[
630+
collect/componentA/index.js,
631+
1,
632+
],
633633
],
634634
translation: ,
635635
},
@@ -757,9 +757,9 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
757757
},
758758
Hello World: {
759759
comments: [
760-
Hello comment,
761760
Comment A,
762761
Comment A again,
762+
Hello comment,
763763
js-lingui-explicit-id,
764764
],
765765
context: null,
@@ -769,10 +769,6 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
769769
},
770770
obsolete: false,
771771
origin: [
772-
[
773-
collect/componentA/index.js,
774-
1,
775-
],
776772
[
777773
collect/componentA/componentA.js,
778774
2,
@@ -781,6 +777,10 @@ exports[`Catalog make should merge with existing catalogs 2`] = `
781777
collect/componentA/componentA.js,
782778
3,
783779
],
780+
[
781+
collect/componentA/index.js,
782+
1,
783+
],
784784
],
785785
translation: ,
786786
},
@@ -849,9 +849,9 @@ exports[`Catalog make should only update the specified locale 2`] = `
849849
},
850850
Hello World: {
851851
comments: [
852-
Hello comment,
853852
Comment A,
854853
Comment A again,
854+
Hello comment,
855855
js-lingui-explicit-id,
856856
],
857857
context: null,
@@ -861,10 +861,6 @@ exports[`Catalog make should only update the specified locale 2`] = `
861861
},
862862
obsolete: false,
863863
origin: [
864-
[
865-
collect/componentA/index.js,
866-
1,
867-
],
868864
[
869865
collect/componentA/componentA.js,
870866
2,
@@ -873,6 +869,10 @@ exports[`Catalog make should only update the specified locale 2`] = `
873869
collect/componentA/componentA.js,
874870
3,
875871
],
872+
[
873+
collect/componentA/index.js,
874+
1,
875+
],
876876
],
877877
translation: ,
878878
},
@@ -922,9 +922,9 @@ exports[`Catalog makeTemplate should collect and write a template 2`] = `
922922
},
923923
Hello World: {
924924
comments: [
925-
Hello comment,
926925
Comment A,
927926
Comment A again,
927+
Hello comment,
928928
js-lingui-explicit-id,
929929
],
930930
context: null,
@@ -934,10 +934,6 @@ exports[`Catalog makeTemplate should collect and write a template 2`] = `
934934
},
935935
obsolete: false,
936936
origin: [
937-
[
938-
collect/componentA/index.js,
939-
1,
940-
],
941937
[
942938
collect/componentA/componentA.js,
943939
2,
@@ -946,6 +942,10 @@ exports[`Catalog makeTemplate should collect and write a template 2`] = `
946942
collect/componentA/componentA.js,
947943
3,
948944
],
945+
[
946+
collect/componentA/index.js,
947+
1,
948+
],
949949
],
950950
translation: ,
951951
},

packages/cli/src/api/catalog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ export class Catalog {
8080
}
8181

8282
async make(options: MakeOptions): Promise<AllCatalogsType | false> {
83-
const nextCatalog = await this.collect({ files: options.files })
83+
const [nextCatalog, prevCatalogs] = await Promise.all([
84+
this.collect({ files: options.files }),
85+
this.readAll(),
86+
])
87+
8488
if (!nextCatalog) return false
85-
const prevCatalogs = await this.readAll()
8689

8790
const catalogs = this.merge(prevCatalogs, nextCatalog, {
8891
overwrite: options.overwrite,

packages/cli/src/api/catalog/extractFromFiles.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,64 @@ export async function extractFromFiles(
3535
const messages: ExtractedCatalogType = {}
3636

3737
let catalogSuccess = true
38-
for (const filename of paths) {
39-
const fileSuccess = await extract(
40-
filename,
41-
(next: ExtractedMessage) => {
42-
if (!messages[next.id]) {
43-
messages[next.id] = {
44-
message: next.message,
45-
context: next.context,
46-
placeholders: {},
47-
comments: [],
48-
origin: [],
38+
39+
await Promise.all(
40+
paths.map(async (filename) => {
41+
const fileSuccess = await extract(
42+
filename,
43+
(next: ExtractedMessage) => {
44+
if (!messages[next.id]) {
45+
messages[next.id] = {
46+
message: next.message,
47+
context: next.context,
48+
placeholders: {},
49+
comments: [],
50+
origin: [],
51+
}
4952
}
50-
}
5153

52-
const prev = messages[next.id]
54+
const prev = messages[next.id]
5355

54-
// there might be a case when filename was not mapped from sourcemaps
55-
const filename = next.origin[0]
56-
? path.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/")
57-
: ""
56+
// there might be a case when filename was not mapped from sourcemaps
57+
const filename = next.origin[0]
58+
? path.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/")
59+
: ""
5860

59-
const origin: MessageOrigin = [filename, next.origin[1]]
61+
const origin: MessageOrigin = [filename, next.origin[1]]
6062

61-
if (prev.message && next.message && prev.message !== next.message) {
62-
throw new Error(
63-
`Encountered different default translations for message ${pico.yellow(
64-
next.id
65-
)}` +
66-
`\n${pico.yellow(prettyOrigin(prev.origin))} ${prev.message}` +
67-
`\n${pico.yellow(prettyOrigin([origin]))} ${next.message}`
68-
)
69-
}
63+
if (prev.message && next.message && prev.message !== next.message) {
64+
throw new Error(
65+
`Encountered different default translations for message ${pico.yellow(
66+
next.id
67+
)}` +
68+
`\n${pico.yellow(prettyOrigin(prev.origin))} ${prev.message}` +
69+
`\n${pico.yellow(prettyOrigin([origin]))} ${next.message}`
70+
)
71+
}
7072

71-
messages[next.id] = {
72-
...prev,
73-
message: prev.message ?? next.message,
74-
comments: next.comment
75-
? [...prev.comments, next.comment]
76-
: prev.comments,
77-
origin: [...prev.origin, [filename, next.origin[1]]],
78-
placeholders: mergePlaceholders(prev.placeholders, next.placeholders),
73+
messages[next.id] = {
74+
...prev,
75+
message: prev.message ?? next.message,
76+
comments: next.comment
77+
? [...prev.comments, next.comment].sort()
78+
: prev.comments,
79+
origin: (
80+
[...prev.origin, [filename, next.origin[1]]] as MessageOrigin[]
81+
).sort((a, b) => a[0].localeCompare(b[0])),
82+
placeholders: mergePlaceholders(
83+
prev.placeholders,
84+
next.placeholders
85+
),
86+
}
87+
},
88+
config,
89+
{
90+
extractors: config.extractors as ExtractorType[],
7991
}
80-
},
81-
config,
82-
{
83-
extractors: config.extractors as ExtractorType[],
84-
}
85-
)
86-
catalogSuccess &&= fileSuccess
87-
}
92+
)
93+
catalogSuccess &&= fileSuccess
94+
})
95+
)
8896

8997
if (!catalogSuccess) return undefined
9098

0 commit comments

Comments
 (0)