11const { execSync, execFileSync } = require ( "child_process" ) ;
2+ const { S3 } = require ( "aws-sdk" ) ;
23
34class LambdaNextjsPlugin {
45 constructor ( serverless , options ) {
@@ -7,9 +8,12 @@ class LambdaNextjsPlugin {
78
89 this . hooks = {
910 "before:package:initialize" : this . beforePackage . bind ( this ) ,
10- "before:deploy:deploy" : this . beforeDeploy . bind ( this ) ,
11+ "before:deploy:deploy" : this . uploadFilesToS3 . bind ( this ) ,
12+ "after:deploy:deploy" : this . uploadFilesToS3 . bind ( this ) ,
1113 "before:offline:start:init" : this . beforeOfflineStart . bind ( this ) ,
1214 } ;
15+
16+ this . s3Client = new S3 ( ) ;
1317 }
1418
1519 log ( message , ...others ) {
@@ -27,27 +31,38 @@ class LambdaNextjsPlugin {
2731 execFileSync ( `${ __dirname } /build-bridge.js` , { stdio : "inherit" } ) ;
2832 }
2933
30- beforeDeploy ( ) {
34+ async uploadFilesToS3 ( ) {
3135 const bucket =
3236 this . serverless . service . resources . Resources . StaticFilesBucket . Properties
3337 . BucketName ;
34- const profile = this . serverless . service . provider . profile ;
35- execSync (
36- `aws s3 sync .next/static s3://${ bucket } /_next/static ${
37- profile ? `--profile ${ profile } ` : ""
38- } `,
39- {
40- stdio : "inherit" ,
41- }
42- ) ;
43- execSync (
44- `aws s3 sync public s3://${ bucket } /public ${
45- profile ? `--profile ${ profile } ` : ""
46- } `,
47- {
48- stdio : "inherit" ,
49- }
50- ) ;
38+
39+ try {
40+ await this . s3Client
41+ . getBucketAcl ( {
42+ Bucket : bucket ,
43+ } )
44+ . promise ( ) ;
45+
46+ const profile = this . serverless . service . provider . profile ;
47+ execSync (
48+ `aws s3 sync .next/static s3://${ bucket } /_next/static ${
49+ profile ? `--profile ${ profile } ` : ""
50+ } `,
51+ {
52+ stdio : "inherit" ,
53+ }
54+ ) ;
55+ execSync (
56+ `aws s3 sync public s3://${ bucket } /public ${
57+ profile ? `--profile ${ profile } ` : ""
58+ } `,
59+ {
60+ stdio : "inherit" ,
61+ }
62+ ) ;
63+ } catch ( error ) {
64+ this . log ( "Bucket not available" ) ;
65+ }
5166 }
5267}
5368
0 commit comments