fs-extra#unlinkSync TypeScript Examples
The following examples show how to use
fs-extra#unlinkSync.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: image-compressor.ts From image-optimizer with MIT License | 4 votes |
#processFile = (file: DroppedFile, output: string) => {
const originalSize = getFileSize(file.path)
return new Promise<void>((resolve, reject) => {
switch (file.type) {
case MIME_TYPE_ENUM.jpg: {
const { quality } = store.app.get('mozjpeg')
let originalFile: string
const isAddTempFile =
!store.app.get('addToSubfolder') && !store.app.get('addMinSuffix')
if (isAddTempFile) {
originalFile = output + '.tmp'
copyFileSync(file.path, originalFile)
} else {
originalFile = file.path
}
execFile(
mozjpeg,
['-quality', `${quality}`, '-outfile', output, originalFile],
err => {
if (err) {
console.log(err)
reject(err)
}
const compressedSize = getFileSize(output)
this.#sendToRenderer(file, originalSize, compressedSize)
if (isAddTempFile) unlinkSync(originalFile)
resolve()
}
)
break
}
case MIME_TYPE_ENUM.png: {
const { qualityMin, qualityMax } = store.app.get('pngquant')
execFile(
pngquant,
[
'--quality',
`${qualityMin}-${qualityMax}`,
'-fo',
output,
file.path
],
err => {
if (err) {
console.log(err)
reject(err)
}
const compressedSize = getFileSize(output)
this.#sendToRenderer(file, originalSize, compressedSize)
resolve()
}
)
break
}
case MIME_TYPE_ENUM.gif: {
execFile(gifsicle, ['-o', output, file.path], err => {
if (err) {
console.log(err)
reject(err)
}
const compressedSize = getFileSize(output)
this.#sendToRenderer(file, originalSize, compressedSize)
resolve()
})
break
}
case MIME_TYPE_ENUM.svg: {
readFile(file.path, (err, buffer) => {
if (err) {
console.log(err)
reject(err)
}
const { data } = svg.optimize(buffer)
writeFile(output, data, err => {
if (err) console.log(err)
const compressedSize = getFileSize(output)
this.#sendToRenderer(file, originalSize, compressedSize)
resolve()
})
})
break
}
}
})
}