Skip to content

Commit 73bcbe4

Browse files
authored
refactor: update cli output (#16)
1 parent 1dee334 commit 73bcbe4

5 files changed

Lines changed: 97 additions & 46 deletions

File tree

src/bin/build.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,27 @@ import resolvePlugin from '@rollup/plugin-node-resolve'
1111
import aliasPlugin from '@rollup/plugin-alias'
1212
import { dts as dtsPlugin } from 'rollup-plugin-dts'
1313
import { esbuild as esbuildPlugin } from '@/utils/plugins/esbuild'
14-
import { getOutputPath, formatMs, formatBytes, error } from '@/utils'
14+
import {
15+
getOutputPath,
16+
getLongestOutput,
17+
formatMs,
18+
formatBytes,
19+
error,
20+
} from '@/utils'
1521
import type { ModuleFormat } from 'rollup'
1622
import type { RollupAliasOptions } from '@rollup/plugin-alias'
1723
import type { Options, BuildStats, BuildLogs } from '@/types'
1824

19-
function logModuleStats(file: {
20-
format: string
21-
path: string
22-
buildTime: number
23-
size: number
24-
logs: BuildLogs[]
25-
}) {
25+
function logModuleStats(
26+
file: {
27+
format: string
28+
path: string
29+
buildTime: number
30+
size: number
31+
logs: BuildLogs[]
32+
},
33+
longestOutput: number,
34+
) {
2635
const cl = console.log
2736
const base = parse(file.path).base
2837
const path = file.path.replace(base, '')
@@ -32,17 +41,32 @@ function logModuleStats(file: {
3241
if (format === 'commonjs') format = 'cjs'
3342
if (format === 'module') format = 'esm'
3443

44+
longestOutput = longestOutput + 2
45+
const ansiCode = 9
46+
const pathDim = dim(path)
47+
const output = pathDim + base
48+
const pathDimNoAnsi = pathDim.length - ansiCode
49+
const difference = longestOutput - pathDimNoAnsi - base.length
50+
const padLength = output.length + difference
51+
3552
cl(
36-
dim('├─'),
37-
`+ ${format}`,
38-
dim(path) + base,
39-
dim(`(${formatMs(file.buildTime)})`),
53+
dim('+'),
54+
format.padEnd(5),
55+
output.padEnd(padLength),
56+
dim('time'),
57+
formatMs(file.buildTime).padEnd(7),
58+
dim('size'),
4059
formatBytes(file.size),
4160
)
4261

4362
if (file.logs) {
4463
for (const log of file.logs) {
45-
cl(dim('├─'), `! ${log.level}`, dim(path) + base, dim(log.log.message))
64+
cl(
65+
dim('!'),
66+
log.level.padEnd(5),
67+
output.padEnd(padLength),
68+
dim(log.log.message),
69+
)
4670
}
4771
}
4872
}
@@ -64,6 +88,8 @@ export async function build(
6488
await hooks?.['build:start']?.(options, buildStats)
6589

6690
if (options.entries) {
91+
const longestOutput = getLongestOutput(outDir, options.entries)
92+
6793
start = Date.now()
6894

6995
const aliasDir = resolve(cwd, './src')
@@ -133,7 +159,7 @@ export async function build(
133159
buildStats.files.push(fileStats)
134160
buildStats.size = buildStats.size + stats.size
135161

136-
logModuleStats(fileStats)
162+
logModuleStats(fileStats, longestOutput)
137163
}
138164
}
139165

@@ -234,7 +260,7 @@ export async function build(
234260
buildStats.files.push(file)
235261
buildStats.size = buildStats.size + stats.size
236262

237-
logModuleStats(file)
263+
logModuleStats(file, longestOutput)
238264

239265
await hooks?.['build:entry:end']?.(_entry, buildStats)
240266
}
@@ -300,7 +326,7 @@ export async function build(
300326
buildStats.files.push(fileStats)
301327
buildStats.size = buildStats.size + stats.size
302328

303-
logModuleStats(fileStats)
329+
logModuleStats(fileStats, longestOutput)
304330

305331
await hooks?.['build:entry:end']?.(_entry, buildStats)
306332
}
@@ -328,7 +354,7 @@ export async function build(
328354
buildStats.files.push(fileStats)
329355
buildStats.size = buildStats.size + stats.size
330356

331-
logModuleStats(fileStats)
357+
logModuleStats(fileStats, longestOutput)
332358
}
333359
}
334360

src/bin/builder.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { green, dim, bold } from '@hypernym/colors'
2-
import { logoname, version } from './meta'
1+
import { dim } from '@hypernym/colors'
2+
import { version } from './meta'
33
import { build } from './build'
44
import { logger, formatMs, formatBytes, error } from '@/utils'
55
import type { ConfigLoader } from '@/types'
@@ -14,42 +14,33 @@ export async function createBuilder(
1414
const cl = console.log
1515

1616
await hooks?.['bundle:start']?.(options)
17-
const line = '─'.repeat(logoname.length + 2)
1817

19-
cl()
20-
cl(dim(`┌${line}┐`))
21-
cl(dim('│'), `${logoname.toUpperCase()}`, dim('│'), dim(`v${version}`))
22-
cl(dim(`└${line}┘`))
23-
cl(dim(bold('i')), 'Config', dim(configPath))
24-
cl(dim(bold('i')), 'Bundling started...')
18+
logger.info(dim(`v${version}`))
19+
cl('Config', dim(configPath))
20+
cl('Bundling started...')
2521
cl(
26-
dim(bold('*')),
2722
'Processing',
2823
dim(`[${new Date().toLocaleTimeString()}]`),
2924
'Transforming files',
3025
)
31-
cl(dim('│'))
26+
cl()
3227

3328
await build(cwd, options)
3429
.then((stats) => {
35-
const check = green(bold('✔'))
36-
const buildTime = green(formatMs(stats.buildTime))
37-
const buildSize = green(formatBytes(stats.size))
30+
const buildTime = dim(formatMs(stats.buildTime))
31+
const buildSize = dim(formatBytes(stats.size))
3832
const totalModules = stats.files.length
3933
const modules =
40-
totalModules > 1
41-
? `${green(totalModules)} modules`
42-
: `${green(totalModules)} module`
34+
totalModules > 1 ? `${totalModules} modules` : `${totalModules} module`
4335

44-
cl(dim('│'))
36+
cl()
4537
cl(
46-
dim(bold('*')),
4738
'Succeeded',
4839
dim(`[${new Date().toLocaleTimeString()}]`),
4940
'Module transformation is done',
5041
)
51-
cl(check, `Bundling fully completed in ${buildTime}`)
52-
cl(check, `${modules} transformed. Total size is ${buildSize}`)
42+
cl(`Bundling fully completed in ${buildTime}`)
43+
cl(`${modules} transformed. Total size is ${buildSize}`)
5344
logger.info(`Bundle is generated and ready for production`)
5445
cl()
5546
})

src/utils/get-longest-output.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getOutputPath } from './get-output-path'
2+
import type { Options } from '@/types'
3+
4+
export function getLongestOutput(
5+
outDir: string,
6+
entries: Options['entries'],
7+
): number {
8+
const outputs: string[] = []
9+
10+
for (const entry of entries) {
11+
if ('copy' in entry && entry.copy) {
12+
const out = entry.copy.output
13+
outputs.push(out)
14+
}
15+
16+
if ('input' in entry && entry.input) {
17+
const out = entry.output || getOutputPath(outDir, entry.input)
18+
outputs.push(out)
19+
}
20+
21+
if ('declaration' in entry && entry.declaration) {
22+
const out = entry.output || getOutputPath(outDir, entry.declaration, true)
23+
outputs.push(out)
24+
}
25+
26+
if ('template' in entry && entry.template) {
27+
outputs.push(entry.output)
28+
}
29+
}
30+
31+
return Math.max(...outputs.map((v) => v.length))
32+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './error'
33
export * from './format-ms'
44
export * from './format-bytes'
55
export * from './get-output-path'
6+
export * from './get-longest-output'

src/utils/logger.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import process from 'node:process'
22
import { dim } from '@hypernym/colors'
3-
import { logoname } from '@/bin/meta'
3+
import { logo } from '@/bin/meta'
44

5-
const name = logoname.toUpperCase()
65
const cl = console.log
6+
const separator = `/`
77

88
export const logger = {
99
info: (...args: any[]): void => {
10-
const time = new Date().toLocaleTimeString()
11-
return cl(name, dim(`[${time}]`), ...args)
10+
cl(logo, dim(separator), ...args)
1211
},
1312
error: (...args: any[]): void => {
14-
const time = new Date().toLocaleTimeString()
15-
return cl(name, dim(`[${time}]`), ...args)
13+
cl()
14+
cl(logo, dim(separator), ...args)
15+
cl()
1616
},
1717
exit: (message: string): never => {
18-
const time = new Date().toLocaleTimeString()
19-
cl(name, dim(`[${time}]`), message)
18+
cl()
19+
cl(logo, dim(separator), message)
20+
cl()
2021

2122
return process.exit()
2223
},

0 commit comments

Comments
 (0)