@@ -4,38 +4,16 @@ import fse from "fs-extra";
44import readDirectoryFiles from "./lib/readDirectoryFiles" ;
55import filterOutDirectories from "./lib/filterOutDirectories" ;
66import { IMMUTABLE_CACHE_CONTROL_HEADER } from "./lib/constants" ;
7- import S3ClientFactory , { S3Client } from "./lib/s3" ;
7+ import S3ClientFactory from "./lib/s3" ;
88
99type UploadStaticAssetsOptions = {
1010 bucketName : string ;
1111 nextConfigDir : string ;
1212} ;
1313
14- const uploadPublicOrStaticDirectory = async (
15- s3 : S3Client ,
16- directory : "public" | "static" ,
17- nextConfigDir : string
18- ) : Promise < Promise < AWS . S3 . ManagedUpload . SendData > [ ] > => {
19- const files = await readDirectoryFiles ( path . join ( nextConfigDir , directory ) ) ;
20-
21- return files . filter ( filterOutDirectories ) . map ( fileItem =>
22- s3 . uploadFile ( {
23- filePath : fileItem . path ,
24- s3Key : path . posix . relative ( path . resolve ( nextConfigDir ) , fileItem . path )
25- } )
26- ) ;
27- } ;
28-
29- const filePathToS3Key = ( filePath : string ) : string => {
30- const relevantFilePathPart = filePath . substring (
31- filePath . indexOf ( ".next" + path . sep )
32- ) ;
33- return relevantFilePathPart . replace ( ".next" , "_next" ) ;
34- } ;
35-
3614const uploadStaticAssets = async (
3715 options : UploadStaticAssetsOptions
38- ) : Promise < AWS . S3 . ManagedUpload . SendData > => {
16+ ) : Promise < AWS . S3 . ManagedUpload . SendData [ ] > => {
3917 const { bucketName, nextConfigDir } = options ;
4018
4119 const s3 = S3ClientFactory ( {
@@ -55,7 +33,9 @@ const uploadStaticAssets = async (
5533 const nextBuildFileUploads = buildStaticFiles
5634 . filter ( filterOutDirectories )
5735 . map ( async fileItem => {
58- const s3Key = filePathToS3Key ( fileItem . path ) ;
36+ const s3Key = path
37+ . relative ( path . resolve ( nextConfigDir ) , fileItem . path )
38+ . replace ( / ^ .n e x t / , "_next" ) ;
5939
6040 return s3 . uploadFile ( {
6141 s3Key,
@@ -77,22 +57,29 @@ const uploadStaticAssets = async (
7757 ) ;
7858
7959 return s3 . uploadFile ( {
80- s3Key : `static-pages/${ relativePageFilePath . replace ( / ^ p a g e s \/ / , "" ) } ` ,
60+ s3Key : `static-pages/${ ( relativePageFilePath as string ) . replace (
61+ / ^ p a g e s \/ / ,
62+ ""
63+ ) } `,
8164 filePath : pageFilePath
8265 } ) ;
8366 } ) ;
8467
85- const publicDirUploads = await uploadPublicOrStaticDirectory (
86- s3 ,
87- "public" ,
88- nextConfigDir
89- ) ;
68+ const uploadPublicOrStaticDirectory = async (
69+ directory : "public" | "static"
70+ ) : Promise < Promise < AWS . S3 . ManagedUpload . SendData > [ ] > => {
71+ const files = await readDirectoryFiles ( path . join ( nextConfigDir , directory ) ) ;
9072
91- const staticDirUploads = await uploadPublicOrStaticDirectory (
92- s3 ,
93- "static" ,
94- nextConfigDir
95- ) ;
73+ return files . filter ( filterOutDirectories ) . map ( fileItem =>
74+ s3 . uploadFile ( {
75+ filePath : fileItem . path ,
76+ s3Key : path . posix . relative ( path . resolve ( nextConfigDir ) , fileItem . path )
77+ } )
78+ ) ;
79+ } ;
80+
81+ const publicDirUploads = await uploadPublicOrStaticDirectory ( "public" ) ;
82+ const staticDirUploads = await uploadPublicOrStaticDirectory ( "static" ) ;
9683
9784 const allUploads = [
9885 ...nextBuildFileUploads ,
0 commit comments