@@ -8,7 +8,7 @@ import * as fs from 'node:fs/promises';
8
8
import * as path from 'node:path' ;
9
9
10
10
import { getWorkspacePackages } from '../cli/pnpm.mjs' ;
11
- import { BASE_IGNORES , wrapInWorker } from './build.mjs' ;
11
+ import { BASE_IGNORES , mapConcurrently } from './build.mjs' ;
12
12
13
13
/**
14
14
* @typedef {Object } Args
@@ -18,12 +18,12 @@ import { BASE_IGNORES, wrapInWorker } from './build.mjs';
18
18
*/
19
19
20
20
/**
21
- * Extracts error codes from all files in a directory.
21
+ * Gets all relevant files for a package to parse.
22
+ *
22
23
* @param {import('../cli/pnpm.mjs').PublicPackage } pkg
23
- * @param {Set<string> } errors
24
- * @param {import('@mui/internal-babel-plugin-minify-errors').Options['detection'] } [detection='opt-in']
24
+ * @returns {Promise<string[]> } An array of file paths.
25
25
*/
26
- async function extractErrorCodesForPackage ( pkg , errors , detection = 'opt-in' ) {
26
+ async function getFilesForPackage ( pkg ) {
27
27
const srcPath = path . join ( pkg . path , 'src' ) ;
28
28
const srcPathExists = await fs
29
29
. stat ( srcPath )
@@ -42,11 +42,19 @@ async function extractErrorCodesForPackage(pkg, errors, detection = 'opt-in') {
42
42
] ,
43
43
cwd,
44
44
} ) ;
45
- const workerCount = Math . min ( 40 , files . length ) ;
46
- console . log ( `🔍 ${ pkg . name } : Processing ${ files . length } file${ files . length > 1 ? 's' : '' } ...` ) ;
47
- await wrapInWorker (
48
- async ( file ) => {
49
- const fullPath = path . join ( cwd , file ) ;
45
+ return files . map ( ( file ) => path . join ( cwd , file ) ) ;
46
+ }
47
+
48
+ /**
49
+ * Extracts error codes from all files in a directory.
50
+ * @param {string[] } files
51
+ * @param {Set<string> } errors
52
+ * @param {import('@mui/internal-babel-plugin-minify-errors').Options['detection'] } [detection='opt-in']
53
+ */
54
+ async function extractErrorCodesForWorkspace ( files , errors , detection = 'opt-in' ) {
55
+ await mapConcurrently (
56
+ files ,
57
+ async ( fullPath ) => {
50
58
const code = await fs . readFile ( fullPath , 'utf8' ) ;
51
59
const ast = await parseAsync ( code , {
52
60
filename : fullPath ,
@@ -73,7 +81,7 @@ async function extractErrorCodesForPackage(pkg, errors, detection = 'opt-in') {
73
81
} ,
74
82
} ) ;
75
83
} ,
76
- { defaultConcurrency : workerCount , items : files } ,
84
+ 30 ,
77
85
) ;
78
86
}
79
87
@@ -86,6 +94,9 @@ export default async function extractErrorCodes(args) {
86
94
* @type {Set<string> }
87
95
*/
88
96
const errors = new Set ( ) ;
97
+
98
+ // Find relevant files
99
+
89
100
const basePackages = await getWorkspacePackages ( {
90
101
publicOnly : true ,
91
102
} ) ;
@@ -97,7 +108,19 @@ export default async function extractErrorCodes(args) {
97
108
! pkg . name . startsWith ( '@mui-internal/' ) &&
98
109
! skipPackages . includes ( pkg . name ) ,
99
110
) ;
100
- await Promise . all ( packages . map ( ( pkg ) => extractErrorCodesForPackage ( pkg , errors , detection ) ) ) ;
111
+ const files = await Promise . all ( packages . map ( ( pkg ) => getFilesForPackage ( pkg ) ) ) ;
112
+ packages . forEach ( ( pkg , index ) => {
113
+ console . log (
114
+ `🔍 ${ pkg . name } : Found ${ files [ index ] . length } file${ files [ index ] . length > 1 ? 's' : '' } ` ,
115
+ ) ;
116
+ } ) ;
117
+
118
+ // Extract error codes from said files.
119
+ const filesToProcess = files . flat ( ) ;
120
+ console . log ( `🔍 Extracting error codes from ${ filesToProcess . length } files...` ) ;
121
+ await extractErrorCodesForWorkspace ( filesToProcess , errors , detection ) ;
122
+
123
+ // Write error codes to the specified file.
101
124
const errorCodeFilePath = path . resolve ( errorCodesPath ) ;
102
125
const fileExists = await fs
103
126
. stat ( errorCodeFilePath )
@@ -133,13 +156,13 @@ export default async function extractErrorCodes(args) {
133
156
if ( ! fileExists ) {
134
157
await fs . mkdir ( path . dirname ( errorCodeFilePath ) , { recursive : true } ) ;
135
158
}
136
- await fs . writeFile ( errorCodeFilePath , `${ JSON . stringify ( finalErrorCodes , null , 2 ) } \n` ) ;
137
159
const newErrorCount = inverseLookupCode . size - originalErrorCount ;
138
160
if ( newErrorCount === 0 ) {
139
161
console . log ( `✅ No new error codes found.` ) ;
140
162
} else {
141
163
console . log (
142
164
`📝 Wrote ${ newErrorCount } new error code${ newErrorCount > 1 ? 's' : '' } to "${ errorCodesPath } "` ,
143
165
) ;
166
+ await fs . writeFile ( errorCodeFilePath , `${ JSON . stringify ( finalErrorCodes , null , 2 ) } \n` ) ;
144
167
}
145
168
}
0 commit comments