Skip to content

Commit b84e304

Browse files
authored
New: Add jpg command (#87)
1 parent 1141cde commit b84e304

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

.changeset/green-queens-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tidaltheory/lens': minor
3+
---
4+
5+
`jpg` command to convert an image to a high-quality JPG file

src/cli.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,30 @@ prog.command('add <src>')
144144
}
145145
})
146146

147+
prog.command('jpg <src>')
148+
.describe('Convert an image to high-quality JPG')
149+
.action(async (source: string) => {
150+
let spinner = ora().start()
151+
let { name, ext } = parse(source)
152+
153+
if (ext.endsWith('jpg')) {
154+
spinner.succeed('Image is already in JPG format!')
155+
// eslint-disable-next-line unicorn/no-process-exit
156+
process.exit(0)
157+
}
158+
159+
let sharpImage = sharp(source)
160+
spinner.text = 'Converting to JPG...'
161+
162+
try {
163+
await sharpImage
164+
.withMetadata()
165+
.toFormat('jpg', { quality: 100, chromaSubsampling: '4:4:4' })
166+
.toFile(`${name}.jpg`)
167+
spinner.succeed(`${name} converted to JPG format!`)
168+
} catch (error: unknown) {
169+
spinner.fail(String(error))
170+
}
171+
})
172+
147173
prog.parse(process.argv)

0 commit comments

Comments
 (0)