@dotcom-tool-kit/upload-assets-to-s3 4.0.2 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Task } from '@dotcom-tool-kit/base';
|
|
1
|
+
import { Task, TaskRunContext } from '@dotcom-tool-kit/base';
|
|
2
2
|
import { S3Client } from '@aws-sdk/client-s3';
|
|
3
|
-
import {
|
|
3
|
+
import { UploadAssetsToS3Schema } from '@dotcom-tool-kit/schemas/lib/tasks/upload-assets-to-s3';
|
|
4
4
|
export default class UploadAssetsToS3 extends Task<{
|
|
5
5
|
task: typeof UploadAssetsToS3Schema;
|
|
6
6
|
}> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
uploadAssetsToS3(options: UploadAssetsToS3Options): Promise<void>;
|
|
7
|
+
uploadFile(file: string, s3: S3Client): Promise<void>;
|
|
8
|
+
run({ cwd }: TaskRunContext): Promise<void>;
|
|
10
9
|
}
|
|
11
10
|
//# sourceMappingURL=upload-assets-to-s3.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload-assets-to-s3.d.ts","sourceRoot":"","sources":["../../src/tasks/upload-assets-to-s3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"upload-assets-to-s3.d.ts","sourceRoot":"","sources":["../../src/tasks/upload-assets-to-s3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAE5D,OAAO,EAAE,QAAQ,EAAoB,MAAM,oBAAoB,CAAA;AAM/D,OAAO,EACL,sBAAsB,EACvB,MAAM,wDAAwD,CAAA;AAE/D,MAAM,CAAC,OAAO,OAAO,gBAAiB,SAAQ,IAAI,CAAC;IAAE,IAAI,EAAE,OAAO,sBAAsB,CAAA;CAAE,CAAC;IACnF,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAiDrD,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;CAuClD"}
|
|
@@ -10,16 +10,13 @@ const glob_1 = require("glob");
|
|
|
10
10
|
const error_1 = require("@dotcom-tool-kit/error");
|
|
11
11
|
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
12
12
|
class UploadAssetsToS3 extends base_1.Task {
|
|
13
|
-
async
|
|
14
|
-
await this.uploadAssetsToS3(this.options);
|
|
15
|
-
}
|
|
16
|
-
async uploadFile(file, options, s3) {
|
|
13
|
+
async uploadFile(file, s3) {
|
|
17
14
|
const type = getFileType(file);
|
|
18
15
|
const encoding = getFileEncoding(file);
|
|
19
|
-
const filepath = path_1.default.join(options.directory, file);
|
|
16
|
+
const filepath = path_1.default.join(this.options.directory, file);
|
|
20
17
|
const body = fs.createReadStream(filepath);
|
|
21
|
-
const key = path_1.default.posix.join(options.destination, file);
|
|
22
|
-
const bucketByEnv = process.env.NODE_ENV === 'branch' ? options.reviewBucket : options.prodBucket;
|
|
18
|
+
const key = path_1.default.posix.join(this.options.destination, file);
|
|
19
|
+
const bucketByEnv = process.env.NODE_ENV === 'branch' ? this.options.reviewBucket : this.options.prodBucket;
|
|
23
20
|
let currentBucket = '';
|
|
24
21
|
try {
|
|
25
22
|
if (typeof bucketByEnv === 'string') {
|
|
@@ -30,7 +27,7 @@ class UploadAssetsToS3 extends base_1.Task {
|
|
|
30
27
|
ACL: 'public-read',
|
|
31
28
|
ContentType: `${type}; charset=utf-8`,
|
|
32
29
|
ContentEncoding: encoding,
|
|
33
|
-
CacheControl: options.cacheControl
|
|
30
|
+
CacheControl: this.options.cacheControl
|
|
34
31
|
};
|
|
35
32
|
currentBucket = params.Bucket;
|
|
36
33
|
await s3.send(new client_s3_1.PutObjectCommand(params));
|
|
@@ -45,7 +42,7 @@ class UploadAssetsToS3 extends base_1.Task {
|
|
|
45
42
|
ACL: 'public-read',
|
|
46
43
|
ContentType: `${type}; charset=utf-8`,
|
|
47
44
|
ContentEncoding: encoding,
|
|
48
|
-
CacheControl: options.cacheControl
|
|
45
|
+
CacheControl: this.options.cacheControl
|
|
49
46
|
};
|
|
50
47
|
currentBucket = params.Bucket;
|
|
51
48
|
await s3.send(new client_s3_1.PutObjectCommand(params));
|
|
@@ -61,33 +58,34 @@ class UploadAssetsToS3 extends base_1.Task {
|
|
|
61
58
|
throw error;
|
|
62
59
|
}
|
|
63
60
|
}
|
|
64
|
-
async
|
|
61
|
+
async run({ cwd }) {
|
|
65
62
|
// Wrap extensions in braces if there are multiple
|
|
66
|
-
const extensions = options.extensions.includes(',') ? `{${options.extensions}}` : options.extensions;
|
|
63
|
+
const extensions = this.options.extensions.includes(',') ? `{${this.options.extensions}}` : this.options.extensions;
|
|
67
64
|
const globFile = `**/*${extensions}`;
|
|
68
|
-
const
|
|
65
|
+
const resolvedDirectory = path_1.default.resolve(cwd, this.options.directory);
|
|
66
|
+
const files = glob_1.glob.sync(globFile, { cwd: resolvedDirectory, nodir: true });
|
|
69
67
|
if (files.length === 0) {
|
|
70
|
-
throw new error_1.ToolKitError(`no files found at the provided directory: ${
|
|
68
|
+
throw new error_1.ToolKitError(`no files found at the provided directory: ${resolvedDirectory}`);
|
|
71
69
|
}
|
|
72
|
-
const accessKeyId = process.env[options.accessKeyIdEnvVar];
|
|
73
|
-
const secretAccessKey = process.env[options.secretAccessKeyEnvVar];
|
|
70
|
+
const accessKeyId = process.env[this.options.accessKeyIdEnvVar];
|
|
71
|
+
const secretAccessKey = process.env[this.options.secretAccessKeyEnvVar];
|
|
74
72
|
if (!accessKeyId || !secretAccessKey) {
|
|
75
73
|
const missingVars = [
|
|
76
|
-
!accessKeyId ? options.accessKeyIdEnvVar : false,
|
|
77
|
-
!secretAccessKey ? options.secretAccessKeyEnvVar : false
|
|
74
|
+
!accessKeyId ? this.options.accessKeyIdEnvVar : false,
|
|
75
|
+
!secretAccessKey ? this.options.secretAccessKeyEnvVar : false
|
|
78
76
|
];
|
|
79
77
|
const error = new error_1.ToolKitError(`environment variable${missingVars.length > 1 ? 's' : ''} ${missingVars.join(' and ')} not set`);
|
|
80
78
|
error.details = `if your AWS credentials are stored in different environment variables, set the ${logger_1.styles.code('accessKeyIdEnvVar')} and ${logger_1.styles.code('secretAccessKeyEnvVar')} options for this task.`;
|
|
81
79
|
throw error;
|
|
82
80
|
}
|
|
83
81
|
const s3 = new client_s3_1.S3Client({
|
|
84
|
-
region: options.region,
|
|
82
|
+
region: this.options.region,
|
|
85
83
|
credentials: {
|
|
86
84
|
accessKeyId,
|
|
87
85
|
secretAccessKey
|
|
88
86
|
}
|
|
89
87
|
});
|
|
90
|
-
await Promise.all(files.map((file) => this.uploadFile(file,
|
|
88
|
+
await Promise.all(files.map((file) => this.uploadFile(file, s3)));
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
91
|
exports.default = UploadAssetsToS3;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcom-tool-kit/upload-assets-to-s3",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"scripts": {
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@aws-sdk/client-s3": "^3.256.0",
|
|
14
|
-
"@dotcom-tool-kit/base": "^1.
|
|
15
|
-
"@dotcom-tool-kit/error": "^4.0.
|
|
16
|
-
"@dotcom-tool-kit/logger": "^4.0.
|
|
14
|
+
"@dotcom-tool-kit/base": "^1.1.0",
|
|
15
|
+
"@dotcom-tool-kit/error": "^4.0.1",
|
|
16
|
+
"@dotcom-tool-kit/logger": "^4.0.1",
|
|
17
17
|
"glob": "^7.1.6",
|
|
18
18
|
"mime": "^2.5.2",
|
|
19
19
|
"tslib": "^2.3.1"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/upload-assets-to-s3",
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@aws-sdk/types": "^3.13.1",
|
|
30
|
-
"@dotcom-tool-kit/schemas": "^1.
|
|
30
|
+
"@dotcom-tool-kit/schemas": "^1.2.0",
|
|
31
31
|
"@jest/globals": "^27.4.6",
|
|
32
32
|
"@types/glob": "^7.1.3",
|
|
33
33
|
"@types/jest": "^27.4.0",
|