-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s3-plugin): Remove prepare option to create bucket
- Loading branch information
Showing
9 changed files
with
22 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-22.1 KB
packages/reg-publish-s3-plugin/e2e/report-fixture/dir_a/sample01.png
Binary file not shown.
Binary file removed
BIN
-22.1 KB
packages/reg-publish-s3-plugin/e2e/report-fixture/dir_b/sample01.png
Binary file not shown.
16 changes: 0 additions & 16 deletions
16
packages/reg-publish-s3-plugin/e2e/report-fixture/index.html
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,29 @@ | ||
import { v4 as uuid } from "uuid"; | ||
import { CreateBucketCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3"; | ||
import { PluginPreparer, PluginCreateOptions, PluginLogger } from "reg-suit-interface"; | ||
import { PluginConfig } from "./s3-publisher-plugin"; | ||
|
||
export interface SetupInquireResult { | ||
createBucket: boolean; | ||
bucketName?: string; | ||
bucketName: string; | ||
} | ||
|
||
function createPolicy(bucketName: string) { | ||
return { | ||
Version: "2012-10-17", | ||
Id: "Policy1498486961145", | ||
Statement: [ | ||
{ | ||
Sid: "Stmt1498486956732", | ||
Effect: "Allow", | ||
Principal: "*", | ||
Action: "s3:GetObject", | ||
Resource: `arn:aws:s3:::${bucketName}/*`, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
const BUCKET_PREFIX = "reg-publish-bucket"; | ||
|
||
export class S3BucketPreparer implements PluginPreparer<SetupInquireResult, PluginConfig> { | ||
private _s3client = new S3Client(); | ||
_logger!: PluginLogger; | ||
|
||
inquire() { | ||
return [ | ||
{ | ||
name: "createBucket", | ||
type: "confirm", | ||
message: "Create a new S3 bucket", | ||
default: true, | ||
}, | ||
{ | ||
name: "bucketName", | ||
type: "input", | ||
message: "Existing bucket name", | ||
when: (ctx: any) => !(ctx as { createBucket: boolean }).createBucket, | ||
message: "Bucket name", | ||
}, | ||
]; | ||
} | ||
|
||
prepare(config: PluginCreateOptions<SetupInquireResult>) { | ||
async prepare(config: PluginCreateOptions<SetupInquireResult>) { | ||
this._logger = config.logger; | ||
const ir = config.options; | ||
if (!ir.createBucket) { | ||
return Promise.resolve({ | ||
bucketName: ir.bucketName as string, | ||
}); | ||
} else { | ||
const id = uuid(); | ||
const bucketName = `${BUCKET_PREFIX}-${id}`; | ||
if (config.noEmit) { | ||
this._logger.info(`Skip to create S3 bucket ${bucketName} because noEmit option.`); | ||
return Promise.resolve({ bucketName }); | ||
} | ||
this._logger.info(`Create new S3 bucket: ${this._logger.colors.magenta(bucketName)}`); | ||
const spinner = this._logger.getSpinner(`creating bucket...`); | ||
spinner.start(); | ||
return this._createBucket(bucketName) | ||
.then(bucketName => { | ||
return this._updatePolicy(bucketName); | ||
}) | ||
.then(bucketName => { | ||
spinner.stop(); | ||
return { bucketName }; | ||
}); | ||
} | ||
} | ||
|
||
_updatePolicy(bucketName: string) { | ||
return new Promise<string>((resolve, reject) => { | ||
this._s3client | ||
.send( | ||
new PutBucketPolicyCommand({ | ||
Bucket: bucketName, | ||
Policy: JSON.stringify(createPolicy(bucketName)), | ||
}), | ||
) | ||
.then(() => resolve(bucketName)) | ||
.catch(err => reject(err)); | ||
}); | ||
} | ||
|
||
_createBucket(bucketName: string) { | ||
return new Promise<string>((resolve, reject) => { | ||
this._s3client | ||
.send( | ||
new CreateBucketCommand({ | ||
Bucket: bucketName, | ||
}), | ||
) | ||
.then(() => resolve(bucketName)) | ||
.catch(err => reject(err)); | ||
}); | ||
const { bucketName } = config.options; | ||
const pluginConfig: PluginConfig = { | ||
bucketName, | ||
}; | ||
return pluginConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters