Skip to content

Commit

Permalink
feat: add concurrency limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaw52 committed Mar 3, 2023
1 parent e6f8b00 commit 11b1c3d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"fast-glob": "^3.2.12",
"kolorist": "^1.7.0",
"ora": "^6.1.2",
"p-limit": "^4.0.0",
"prompts": "^2.4.2",
"slash": "^5.0.0"
},
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/transformStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import consola from 'consola'
import slash from 'slash'
import glob from 'fast-glob'
import ora from 'ora'
import pLimit from 'p-limit'
import { loadArgs } from './utils/loadArgs'
import { gitMv } from './utils/gitMv'
import { formatMs } from './utils/formatTime'
Expand Down Expand Up @@ -41,7 +42,8 @@ const runRename = async (oldPath: string, isGitMv: 1 | 0) => {
* @returns 迁移的文件List
*/
export const transformStart = async (scanPath: string, isGitMv: 1 | 0): Promise<string[]> => {
const ignore = loadArgs()
const { ignore, concurrency } = loadArgs()

const tsFiles = glob.sync(`${slash(scanPath)}/**/*.{ts,js}`, {
ignore: ignore.length > 0
? ['**/node_modules/**', '**/dist/**', ...ignore]
Expand Down Expand Up @@ -80,8 +82,10 @@ export const transformStart = async (scanPath: string, isGitMv: 1 | 0): Promise<

try {
spinner.start('Start scanning\n')
const limit = pLimit(concurrency)

await Promise.all(
needTransformList.map(path => runRename(path, isGitMv)),
needTransformList.map(path => limit(async () => runRename(path, isGitMv))),
)

const runTime = formatMs(Date.now() - startTime)
Expand Down
23 changes: 14 additions & 9 deletions src/utils/loadArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import cac from 'cac'
import consola from 'consola'
import { version } from '../../package.json'

export const loadArgs = (): string[] => {
const parseIgnore = (ignore?: (string | boolean)[] | string | boolean) => {
if (!ignore || typeof ignore === 'boolean')
return []

if (Array.isArray(ignore))
return ignore.filter(Boolean).map(el => (el as string)?.trim())

return ignore.trim().split(',')
}

export const loadArgs = (): { ignore: string[]; concurrency: number } => {
try {
const cli = cac('@jaw52/transform-jsx-for-vite')
cli
.version(version)
.option('--ignore <ignore>', 'ignore path')
.option('--concurrency <concurrency>', 'concurrency limit.')
.help()

const { options = { ignore: [] } } = cli.parse() as { options: { ignore?: (string | boolean)[] | string | boolean } }

if (!options.ignore || typeof options.ignore === 'boolean')
return []

if (Array.isArray(options?.ignore))
return options?.ignore.filter(Boolean).map(el => (el as string)?.trim())
const { options } = cli.parse() as { options: { ignore?: (string | boolean)[] | string | boolean; concurrency?: number } }

return options?.ignore.trim().split(',')
return { ignore: parseIgnore(options.ignore), concurrency: options.concurrency ?? 10 }
}
catch (error) {
consola.error(error)
Expand Down

0 comments on commit 11b1c3d

Please sign in to comment.