Skip to content

Commit

Permalink
feat(cli): generate perf.json when perf is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Dec 14, 2023
1 parent 8526400 commit c5e59d6
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,9 @@ async function interactive({
outro(`Output directory: ${c.green(getRelativePath(cwd, outputDir))}`)

if (perf) {
handlePerfMeasurements(measurements)
printPerfStats(measurements)

writePerfStats(measurements, path.join(outputBase, 'perf.json'))
}
}

Expand Down Expand Up @@ -456,19 +458,19 @@ async function nonInteractive(features: Feature[], {
return process.exit(1)
}

const output = _output ?? defaultOutputBase
const outputBase = _output ?? defaultOutputBase
const singleFeature = features.length === 1
const unpackerOutput = _unpackerOutput ?? (singleFeature ? output : path.join(output, defaultUnpackerOutputFolder))
const unminifyOutput = _unminifyOutput ?? (singleFeature ? output : path.join(output, defaultUnminifyOutputFolder))
const unpackerOutput = _unpackerOutput ?? (singleFeature ? outputBase : path.join(outputBase, defaultUnpackerOutputFolder))
const unminifyOutput = _unminifyOutput ?? (singleFeature ? outputBase : path.join(outputBase, defaultUnminifyOutputFolder))

if (!isPathInside(cwd, output)) {
if (!isPathInside(cwd, outputBase)) {
log.error('Output directory must be inside the current working directory')
return process.exit(1)
}

if (!force) {
if (fsa.existsSync(output)) {
log.error(`Output directory already exists at ${c.green(output)}. Pass ${c.green('--force')} to overwrite`)
if (fsa.existsSync(outputBase)) {
log.error(`Output directory already exists at ${c.green(outputBase)}. Pass ${c.green('--force')} to overwrite`)
return process.exit(1)
}

Expand Down Expand Up @@ -566,7 +568,9 @@ async function nonInteractive(features: Feature[], {
outro(`Output directory: ${c.green(relativeOutputPath)}`)

if (perf) {
handlePerfMeasurements(measurements)
printPerfStats(measurements)

writePerfStats(measurements, path.join(outputBase, 'perf.json'))
}
}
}
Expand All @@ -578,7 +582,7 @@ function formatElapsed(elapsed: number) {
return `${~~(elapsed / 1000 / 60 / 60)}h${~~((elapsed / 1000 / 60) % 60)}m${~~((elapsed / 1000) % 60)}s`
}

function handlePerfMeasurements(measurements: Measurement[]) {
function printPerfStats(measurements: Measurement[]) {
const groupedByRules = measurements
.flat()
.reduce<Record<string, number>>((acc, { key, time }) => {
Expand All @@ -592,6 +596,17 @@ function handlePerfMeasurements(measurements: Measurement[]) {
console.table(table, ['key', 'time'])
}

function writePerfStats(measurements: Measurement[], outputPath: string) {
fsa.writeJSONSync(outputPath, measurements.flat(), {
encoding: 'utf-8',
spaces: 2,
})

console.log()
console.log(`Performance statistics generated at ${c.green(getRelativePath(process.cwd(), outputPath))}`)
console.log()
}

function generateModuleMeta(modules: Module[]) {
return modules.reduce<ModuleMeta>((acc, mod) => {
acc[mod.id] = {
Expand Down

0 comments on commit c5e59d6

Please sign in to comment.