Skip to content

Commit

Permalink
feat: throw an error when no file was found (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassioFront authored Oct 9, 2024
1 parent 2ac7070 commit 21304df
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-seals-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-static-copy': major
---

feat: throw an error when does not find file
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
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export type ViteStaticCopyOptions = {
*/
structured?: boolean
/**
* Suppress console output.
* Suppress console output and ignore validation errors.
* @default false
*/
silent?: boolean
Expand Down
7 changes: 5 additions & 2 deletions src/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ 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())))
if (!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: boolean
) => {
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: boolean
) => {
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).toContain(
'No file was found to copy on does-not-exist.txt src.'
)
})

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 21304df

Please sign in to comment.