Skip to content

Commit 539c1e5

Browse files
committed
refactor: update utils
1 parent 13dad94 commit 539c1e5

5 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/utils/error.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import process from 'node:process'
22
import { cyan, red } from '@hypernym/colors'
33
import { name } from '../bin/meta.js'
4+
import { log } from '../utils/logger.js'
45

56
export function error(err: any): never {
6-
console.log(red(name), 'Something went wrong...')
7+
log(red(name), 'Something went wrong...')
78
console.error(err)
89
return process.exit(1)
910
}

src/utils/format-bytes.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function formatBytes(bytes: number) {
2+
const decimals = 2
3+
const units = ['B', 'KB', 'MB', 'GB', 'TB']
4+
5+
if (bytes === 0) return `0 B`
6+
7+
const k = 1024
8+
const dm = decimals < 0 ? 0 : decimals
9+
const i = Math.floor(Math.log(bytes) / Math.log(k))
10+
11+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`
12+
}

src/utils/format-ms.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function formatMs(ms: number): string {
2+
const s = 1000
3+
const m = s * 60
4+
const h = m * 60
5+
const msAbs = Math.abs(ms)
6+
7+
if (msAbs >= h) return `${(ms / h).toFixed(2)}h`
8+
if (msAbs >= m) return `${(ms / m).toFixed(2)}m`
9+
if (msAbs >= s) return `${(ms / s).toFixed(2)}s`
10+
11+
return `${ms}ms`
12+
}

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './logger.js'
22
export * from './error.js'
3+
export * from './format-ms.js'
4+
export * from './format-bytes.js'

src/utils/logger.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import process, { stdout } from 'node:process'
2-
import { cyan, magenta, dim } from '@hypernym/colors'
2+
import { magenta, dim } from '@hypernym/colors'
33
import { name, version } from '../bin/meta.js'
44

5-
const cl = console.log
5+
export const cl = console.log
66

77
export const log = (...args: string[]) => {
88
let length = args.length + 2
@@ -21,12 +21,6 @@ export const log = (...args: string[]) => {
2121
}
2222

2323
export const logger = {
24-
start: (): void => {
25-
cl()
26-
log(dim(name), dim(version))
27-
log(cyan(name), `Bundling started...`)
28-
cl()
29-
},
3024
exit: (message: string): never => {
3125
cl()
3226
log(dim(name), dim(version))

0 commit comments

Comments
 (0)