Skip to content

Commit

Permalink
Enhanced transform functionality to include compression
Browse files Browse the repository at this point in the history
  • Loading branch information
mindflowgo committed Nov 4, 2024
1 parent 7b3b781 commit e729953
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ async function getCompressedContent(
}
//unknown encoding, return empty compressed
if (destExt == '') {
return { destExt, data: null }
return { destExt, compressedData: null }
}

const content = await fs.readFile(file)

const data = await new Promise<Buffer>((resolve, reject) => {
const compressedData = await new Promise<Buffer>((resolve, reject) => {
zlib[transform.compress || 'gzip'](
content,
(err: Error | null, result: Buffer) => {
Expand All @@ -142,7 +142,7 @@ async function getCompressedContent(
)
})

return { destExt, data }
return { destExt, compressedData }
}

export async function getTransformedContent(
Expand Down Expand Up @@ -177,18 +177,20 @@ async function transformCopy(
}
}

const { destExt, data } = await getCompressedContent(src, transform)
const { destExt, compressedData } = await getCompressedContent(src, transform)
// if this worked, adjust dest and use, else retry with getTransformedContent()
if (destExt) {
dest += destExt
}
const transformedContent = destExt
? data
: await getTransformedContent(src, transform)
if (transformedContent === null) {
return { copied: false }
if (compressedData === null) {
return { copied: false }
}
await fs.outputFile(dest + destExt, compressedData)
} else {
const transformedContent = await getTransformedContent(src, transform)
if (transformedContent === null) {
return { copied: false }
}
await fs.outputFile(dest, transformedContent)
}
await fs.outputFile(dest, transformedContent)
return { copied: true }
}

Expand Down

0 comments on commit e729953

Please sign in to comment.