@@ -8,8 +8,12 @@ import fs from 'fs-extra'
8
8
import dedent from 'dedent-js'
9
9
import CodeFrameError from '../util/CodeFrameError'
10
10
import { formatIpcMatches } from '../util/formatMatches'
11
- import { AstxWorkerPool , astxCosmiconfig } from '../node'
12
- import { invertIpcError } from '../node/ipc'
11
+ import { AstxWorkerPool , astxCosmiconfig , runTransform } from '../node'
12
+ import {
13
+ invertIpcError ,
14
+ IpcTransformResult ,
15
+ makeIpcTransformResult ,
16
+ } from '../node/ipc'
13
17
import { Transform } from '../Astx'
14
18
import ansiEscapes from 'ansi-escapes'
15
19
import { Progress } from '../node/AstxWorkerPool'
@@ -28,6 +32,7 @@ type Options = {
28
32
filesAndDirectories ?: string [ ]
29
33
yes ?: boolean
30
34
gitignore ?: boolean
35
+ threads ?: number
31
36
}
32
37
33
38
const transform : CommandModule < Options > = {
@@ -71,6 +76,10 @@ const transform: CommandModule<Options> = {
71
76
type : 'boolean' ,
72
77
describe : `ignore gitignored files` ,
73
78
default : true ,
79
+ } )
80
+ . option ( 'workers' , {
81
+ type : 'number' ,
82
+ describe : 'number of worker threads to use' ,
74
83
} ) ,
75
84
76
85
handler : async ( argv : Arguments < Options > ) => {
@@ -148,12 +157,14 @@ const transform: CommandModule<Options> = {
148
157
149
158
const interactive = isInteractive ( )
150
159
const config = ( await astxCosmiconfig . search ( ) ) ?. config
151
- const pool = new AstxWorkerPool ( { capacity : config ?. workers } )
160
+ const workers = argv . workers ?? config ?. workers
161
+ const pool =
162
+ workers === 1 ? null : new AstxWorkerPool ( { capacity : workers } )
152
163
try {
153
164
if ( interactive ) {
154
165
spinnerInterval = setInterval ( showProgress , 30 )
155
166
}
156
- for await ( const event of pool . runTransform ( {
167
+ const runTransformOptions = {
157
168
gitignore : gitignore ? undefined : null ,
158
169
transform,
159
170
transformFile,
@@ -162,7 +173,14 @@ const transform: CommandModule<Options> = {
162
173
parser : parser as any ,
163
174
parserOptions : parserOptions ? JSON . parse ( parserOptions ) : undefined ,
164
175
} ,
165
- } ) ) {
176
+ }
177
+ for await ( const _event of pool
178
+ ? pool . runTransform ( runTransformOptions )
179
+ : runTransform ( runTransformOptions ) ) {
180
+ const event : { type : 'result' ; result : IpcTransformResult } | Progress =
181
+ pool
182
+ ? ( _event as any )
183
+ : { type : 'result' , result : makeIpcTransformResult ( _event as any ) }
166
184
if ( event . type === 'progress' ) {
167
185
progress = event
168
186
if ( interactive ) showProgress ( )
@@ -291,7 +309,7 @@ const transform: CommandModule<Options> = {
291
309
}
292
310
if ( process . send ) process . send ( { exit : 0 } )
293
311
}
294
- await pool . end ( )
312
+ await pool ? .end ( )
295
313
process . exit ( 0 )
296
314
} ,
297
315
}
0 commit comments