Skip to content

Commit 21304df

Browse files
authored
feat: throw an error when no file was found (#127)
1 parent 2ac7070 commit 21304df

File tree

8 files changed

+80
-9
lines changed

8 files changed

+80
-9
lines changed

.changeset/rotten-seals-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vite-plugin-static-copy': major
3+
---
4+
5+
feat: throw an error when does not find file

src/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export const buildPlugin = ({
2929
config.root,
3030
config.build.outDir,
3131
targets,
32-
structured
32+
structured,
33+
silent
3334
)
3435
if (!silent) outputCopyLog(config.logger, result)
3536
}

src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export type ViteStaticCopyOptions = {
9090
*/
9191
structured?: boolean
9292
/**
93-
* Suppress console output.
93+
* Suppress console output and ignore validation errors.
9494
* @default false
9595
*/
9696
silent?: boolean

src/serve.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ export const servePlugin = ({
3434
const copyTargets = await collectCopyTargets(
3535
config.root,
3636
targets,
37-
structured
37+
structured,
38+
silent
3839
)
3940
updateFileMapFromTargets(copyTargets, fileMap)
4041
} catch (e) {
41-
config.logger.error(formatConsole(pc.red((e as Error).toString())))
42+
if (!silent) {
43+
config.logger.error(formatConsole(pc.red((e as Error).toString())))
44+
}
4245
}
4346
}
4447
const collectFileMapDebounce = debounce(100, async () => {

src/utils.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ async function renameTarget(
3838
export const collectCopyTargets = async (
3939
root: string,
4040
targets: Target[],
41-
structured: boolean
41+
structured: boolean,
42+
silent: boolean
4243
) => {
4344
const copyTargets: SimpleTarget[] = []
4445

@@ -59,6 +60,9 @@ export const collectCopyTargets = async (
5960
cwd: root
6061
})
6162

63+
if (matchedPaths.length === 0 && !silent) {
64+
throw new Error(`No file was found to copy on ${src} src.`)
65+
}
6266
for (const matchedPath of matchedPaths) {
6367
const relativeMatchedPath = path.isAbsolute(matchedPath)
6468
? path.relative(root, matchedPath)
@@ -145,9 +149,15 @@ export const copyAll = async (
145149
rootSrc: string,
146150
rootDest: string,
147151
targets: Target[],
148-
structured: boolean
152+
structured: boolean,
153+
silent: boolean
149154
) => {
150-
const copyTargets = await collectCopyTargets(rootSrc, targets, structured)
155+
const copyTargets = await collectCopyTargets(
156+
rootSrc,
157+
targets,
158+
structured,
159+
silent
160+
)
151161
let copiedCount = 0
152162

153163
for (const copyTarget of copyTargets) {
@@ -244,8 +254,6 @@ export const outputCopyLog = (
244254
const skippedMessage =
245255
skipped > 0 ? ` ${pc.gray(`(Skipped ${skipped} items.)`)}` : ''
246256
logger.info(formatConsole(`${copiedMessage}${skippedMessage}`))
247-
} else {
248-
logger.warn(formatConsole(pc.yellow('No items to copy.')))
249257
}
250258
}
251259

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from 'vite'
2+
import { viteStaticCopy } from 'vite-plugin-static-copy'
3+
4+
export default defineConfig({
5+
plugins: [
6+
viteStaticCopy({
7+
targets: [
8+
{
9+
src: 'does-not-exist.txt',
10+
dest: 'does-not-exist'
11+
}
12+
],
13+
silent: true
14+
})
15+
]
16+
})

test/fixtures/vite.error.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from 'vite'
2+
import { viteStaticCopy } from 'vite-plugin-static-copy'
3+
4+
export default defineConfig({
5+
plugins: [
6+
viteStaticCopy({
7+
targets: [
8+
{
9+
src: 'does-not-exist.txt',
10+
dest: 'does-not-exist'
11+
}
12+
]
13+
})
14+
]
15+
})

test/tests.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,27 @@ describe('build', () => {
137137
}
138138
})
139139
}
140+
describe('on error', () => {
141+
test('should throw error when it does not find the file on given src', async () => {
142+
let result = ''
143+
try {
144+
await build(getConfig('vite.error.config.ts'))
145+
} catch (error: unknown) {
146+
result = (error as Error).message
147+
}
148+
expect(result).toContain(
149+
'No file was found to copy on does-not-exist.txt src.'
150+
)
151+
})
152+
153+
test('should not throw error when it does not find the file on given src as silent=true', async () => {
154+
let result = ''
155+
try {
156+
await build(getConfig('vite.error-silent.config.ts'))
157+
} catch (error: unknown) {
158+
result = (error as Error).message
159+
}
160+
expect(result).toBe('')
161+
})
162+
})
140163
})

0 commit comments

Comments
 (0)