Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: throw an error when no file was found #127

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 &&
Copy link
Contributor Author

@tassioFront tassioFront Sep 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it was missed. All logs here are silenceable

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('')
})
})
})
Loading