Skip to content

Commit

Permalink
pref: update test & types
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Mar 15, 2023
1 parent 622f4a8 commit c03db5b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 73 deletions.
22 changes: 7 additions & 15 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import tinify from 'tinify'
import fs from 'fs-extra'
import consola from 'consola'
import { IMG_EXT } from './constant'
import type { CompressOption } from './types'
import { formatFileName, formatFileSize, isPathValid } from './utils'
import type { CPmodel, CompressOption } from './types'
import { debugLog, isPathValid } from './utils'
import { getConfig } from './config'

class TinifyCompressor {
Expand All @@ -31,19 +31,18 @@ class TinifyCompressor {
* @param {CompressOption} option compress option
*/
async compressImages(imgFiles: string[], option: CompressOption = { debug: true }) {
const logs = (await Promise.all(imgFiles.map(imgFile => this.compressImage(imgFile, { ...option, debug: false }))))
.map(this.debugLog)
const result = (await Promise.all(imgFiles.map(imgFile => this.compressImage(imgFile, { ...option, debug: false }))))

if (option.debug)
logs.forEach(log => consola.info(log))
result.map(debugLog).forEach(consola.info)
}

/**
* Compress image
* @param {string} filePath need to compress image file path
* @param {CompressOption} option compress option
*/
async compressImage(filePath: string, { handler, convertType, transform, debug = true }: CompressOption = {}) {
async compressImage(filePath: string, { handler, convertType, transform, debug = true }: CompressOption = {}): Promise<CPmodel> {
let source = this.tinifyInstance.fromFile(filePath)
const fileName = path.basename(filePath)
const newPath = handler ? handler(filePath, fileName) : filePath
Expand All @@ -70,23 +69,16 @@ 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 }))
debug && consola.info(debugLog({ prevSize, nextSize, fileName }))

return { prevSize, nextSize, fileName }
}

private debugLog({ prevSize, nextSize, fileName }: { prevSize: number; nextSize: number; fileName: string }) {
return `
Compress suceess of ${formatFileName(fileName, 16)},
Size: ${formatFileSize(prevSize).padEnd(9)} -> ${formatFileSize(nextSize).padEnd(9)},
Diff: ${formatFileSize(prevSize - nextSize).padEnd(9)}
`.trim().replace(/\n\s+/g, ' ')
}

