Skip to content

Commit 4206c02

Browse files
committed
feat(s3): trigger upload before/after but checking for assets bucket
This solves first deploys scenarios for now, but the solution needs to be improved later
1 parent 3203bd6 commit 4206c02

File tree

3 files changed

+497
-20
lines changed

3 files changed

+497
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"main": "src/index.js",
55
"license": "MIT",
66
"dependencies": {
7+
"aws-sdk": "^2.1230.0",
78
"serverless-http": "^3.0.1",
89
"webpack": "^5.73.0"
910
},

src/index.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { execSync, execFileSync } = require("child_process");
2+
const { S3 } = require("aws-sdk");
23

34
class 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

Comments
 (0)