Skip to content

Commit f83180b

Browse files
committed
feat: implement parallel runner
BREAKING CHANGE: move runTransform/runTransformOnFile to: import { runTransform, runTransformOnFile } from 'astx/node'
1 parent 1f502c6 commit f83180b

19 files changed

+628
-103
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"@babel/plugin-transform-runtime": "^7.12.10",
110110
"@babel/preset-env": "^7.18.10",
111111
"@babel/preset-typescript": "^7.18.6",
112-
"@babel/register": "^7.12.10",
112+
"@babel/register": "^7.18.9",
113113
"@commitlint/cli": "^11.0.0",
114114
"@commitlint/config-conventional": "^11.0.0",
115115
"@jedwards1211/commitlint-config": "^1.0.2",
@@ -175,9 +175,11 @@
175175
"inquirer": "^7.3.3",
176176
"lodash": "^4.17.20",
177177
"minimatch": "^5.1.0",
178+
"p-event": "^4.0.0",
178179
"recast": "^0.21.2",
179180
"resolve": "^1.19.0",
180181
"shallowequal": "^1.1.0",
182+
"tiny-typed-emitter": "^2.1.0",
181183
"typed-validators": "^4.5.1",
182184
"yargs": "^16.2.0"
183185
}

pnpm-lock.yaml

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Astx.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1-
import { Node, NodePath } from './types'
1+
import { Expression, Statement, Node, NodePath } from './types'
22
import { Backend } from './backend/Backend'
33
import find, { Match, convertWithCaptures, createMatch } from './find'
44
import replace from './replace'
55
import compileMatcher, { MatchResult } from './compileMatcher'
66
import CodeFrameError from './util/CodeFrameError'
77
import ensureArray from './util/ensureArray'
8+
import * as AstTypes from 'ast-types'
9+
10+
export type TransformOptions = {
11+
/** The absolute path to the current file. */
12+
path: string
13+
/** The source code of the current file. */
14+
source: string
15+
astx: Astx
16+
expression(strings: TemplateStringsArray, ...quasis: any[]): Expression
17+
statement(strings: TemplateStringsArray, ...quasis: any[]): Statement
18+
statements(strings: TemplateStringsArray, ...quasis: any[]): Statement[]
19+
t: typeof AstTypes
20+
report: (msg: string) => void
21+
}
22+
23+
export type Transform = {
24+
astx?: (options: TransformOptions) => string | null | undefined | void
25+
find?: string | Node | Node[]
26+
replace?: string | Node | Node[] | GetReplacement
27+
where?: FindOptions['where']
28+
}
29+
30+
export type TransformResult = {
31+
file: string
32+
source?: string
33+
transformed?: string
34+
reports?: any[]
35+
error?: Error
36+
matches?: readonly Match[]
37+
backend: Backend
38+
}
839

940
export type ParsePattern = (
1041
strings: string | string[] | TemplateStringsArray,

src/AstxConfig.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Backend } from './backend/Backend'
2-
import { cosmiconfig } from 'cosmiconfig'
32
import * as t from 'typed-validators'
4-
import { CosmiconfigResult } from 'cosmiconfig/dist/types'
53

64
export type AstxConfig = {
75
parser?:
@@ -11,6 +9,7 @@ export type AstxConfig = {
119
| 'recast/babel/auto'
1210
| Backend
1311
parserOptions?: Record<string, any>
12+
workers?: number
1413
}
1514

1615
export const AstxConfigType: t.TypeAlias<AstxConfig> = t.alias(
@@ -26,16 +25,7 @@ export const AstxConfigType: t.TypeAlias<AstxConfig> = t.alias(
2625
),
2726

2827
parserOptions: t.record(t.string(), t.any()),
28+
workers: t.number(),
2929
},
3030
})
3131
)
32-
33-
export const astxCosmiconfig = cosmiconfig('astx', {
34-
transform: (result: CosmiconfigResult): CosmiconfigResult =>
35-
result
36-
? {
37-
...result,
38-
config: AstxConfigType.assert(result.config || {}),
39-
}
40-
: null,
41-
})

