Skip to content

Commit

Permalink
feat: throw error on does not find file
Browse files Browse the repository at this point in the history
  • Loading branch information
tassioFront committed Sep 25, 2024
1 parent 2ac7070 commit 79435f4
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const buildPlugin = ({
config.root,
config.build.outDir,
targets,
structured
structured,
silent
)
if (!silent) outputCopyLog(config.logger, result)
}
Expand Down
6 changes: 4 additions & 2 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export const servePlugin = ({
const copyTargets = await collectCopyTargets(
config.root,
targets,
structured
structured,
silent
)
updateFileMapFromTargets(copyTargets, fileMap)
} catch (e) {
config.logger.error(formatConsole(pc.red((e as Error).toString())))
!silent &&
config.logger.error(formatConsole(pc.red((e as Error).toString())))
}
}
const collectFileMapDebounce = debounce(100, async () => {
Expand Down
18 changes: 13 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ async function renameTarget(
export const collectCopyTargets = async (
root: string,
targets: Target[],
structured: boolean
structured: boolean,
silent = false
) => {
const copyTargets: SimpleTarget[] = []

Expand All @@ -59,6 +60,9 @@ export const collectCopyTargets = async (
cwd: root
})

if (matchedPaths.length === 0 && !silent) {
throw new Error(`No file was found to copy on ${src} src.`)
}
for (const matchedPath of matchedPaths) {
const relativeMatchedPath = path.isAbsolute(matchedPath)
? path.relative(root, matchedPath)
Expand Down Expand Up @@ -145,9 +149,15 @@ export const copyAll = async (
rootSrc: string,
rootDest: string,
targets: Target[],
structured: boolean
structured: boolean,
silent = false
) => {
const copyTargets = await collectCopyTargets(rootSrc, targets, structured)
const copyTargets = await collectCopyTargets(
rootSrc,
targets,
structured,
silent
)
let copiedCount = 0

for (const copyTarget of copyTargets) {
Expand Down Expand Up @@ -244,8 +254,6 @@ export const outputCopyLog = (
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
16 changes: 16 additions & 0 deletions test/fixtures/vite.error-silent.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'

export default defineConfig({
plugins: [
viteStaticCopy({
targets: [
{
src: 'does-not-exist.txt',
dest: 'does-not-exist'
}
],
silent: true
})
]
})
15 changes: 15 additions & 0 deletions test/fixtures/vite.error.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'

export default defineConfig({
plugins: [
viteStaticCopy({
targets: [
{
src: 'does-not-exist.txt',
dest: 'does-not-exist'
}
]
})
]
})
23 changes: 23 additions & 0 deletions test/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,27 @@ describe('build', () => {
}
})
}
describe('on error', () => {
test('should throw error when it does not find the file on given src', async () => {
let result = ''
try {
await build(getConfig('vite.error.config.ts'))
} catch (error: unknown) {
result = (error as Error).message
}
expect(
result.includes('No file was found to copy on does-not-exist.txt src.')
).toBeTruthy()
})

test('should not throw error when it does not find the file on given src as silent=true', async () => {
let result = ''
try {
await build(getConfig('vite.error-silent.config.ts'))
} catch (error: unknown) {
result = (error as Error).message
}
expect(result).toBe('')
})
})
})

0 comments on commit 79435f4

Please sign in to comment.