Skip to content

Commit 5add00b

Browse files
committed
feat(cli): add --workers option
1 parent 5bdaf00 commit 5add00b

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,4 +820,5 @@ Options:
820820
-y, --yes don't ask for confirmation before writing changes
821821
[boolean]
822822
--gitignore ignore gitignored files [boolean] [default: true]
823+
--workers number of worker threads to use [number]
823824
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0-development",
44
"description": "super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring",
55
"bin": {
6-
"astx": "es/cli/index.mjs"
6+
"astx": "cli/index.js"
77
},
88
"astx": {
99
"parser": "babel"
@@ -21,7 +21,7 @@
2121
"clean": "rimraf es lib $(cd src; ls) *.js *.d.ts *.js.flow",
2222
"build": "npm run clean && npm run build:types && npm run build:mjs && npm run check:mjs && npm run build:cjs",
2323
"build:types": "tsc -p tsconfig.build.json && tsc -p tsconfig.build.json --outDir es && copy 'src/**/*.js.flow' . && copy 'src/**/*.js.flow' es",
24-
"build:mjs": "cross-env OUTPUT_ESM=1 babel src --out-dir es --out-file-extension .mjs --extensions .ts --source-maps inline && babel-node --extensions .ts src/cli/index.ts -t convertImportsToMjs.ts --parser babel --no-gitignore -y 'es/**/*.mjs'",
24+
"build:mjs": "cross-env OUTPUT_ESM=1 babel src --out-dir es --out-file-extension .mjs --extensions .ts --source-maps inline && babel-node --extensions .ts src/cli/index.ts -t convertImportsToMjs.ts --workers 1 --parser babel --no-gitignore -y 'es/**/*.mjs'",
2525
"check:mjs": "node es/index.mjs && node es/node/index.mjs && node es/cli/index.mjs --help",
2626
"build:cjs": "cross-env BABEL_ENV=es5 babel src --out-dir . --extensions \".ts\"",
2727
"test": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc --reporter=lcov --reporter=text mocha $npm_package_config_mocha",

src/cli/transform.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ 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 } 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'
1317
import { Transform } from '../Astx'
1418
import ansiEscapes from 'ansi-escapes'
1519
import { Progress } from '../node/AstxWorkerPool'
@@ -28,6 +32,7 @@ type Options = {
2832
filesAndDirectories?: string[]
2933
yes?: boolean
3034
gitignore?: boolean
35+
threads?: number
3136
}
3237

3338
const transform: CommandModule<Options> = {
@@ -71,6 +76,10 @@ const transform: CommandModule<Options> = {
7176
type: 'boolean',
7277
describe: `ignore gitignored files`,
7378
default: true,
79+
})
80+
.option('workers', {
81+
type: 'number',
82+
describe: 'number of worker threads to use',
7483
}),
7584

7685
handler: async (argv: Arguments<Options>) => {
@@ -148,12 +157,14 @@ const transform: CommandModule<Options> = {
148157

149158
const interactive = isInteractive()
150159
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 })
152163
try {
153164
if (interactive) {
154165
spinnerInterval = setInterval(showProgress, 30)
155166
}
156-
for await (const event of pool.runTransform({
167+
const runTransformOptions = {
157168
gitignore: gitignore ? undefined : null,
158169
transform,
159170
transformFile,
@@ -162,7 +173,14 @@ const transform: CommandModule<Options> = {
162173
parser: parser as any,
163174
parserOptions: parserOptions ? JSON.parse(parserOptions) : undefined,
164175
},
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) }
166184
if (event.type === 'progress') {
167185
progress = event
168186
if (interactive) showProgress()
@@ -291,7 +309,7 @@ const transform: CommandModule<Options> = {
291309
}
292310
if (process.send) process.send({ exit: 0 })
293311
}
294-
await pool.end()
312+
await pool?.end()
295313
process.exit(0)
296314
},
297315
}

0 commit comments

Comments
 (0)