From 21304df03f6ad668d316653ee9d48c4fbc633bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A1ssio?= Date: Wed, 9 Oct 2024 01:12:32 -0300 Subject: [PATCH] feat: throw an error when no file was found (#127) --- .changeset/rotten-seals-invite.md | 5 +++++ src/build.ts | 3 ++- src/options.ts | 2 +- src/serve.ts | 7 +++++-- src/utils.ts | 18 +++++++++++++----- test/fixtures/vite.error-silent.config.ts | 16 ++++++++++++++++ test/fixtures/vite.error.config.ts | 15 +++++++++++++++ test/tests.test.ts | 23 +++++++++++++++++++++++ 8 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 .changeset/rotten-seals-invite.md create mode 100644 test/fixtures/vite.error-silent.config.ts create mode 100644 test/fixtures/vite.error.config.ts diff --git a/.changeset/rotten-seals-invite.md b/.changeset/rotten-seals-invite.md new file mode 100644 index 0000000..807bb10 --- /dev/null +++ b/.changeset/rotten-seals-invite.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-static-copy': major +--- + +feat: throw an error when does not find file diff --git a/src/build.ts b/src/build.ts index 62c8223..15470f3 100644 --- a/src/build.ts +++ b/src/build.ts @@ -29,7 +29,8 @@ export const buildPlugin = ({ config.root, config.build.outDir, targets, - structured + structured, + silent ) if (!silent) outputCopyLog(config.logger, result) } diff --git a/src/options.ts b/src/options.ts index 219aec2..22e3233 100644 --- a/src/options.ts +++ b/src/options.ts @@ -90,7 +90,7 @@ export type ViteStaticCopyOptions = { */ structured?: boolean /** - * Suppress console output. + * Suppress console output and ignore validation errors. * @default false */ silent?: boolean diff --git a/src/serve.ts b/src/serve.ts index 76984ac..e92b766 100644 --- a/src/serve.ts +++ b/src/serve.ts @@ -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 () => { diff --git a/src/utils.ts b/src/utils.ts index 8c0f713..b5d5dfc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -38,7 +38,8 @@ async function renameTarget( export const collectCopyTargets = async ( root: string, targets: Target[], - structured: boolean + structured: boolean, + silent: boolean ) => { const copyTargets: SimpleTarget[] = [] @@ -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) @@ -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) { @@ -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.'))) } } diff --git a/test/fixtures/vite.error-silent.config.ts b/test/fixtures/vite.error-silent.config.ts new file mode 100644 index 0000000..54cb711 --- /dev/null +++ b/test/fixtures/vite.error-silent.config.ts @@ -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 + }) + ] +}) diff --git a/test/fixtures/vite.error.config.ts b/test/fixtures/vite.error.config.ts new file mode 100644 index 0000000..14ae183 --- /dev/null +++ b/test/fixtures/vite.error.config.ts @@ -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' + } + ] + }) + ] +}) diff --git a/test/tests.test.ts b/test/tests.test.ts index 06c2ccf..a64a985 100644 --- a/test/tests.test.ts +++ b/test/tests.test.ts @@ -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('') + }) + }) })