src/cli/transform.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import inquirer from 'inquirer'
88
import fs from 'fs-extra'
99
import dedent from 'dedent-js'
1010
import CodeFrameError from '../util/CodeFrameError'
11-
import runTransform from '../runTransform'
12-
import Astx from '../Astx'
13-
import formatMatches from '../util/formatMatches'
14-
import { Transform } from '../runTransformOnFile'
11+
import { formatIpcMatches } from '../util/formatMatches'
12+
import { AstxWorkerPool, astxCosmiconfig } from '../node'
13+
import { invertIpcError } from '../node/ipc'
14+
import { Transform } from '../Astx'
1515

1616
/* eslint-disable no-console */
1717

@@ -93,15 +93,9 @@ const transform: CommandModule<Options> = {
9393
let errorCount = 0
9494
let changedCount = 0
9595
let unchangedCount = 0
96-
for await (const {
97-
file,
98-
source,
99-
transformed,
100-
reports,
101-
error,
102-
matches,
103-
backend,
104-
} of runTransform({
96+
const config = (await astxCosmiconfig.search())?.config
97+
const pool = new AstxWorkerPool({ capacity: config?.workers })
98+
for await (const event of pool.runTransform({
10599
transform,
106100
transformFile,
107101
paths,
@@ -110,6 +104,18 @@ const transform: CommandModule<Options> = {
110104
parserOptions: parserOptions ? JSON.parse(parserOptions) : undefined,
111105
},
112106
})) {
107+
if (event.type === 'progress') {
108+
continue
109+
}
110+
const {
111+
file,
112+
source,
113+
transformed,
114+
reports,
115+
matches,
116+
error: _error,
117+
} = event.result
118+
const error = _error ? invertIpcError(_error) : undefined
113119
const relpath = path.relative(process.cwd(), file)
114120
const logHeader = once((logFn: (value: string) => any) =>
115121
logFn(
@@ -150,7 +156,7 @@ const transform: CommandModule<Options> = {
150156
!transform.astx
151157
) {
152158
logHeader(console.log)
153-
console.log(formatMatches(backend, source, matches))
159+
console.log(formatIpcMatches(source, matches))
154160
} else {
155161
unchangedCount++
156162
}
@@ -165,9 +171,10 @@ const transform: CommandModule<Options> = {
165171
)
166172
reports?.forEach((r: any) =>
167173
console.error(
168-
r instanceof Astx && source
169-
? formatMatches(backend, source, r.matches)
170-
: r
174+
// r instanceof Astx && source
175+
// ? formatIpcMatches(source, r.matches)
176+
// : r
177+
r
171178
)
172179
)
173180
}
@@ -220,6 +227,8 @@ const transform: CommandModule<Options> = {
220227
}
221228
if (process.send) process.send({ exit: 0 })
222229
}
230+
await pool.end()
231+
process.exit(0)
223232
},
224233
}
225234

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
export { default as Astx, ParsePattern, GetReplacement } from './Astx'
1+
export {
2+
default as Astx,
3+
Transform,
4+
TransformOptions,
5+
TransformResult,
6+
ParsePattern,
7+
GetReplacement,
8+
} from './Astx'
29
export { default as find, Match, FindOptions } from './find'
310
export { default as compileMatcher, CompiledMatcher } from './compileMatcher'
411
export { default as replace, ReplaceOptions } from './replace'
@@ -13,11 +20,6 @@ export { default as getBabelBackend } from './babel/getBabelBackend'
1320
export { default as BabelBackend } from './babel/BabelBackend'
1421
export { default as getRecastBackend } from './recast/getRecastBackend'
1522
export { default as RecastBackend } from './recast/RecastBackend'
16-
export { default as runTransform } from './runTransform'
17-
export {
18-
default as runTransformOnFile,
19-
Transform,
20-
TransformResult,
21-
} from './runTransformOnFile'
2223
export { default as CodeFrameError } from './util/CodeFrameError'
2324
export { default as CompilePathError } from './util/CompilePathError'
25+
export { AstxConfig } from './AstxConfig'

src/node/AstxWorker.babel.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* eslint-env commonjs */
2+
/* eslint-disable @typescript-eslint/no-var-requires */
3+
4+
require('@babel/register')({ extensions: ['.ts'] })
5+
require('./AstxWorker.ts').workerProcess()

0 commit comments

Comments
 (0)