@@ -5,6 +5,7 @@ import url from "node:url";
55
66import type { BuildOptions as ESBuildOptions } from "esbuild" ;
77import { build as buildAsync , buildSync } from "esbuild" ;
8+ import { globSync } from "glob" ;
89import type {
910 CopyFile ,
1011 DefaultOverrideOptions ,
@@ -443,28 +444,55 @@ export function getPackagePath(options: BuildOptions) {
443444}
444445
445446/**
446- * Copy files that are specified in the `copyFiles` property of the OpenNext config into the output directory.
447+ * Copy files that are specified in the `copyFiles` property into the server functions output directory.
447448 *
448449 * @param copyFiles - Array of files to copy. Each file should have a `srcPath` and `dstPath` property.
449450 * @param outputPath - Path to the output directory.
450451 */
451452export function copyCustomFiles ( copyFiles : CopyFile [ ] , outputPath : string ) {
452453 copyFiles . forEach ( ( { srcPath, dstPath } ) => {
453- if ( ! fs . existsSync ( srcPath ) ) {
454- logger . warn (
455- `${ srcPath } was not found. Make sure this file exists. Can be a relative path to the app directory or an absolute path.` ,
456- ) ;
454+ // Find all files matching the pattern
455+ const matchedFiles = globSync ( srcPath , {
456+ nodir : true ,
457+ windowsPathsNoEscape : true ,
458+ } ) ;
459+
460+ if ( matchedFiles . length === 0 ) {
461+ logger . warn ( `No files found for pattern: ${ srcPath } ` ) ;
457462 return ;
458463 }
459464
460- // Create the destination directory if it doesn't exist
461- const fullDestPath = path . join ( outputPath , dstPath ) ;
462- const destDir = path . dirname ( fullDestPath ) ;
463- if ( ! fs . existsSync ( destDir ) ) {
464- fs . mkdirSync ( destDir , { recursive : true } ) ;
465+ if ( matchedFiles . length === 1 ) {
466+ // Single file match - use dstPath as it is
467+ const srcFile = matchedFiles [ 0 ] ;
468+ const fullDstPath = path . join ( outputPath , dstPath ) ;
469+
470+ copyFile ( srcFile , fullDstPath ) ;
471+ } else {
472+ // Multiple files matched, dstPath will become a directory
473+ matchedFiles . forEach ( ( srcFile ) => {
474+ const filename = path . basename ( srcFile ) ;
475+ const fullDstPath = path . join ( outputPath , dstPath , filename ) ;
476+ copyFile ( srcFile , fullDstPath ) ;
477+ } ) ;
465478 }
466-
467- fs . copyFileSync ( srcPath , fullDestPath ) ;
468- logger . debug ( `Copied ${ srcPath } to ${ fullDestPath } ` ) ;
469479 } ) ;
470480}
481+ /**
482+ * Copy a file to the destination path.
483+ *
484+ * @param srcFile - Path to the source file.
485+ * @param fullDstPath - Path to the destination file.
486+ */
487+ function copyFile ( srcFile : string , fullDstPath : string ) {
488+ const dstDir = path . dirname ( fullDstPath ) ;
489+
490+ if ( ! fs . existsSync ( dstDir ) ) {
491+ fs . mkdirSync ( dstDir , { recursive : true } ) ;
492+ }
493+ if ( fs . existsSync ( fullDstPath ) ) {
494+ logger . warn ( `File already exists: ${ fullDstPath } . It will be overwritten.` ) ;
495+ }
496+ fs . copyFileSync ( srcFile , fullDstPath ) ;
497+ logger . debug ( `Copied ${ srcFile } to ${ fullDstPath } ` ) ;
498+ }
0 commit comments