Skip to content

Commit

Permalink
feat: show skipped items count
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed May 29, 2023
1 parent 1a0faa4 commit 12e0847
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export const buildPlugin = ({
config = _config
},
async writeBundle() {
const copyCount = await copyAll(
const result = await copyAll(
config.root,
config.build.outDir,
targets,
flatten
)
if (!silent) outputCopyLog(config.logger, copyCount)
if (!silent) outputCopyLog(config.logger, result)
}
}
}
39 changes: 30 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ async function transformCopy(
src: string,
dest: string,
overwrite: boolean | 'error'
) {
): Promise<{ copied: boolean }> {
if (overwrite === false || overwrite === 'error') {
const exists = await fsExists(dest)
if (exists) {
if (overwrite === false) {
return // skip copy
// skip copy
return { copied: false }
}
if (overwrite === 'error') {
throw new Error(`File ${dest} already exists`)
Expand All @@ -125,9 +126,11 @@ async function transformCopy(
}

const transformedContent = await getTransformedContent(src, transform)
if (transformedContent !== null) {
await fs.outputFile(dest, transformedContent)
if (transformedContent === null) {
return { copied: false }
}
await fs.outputFile(dest, transformedContent)
return { copied: true }
}

export const copyAll = async (
Expand All @@ -137,6 +140,8 @@ export const copyAll = async (
flatten: boolean
) => {
const copyTargets = await collectCopyTargets(rootSrc, targets, flatten)
let copiedCount = 0

for (const copyTarget of copyTargets) {
const { src, dest, transform, preserveTimestamps, dereference, overwrite } =
copyTarget
Expand All @@ -146,18 +151,27 @@ export const copyAll = async (
const resolvedDest = path.resolve(rootSrc, rootDest, dest)
const transformOption = resolveTransformOption(transform)
if (transformOption) {
await transformCopy(transformOption, resolvedSrc, resolvedDest, overwrite)
const result = await transformCopy(
transformOption,
resolvedSrc,
resolvedDest,
overwrite
)
if (result.copied) {
copiedCount++
}
} else {
await fs.copy(resolvedSrc, resolvedDest, {
preserveTimestamps,
dereference,
overwrite: overwrite === true,
errorOnExist: overwrite === 'error'
})
copiedCount++
}
}

return copyTargets.length
return { targets: copyTargets.length, copied: copiedCount }
}

export const updateFileMapFromTargets = (
Expand Down Expand Up @@ -213,9 +227,16 @@ export const outputCollectedLog = (logger: Logger, collectedMap: FileMap) => {
}
}

export const outputCopyLog = (logger: Logger, copyCount: number) => {
if (copyCount > 0) {
logger.info(formatConsole(pc.green(`Copied ${copyCount} items.`)))
export const outputCopyLog = (
logger: Logger,
result: { targets: number; copied: number }
) => {
if (result.targets > 0) {
const copiedMessage = pc.green(`Copied ${result.copied} items.`)
const skipped = result.targets - result.copied
const skippedMessage =
skipped > 0 ? ` ${pc.gray(`(Skipped ${skipped} items.)`)}` : ''
logger.info(formatConsole(`${copiedMessage}${skippedMessage}`))
} else {
logger.warn(formatConsole(pc.yellow('No items to copy.')))
}
Expand Down

0 comments on commit 12e0847

Please sign in to comment.