/**
* Get image files from directory
*
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './core'
export * from './utils'
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ export interface CliOption {
debug?: boolean
cwd: string
}

export interface CPmodel {
prevSize: number
nextSize: number
fileName: string
}
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import os from 'node:os'
import fs from 'fs-extra'
import type { CPmodel } from './types'

export async function isPathValid(filePath: string): Promise<boolean> {
return await fs.pathExists(filePath)
Expand Down Expand Up @@ -57,3 +58,11 @@ export function formatFileName(name: string, length = 12, ellipsis = '...') {
export function toArray<T>(val: T | T[]): T[] {
return Array.isArray(val) ? val : [val]
}

export function debugLog({ prevSize, nextSize, fileName }: CPmodel) {
return `
Compress suceess of ${formatFileName(fileName, 16)},
Size: ${formatFileSize(prevSize).padEnd(9)} -> ${formatFileSize(nextSize).padEnd(9)},
Diff: ${formatFileSize(prevSize - nextSize).padEnd(9)}
`.trim().replace(/\n\s+/g, ' ')
}
58 changes: 0 additions & 58 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,7 @@
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import { formatFileName, formatFileSize } from '../src/utils'
import { getConfig, resolveConfig } from '../src/config'

describe('Assets Imgs', () => {
test('formatFileSize', () => {
expect(formatFileSize(1024)).toBe('1.00 KB')
expect(formatFileSize(1024 * 1024)).toBe('1.00 MB')
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1.00 GB')
})

test('formatFileName', () => {
expect(path.dirname('/Users/chris/projects/fork/tiny-sdk/test/assets/imgs/WechatIMG99.jpeg')).toMatchInlineSnapshot('"/Users/chris/projects/fork/tiny-sdk/test/assets/imgs"')

const name = 'abcdasdasdasdasdasdsadasefghijkdasd.png'
expect(name.length).toMatchInlineSnapshot('39')
expect(formatFileName(name, 9, '***')).toMatchInlineSnapshot('"a***d.png"')
expect(formatFileName(name).length).toMatchInlineSnapshot('12')
expect(formatFileName(name, 5)).toMatchInlineSnapshot('"abcda"')
expect(formatFileName(name, 11)).toMatchInlineSnapshot('"ab...sd.png"')
expect(formatFileName(name, 30)).toMatchInlineSnapshot('"abcdasdasda...sefghijkdasd.png"')
expect(formatFileName(name, 30).length).toMatchInlineSnapshot('30')
})

test('padEndFormat', () => {
const result = [
{
prevSize: 1022244,
nextSize: 12321,
name: 'WechatIMG99.jpeg',
},
{
prevSize: 13123024,
nextSize: 4122331,
name: 'avatar.png',
},
{
prevSize: 5024,
nextSize: 1024,
name: 'couple.webp',
},
]

const logs = result.map(({ prevSize, nextSize, name }) => {
return `
Compress suceess of ${formatFileName(name, 16)},
Size: ${formatFileSize(prevSize).padEnd(9)} -> ${formatFileSize(nextSize).padEnd(9)},
Diff: ${formatFileSize(prevSize - nextSize).padEnd(9)}
`.trim().replace(/\n\s+/g, ' ')
})

expect(logs).toMatchInlineSnapshot(`
[
"Compress suceess of WechatIMG99.jpeg, Size: 998.29 KB -> 12.03 KB , Diff: 986.25 KB",
"Compress suceess of avatar.png , Size: 12.52 MB -> 3.93 MB , Diff: 8.58 MB",
"Compress suceess of couple.webp , Size: 4.91 KB -> 1.00 KB , Diff: 3.91 KB",
]
`)
})
})

describe('Unconfigured', () => {
test('unconfigured', async () => {
const cwd = path.resolve(__dirname, './fixtures/configs')
Expand Down
53 changes: 53 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import { debugLog, formatFileName, formatFileSize } from '../src/utils'

describe('Utils', () => {
test('formatFileSize', () => {
expect(formatFileSize(1024)).toBe('1.00 KB')
expect(formatFileSize(1024 * 1024)).toBe('1.00 MB')
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1.00 GB')
})

test('formatFileName', () => {
expect(path.dirname('/Users/chris/projects/fork/tiny-sdk/test/assets/imgs/WechatIMG99.jpeg'))
.toBe('/Users/chris/projects/fork/tiny-sdk/test/assets/imgs')

const name = 'abcdasdasdasdasdasdsadasefghijkdasd.png'
expect(name.length).toMatchInlineSnapshot('39')
expect(formatFileName(name, 9, '***')).toMatchInlineSnapshot('"a***d.png"')
expect(formatFileName(name).length).toMatchInlineSnapshot('12')
expect(formatFileName(name, 5)).toMatchInlineSnapshot('"abcda"')
expect(formatFileName(name, 11)).toMatchInlineSnapshot('"ab...sd.png"')
expect(formatFileName(name, 30)).toMatchInlineSnapshot('"abcdasdasda...sefghijkdasd.png"')
expect(formatFileName(name, 30).length).toMatchInlineSnapshot('30')
})

test('debugLog', () => {
const result = [
{
prevSize: 1022244,
nextSize: 12321,
fileName: 'WechatIMG99.jpeg',
},
{
prevSize: 13123024,
nextSize: 4122331,
fileName: 'avatar.png',
},
{
prevSize: 5024,
nextSize: 1024,
fileName: 'couple.webp',
},
]

expect(result.map(debugLog)).toMatchInlineSnapshot(`
[
"Compress suceess of WechatIMG99.jpeg, Size: 998.29 KB -> 12.03 KB , Diff: 986.25 KB",
"Compress suceess of avatar.png , Size: 12.52 MB -> 3.93 MB , Diff: 8.58 MB",
"Compress suceess of couple.webp , Size: 4.91 KB -> 1.00 KB , Diff: 3.91 KB",
]
`)
})
})

0 comments on commit c03db5b

Please sign in to comment.