Skip to content

Commit

Permalink
fix(cli): move out the timing measure
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Dec 12, 2023
1 parent f869af7 commit cb40843
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
37 changes: 23 additions & 14 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { hideBin } from 'yargs/helpers'
import { version } from '../package.json'
import { Concurrency } from './concurrency'
import { findCommonBaseDir, getRelativePath, isPathInside, resolveGlob } from './path'
import { Timing } from './perf'
import { unminify } from './unminify'
import { unpacker } from './unpacker'
import type { ModuleMapping, ModuleMeta } from '@wakaru/ast-utils/types'
Expand Down Expand Up @@ -273,12 +274,14 @@ async function interactive({

const s = spinner()
s.start('...')
const items = await unpacker(inputPaths, outputPath)

const timing = new Timing()
const { result: items, time: elapsed } = await timing.measureTimeAsync(() => unpacker(inputPaths, outputPath))

s.stop('Finished')

const totalModules = items.reduce((acc, item) => acc + item.modules.length, 0)
const totalElapsed = items.reduce((acc, item) => acc + item.elapsed, 0)
const formattedElapsed = totalElapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
const formattedElapsed = elapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
log.success(`Successfully generated ${c.green(totalModules)} modules ${c.dim(`(${formattedElapsed}ms)`)}`)

outro(`Output directory: ${c.green(getRelativePath(cwd, outputPath))}`)
Expand Down Expand Up @@ -392,18 +395,20 @@ async function interactive({

const s = spinner()
s.start('...')

const timing = new Timing()
const concurrencyManager = new Concurrency({ concurrency })
const items = await Promise.all(
const { time: elapsed } = await timing.measureTimeAsync(() => Promise.all(
unminifyInputPaths.map(p => concurrencyManager.add(async () => {
const result = await unminify(p, moduleMapping, moduleMeta, commonBaseDir, outputPath)
s.message(`${c.green(path.relative(cwd, p))}`)
return result
})),
)
))

s.stop('Finished')

const totalElapsed = items.reduce((acc, item) => acc + item.elapsed, 0)
const formattedElapsed = totalElapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
const formattedElapsed = elapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })

log.success(`Successfully unminified ${c.green(unminifyInputPaths.length)} files ${c.dim(`(${formattedElapsed}ms)`)}`)

Expand Down Expand Up @@ -508,12 +513,14 @@ async function nonInteractive(features: Feature[], {

const s = spinner()
s.start('...')
const items = await unpacker(inputPaths, outputPath)

const timing = new Timing()
const { result: items, time: elapsed } = await timing.measureTimeAsync(() => unpacker(inputPaths, outputPath))

s.stop('Finished')

const totalModules = items.reduce((acc, item) => acc + item.modules.length, 0)
const totalElapsed = items.reduce((acc, item) => acc + item.elapsed, 0)
const formattedElapsed = totalElapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
const formattedElapsed = elapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
log.success(`Successfully generated ${c.green(totalModules)} modules ${c.dim(`(${formattedElapsed}ms)`)}`)
outro(`Output directory: ${c.green(relativeOutputPath)}`)

Expand Down Expand Up @@ -559,18 +566,20 @@ async function nonInteractive(features: Feature[], {

const s = spinner()
s.start('...')

const timing = new Timing()
const concurrencyManager = new Concurrency({ concurrency })
const items = await Promise.all(
const { time: elapsed } = await timing.measureTimeAsync(() => Promise.all(
unminifyInputPaths.map(p => concurrencyManager.add(async () => {
const result = await unminify(p, moduleMapping, moduleMeta, commonBaseDir, outputPath)
s.message(`${c.green(path.relative(cwd, p))}`)
return result
})),
)
))

s.stop('Finished')

const totalElapsed = items.reduce((acc, item) => acc + item.elapsed, 0)
const formattedElapsed = totalElapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })
const formattedElapsed = elapsed.toLocaleString('en-US', { maximumFractionDigits: 1 })

log.success(`Successfully unminified ${c.green(unminifyInputPaths.length)} files ${c.dim(`(${formattedElapsed}ms)`)}`)

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/unminify.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ try {
fsa.writeFileSync(outputPath, code, 'utf-8')

parentPort?.postMessage(code)

process.exit(0)
}
catch (e) {
// We print the error here because it will lose the stack trace after being sent to the main thread
Expand Down

0 comments on commit cb40843

Please sign in to comment.