Skip to content

Commit

Permalink
feat: add -v for info command
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Mar 29, 2024
1 parent fbb2fe5 commit 1300ccc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"chalk": "5",
"clipanion": "3.2.1",
"esm-utils": "^4.2.1",
"exifr": "^7.1.3",
"fast-glob": "^3.3.2",
"figures": "^6.1.0",
"fs-extra": "11.2.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/commands/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const getDurationDisplay = humanizer({ language: lang, fallbacks: ['en'], round:
const AllOWED_CODEC = ['mozjpeg', 'webp', 'avif', 'jxl', 'mozjpeg-raw'] as const
type Codec = typeof AllOWED_CODEC extends ReadonlyArray<infer T> ? T : never

enum FileModeOutputType {
AppendBase = 'append-base',
NewDir = 'new-dir',
}

const DEFAULT_CONCURRENCY = process.env.UV_THREADPOOL_SIZE
? Number(process.env.UV_THREADPOOL_SIZE)
: Math.round(cpus().length * 1.5) // 受限于 UV_THREADPOOL_SIZE, 再大到 libuv 那里都得排队
Expand Down Expand Up @@ -85,11 +90,11 @@ export class CompressCommand extends Command {
})

output = Option.String('-o,--output', {
description: 'output patterns, can optional use special `append-base` or `separate-dir`',
description: `output patterns; optional use special value \`${FileModeOutputType.AppendBase}\` or \`${FileModeOutputType.NewDir}\``,
})

codec = Option.String('-C,--codec', 'mozjpeg' satisfies Codec, {
description: `Allowed codec: ${AllOWED_CODEC.map((c) => `\`${c}\``).join(' or ')}`,
description: `Allowed: ${AllOWED_CODEC.map((c) => `\`${c}\``).join(' or ')}`,
})

metadata = Option.Boolean('--metadata', true, {
Expand Down Expand Up @@ -256,10 +261,9 @@ export class CompressCommand extends Command {
// special output
let { output } = this
const q = lossless ? 'lossless' : `q${quality}`
if (output === 'append-base') {
if (output === FileModeOutputType.AppendBase) {
output = `:dir/:file.${codec}-${q}.:ext`
}
if (output === 'separate-dir') {
} else if (output === FileModeOutputType.NewDir) {
output = `:dir-${codec}-${q}/:name.:ext`
}

Expand Down
25 changes: 21 additions & 4 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command, Option, Usage } from 'clipanion'
import exifr from 'exifr'
import path from 'path'
import { decode, metadata } from '../index.js'

Expand All @@ -11,18 +12,34 @@ export class InfoCommand extends Command {

file = Option.String({ required: true })

verbose = Option.Boolean('-v,--verbose', false, {
description: 'show verbose info',
})

execute(): Promise<number | void> {
return main(this)
}
}

export async function main(argv: { file: string }) {
let { file } = argv
export async function main({ file, verbose }: { file: string; verbose: boolean }) {
if (file) file = path.resolve(file)

const meta = await metadata(file)
console.log('metadata: %o', meta)
console.log('\nmetadata: %o', meta)

const decoded = await decode(file)
console.log('decoded info: %s', decoded)
console.log('\n\ndecoded info: %s', decoded)

const _orientation = await exifr.orientation(file)
console.log('\n\nexifr orientation: %o', _orientation)

if (verbose) {
const all = await exifr.parse(file, {
tiff: true,
ifd1: true,
mergeOutput: true,
translateValues: false,
})
console.log('\n\nexifr all: ', all)
}
}
4 changes: 2 additions & 2 deletions src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function sharpMozjpegCompress(
})

if (keepMetadata) {
img = img.withMetadata()
img = img.keepMetadata()
}

const buf = await img.toBuffer()
Expand All @@ -97,7 +97,7 @@ function sharpTargetFormatFactory<T extends 'webp' | 'avif' | 'jxl'>(
let img = (await getSharpInstance(file))[targetFormat](options)

if (keepMetadata) {
img = img.withMetadata()
img = img.keepMetadata()
}

const buf = await img.toBuffer()
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"esModuleInterop": true,
"declaration": true,
"strictNullChecks": true,
"noImplicitThis": true,
"skipLibCheck": true
},
"ts-node": {
Expand Down

0 comments on commit 1300ccc

Please sign in to comment.