Skip to content

Commit

Permalink
feat: log size change
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Feb 19, 2023
1 parent 84e1c62 commit 3495571
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/cli-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sourcePackageJsonFields } from 'unconfig/presets'
import * as p from '@clack/prompts'
import color from 'picocolors'
import type { Config } from './types'
import { isDir, isFile, isPathValid } from './utils'
import { formatFileSize, isDir, isFile, isPathValid } from './utils'
import { TinifyCompressor } from '.'

export async function getConfig(cwd = process.cwd()) {
Expand Down Expand Up @@ -48,31 +48,31 @@ export async function startCli(cwd = process.cwd()) {

const s = p.spinner()
if (await isDir(project.input)) {
s.start('Compressing ~ ~')
s.start('Compressing')
await tinifyIns.compressDir(project.input, {
handler: _ => _.replace(project.input, project.output),
debug: project.debug,
})
s.stop('Compressed ~ ~')
s.stop('Compressed!')
}
else if (await isFile(project.input)) {
s.start('Compressing ~ ~')
s.start('Compressing')
await tinifyIns.compressImage(project.input, {
handler: _ => _.replace(project.input, project.output),
debug: project.debug,
})
s.stop('Compressed ~ ~')
s.stop('Compressed!')
}
else {
p.cancel('Please enter a valid path.')
process.exit(1)
}

const tips = `
End of compression: ${color.cyan('100')} images.
Before: ${color.red('100 KB')}
After : ${color.green('50 KB')}
Diff : ${color.yellow('50 KB')}
End of compression: ${color.cyan(tinifyIns.Total)} images.
Before: ${color.red(formatFileSize(tinifyIns.TotalBeforeSize))}
After : ${color.green(formatFileSize(tinifyIns.TotalAfterSize))}
Diff : ${color.yellow(formatFileSize(tinifyIns.TotalBeforeSize - tinifyIns.TotalAfterSize))}
`.trim()

p.note(tips, 'Compression statistics:')
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { getConfig } from './cli-start'

export class TinifyCompressor {
private tinifyInstance: typeof tinify
Total = 0
TotalBeforeSize = 0
TotalAfterSize = 0

constructor(key: string) {
if (!key)
Expand Down Expand Up @@ -67,6 +70,9 @@ export class TinifyCompressor {
const { size: prevSize } = await fs.stat(filePath)
await source.toFile(newPath)
const { size: nextSize } = await fs.stat(newPath)
this.Total++
this.TotalBeforeSize += prevSize
this.TotalAfterSize += nextSize

debug && consola.info(this.debugLog({ prevSize, nextSize, fileName }))

Expand Down
Binary file modified tests/fixtures/assets/imgs/WechatIMG99.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/assets/imgs/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/fixtures/assets/imgs/test/couple.webp
Binary file not shown.

0 comments on commit 3495571

Please sign in to comment.