Skip to content

Commit 4678ae5

Browse files
committed
feat(runTransform): yield similar events to AstxWorkerPool.runTransform
BREAKING CHANGE: the yield type of `runTransform` is now `{ type: 'result', result: TransformResult } | Progress` instead of `TransformResult`.
1 parent 2f60f9b commit 4678ae5

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

src/cli/transform.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ import fs from 'fs-extra'
88
import dedent from 'dedent-js'
99
import CodeFrameError from '../util/CodeFrameError'
1010
import { formatIpcMatches } from '../util/formatMatches'
11-
import { AstxWorkerPool, astxCosmiconfig, runTransform } from '../node'
11+
import {
12+
AstxWorkerPool,
13+
astxCosmiconfig,
14+
runTransform,
15+
Progress,
16+
} from '../node'
1217
import {
1318
invertIpcError,
1419
IpcTransformResult,
1520
makeIpcTransformResult,
1621
} from '../node/ipc'
1722
import { Transform } from '../Astx'
1823
import ansiEscapes from 'ansi-escapes'
19-
import { Progress } from '../node/AstxWorkerPool'
2024
import { spinner } from './spinner'
2125
import '../node/registerTsNode'
2226
import isInteractive from '../util/isInteractive'
@@ -183,9 +187,9 @@ const transform: CommandModule<Options> = {
183187
? pool.runTransform(runTransformOptions)
184188
: runTransform(runTransformOptions)) {
185189
const event: { type: 'result'; result: IpcTransformResult } | Progress =
186-
pool
187-
? (_event as any)
188-
: { type: 'result', result: makeIpcTransformResult(_event as any) }
190+
!pool && _event.type === 'result'
191+
? { type: 'result', result: makeIpcTransformResult(_event as any) }
192+
: (_event as any)
189193
if (event.type === 'progress') {
190194
progress = event
191195
if (interactive) showProgress()

src/node/AstxWorkerPool.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@ import astxGlob from './astxGlob'
66
import AstxWorker from './AstxWorker'
77
import AsyncPool from './AsyncPool'
88
import { astxCosmiconfig } from './astxCosmiconfig'
9-
import { RunTransformOptions } from './runTransform'
9+
import { RunTransformOptions, Progress } from './runTransform'
1010
import { RunTransformOnFileOptions } from './runTransformOnFile'
1111
import PushPullIterable from '../util/PushPullIterable'
1212

1313
class AbortedError extends Error {}
1414

15-
export type Progress = {
16-
type: 'progress'
17-
completed: number
18-
total: number
19-
globDone: boolean
20-
}
2115
export default class AstxWorkerPool {
2216
pool: AsyncPool<AstxWorker>
2317

src/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { default as AstxWorkerPool } from './AstxWorkerPool'
22
export {
33
default as runTransform,
44
type RunTransformOptions,
5+
type Progress,
56
} from './runTransform'
67
export {
78
default as runTransformOnFile,

src/node/runTransform.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { astxCosmiconfig } from './astxCosmiconfig'
66
import { Transform, TransformResult } from '../Astx'
77
import Gitignore from 'gitignore-fs'
88

9+
export type Progress = {
10+
type: 'progress'
11+
completed: number
12+
total: number
13+
globDone: boolean
14+
}
15+
916
export type RunTransformOptions = {
1017
gitignore?: Gitignore | null
1118
transform?: Transform
@@ -26,17 +33,36 @@ export default async function* runTransform({
2633
cwd = process.cwd(),
2734
config,
2835
signal,
29-
}: RunTransformOptions): AsyncIterable<TransformResult> {
36+
}: RunTransformOptions): AsyncIterable<
37+
{ type: 'result'; result: TransformResult } | Progress
38+
> {
3039
clearCache()
3140
astxCosmiconfig.clearSearchCache()
3241

3342
const transform = transformFile ? await import(transformFile) : _transform
3443
if (signal?.aborted) return
3544
if (!transform) throw new Error(`transform or transformFile is required`)
3645

46+
let completed = 0,
47+
total = 0,
48+
globDone = false
49+
50+
const progress = (): Progress => ({
51+
type: 'progress',
52+
completed,
53+
total,
54+
globDone,
55+
})
56+
57+
yield progress()
58+
if (signal?.aborted) return
59+
3760
const paths = _paths?.length ? _paths : [cwd]
3861
for (const include of paths) {
3962
for await (const file of astxGlob({ include, exclude, cwd, gitignore })) {
63+
if (signal?.aborted) return
64+
total++
65+
yield progress()
4066
if (signal?.aborted) return
4167
let transformed
4268
try {
@@ -46,14 +72,21 @@ export default async function* runTransform({
4672
config,
4773
signal,
4874
})
75+
completed++
76+
yield progress()
4977
} catch (error: any) {
5078
if (error.message === 'aborted' && signal?.aborted) return
5179
throw error
5280
}
5381
if (signal?.aborted) return
54-
yield transformed
82+
yield { type: 'result', result: transformed }
5583
}
5684
}
5785
if (signal?.aborted) return
86+
87+
globDone = true
88+
yield progress()
89+
if (signal?.aborted) return
90+
5891
await transform.finish?.()
5992
}

0 commit comments

Comments
 (0)