Skip to content

Commit 1c227d4

Browse files
committed
add glob to copyFiles
1 parent 1bdb6f0 commit 1c227d4

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

packages/open-next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"chalk": "^5.3.0",
5151
"esbuild": "0.19.2",
5252
"express": "5.0.1",
53+
"glob": "catalog:",
5354
"path-to-regexp": "^6.3.0",
5455
"urlpattern-polyfill": "^10.0.0",
5556
"yaml": "^2.7.0"

packages/open-next/src/build/helper.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import url from "node:url";
55

66
import type { BuildOptions as ESBuildOptions } from "esbuild";
77
import { build as buildAsync, buildSync } from "esbuild";
8+
import { globSync } from "glob";
89
import 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
*/
451452
export 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+
}

packages/open-next/src/types/open-next.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ export interface DefaultFunctionOptions<
291291
}
292292

293293
/**
294-
* @srcPath The path to the file to copy. Can be absolute or relative path.
295-
* @dstPath The path to the destination in the server function. Must be a relative path.
294+
* @srcPath The path to the file to copy. Can be absolute or relative path. Can use glob pattern.
295+
* @dstPath The relative path to the destination in the server function. Will become a directory if multiple files are found in srcPath.
296296
*/
297297
export interface CopyFile {
298298
srcPath: string;
@@ -324,6 +324,7 @@ export interface FunctionOptions extends DefaultFunctionOptions {
324324
experimentalBundledNextServer?: boolean;
325325
/**
326326
* Manually copy files into the server function after the build.
327+
* If multiple files are found with srcPath, dstPath will become a directory.
327328
* @copyFiles The files to copy. Is an array of objects with srcPath and dstPath.
328329
* @example
329330
* ```ts

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ catalog:
1313
postcss: 8.4.27
1414
tailwindcss: 3.3.3
1515
typescript: 5.6.3
16+
glob: ^11.0.1

0 commit comments

Comments
 (0)