@dotcom-tool-kit/upload-assets-to-s3 1.2.3 → 1.4.2
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,8 +1,11 @@
|
|
|
1
1
|
import { Task } from '@dotcom-tool-kit/types';
|
|
2
|
+
import aws from 'aws-sdk';
|
|
2
3
|
import { UploadAssetsToS3Options, UploadAssetsToS3Schema } from '@dotcom-tool-kit/types/lib/schema/upload-assets-to-s3';
|
|
3
4
|
export default class UploadAssetsToS3 extends Task<typeof UploadAssetsToS3Schema> {
|
|
4
5
|
static description: string;
|
|
5
6
|
static defaultOptions: UploadAssetsToS3Options;
|
|
6
7
|
run(): Promise<void>;
|
|
8
|
+
uploadFile(file: string, options: UploadAssetsToS3Options, s3: aws.S3): Promise<void>;
|
|
9
|
+
uploadAssetsToS3(options: UploadAssetsToS3Options): Promise<void>;
|
|
7
10
|
}
|
|
8
11
|
//# 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,wBAAwB,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,MAAM,wBAAwB,CAAA;AAE7C,OAAO,GAAG,MAAM,SAAS,CAAA;AAMzB,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACvB,MAAM,uDAAuD,CAAA;AAE9D,MAAM,CAAC,OAAO,OAAO,gBAAiB,SAAQ,IAAI,CAAC,OAAO,sBAAsB,CAAC;IAC/E,MAAM,CAAC,WAAW,SAAK;IAEvB,MAAM,CAAC,cAAc,EAAE,uBAAuB,CAS7C;IAEK,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAGpB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA0DrF,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,IAAI,CAAC;CAaxE"}
|
|
@@ -7,9 +7,78 @@ const aws_sdk_1 = (0, tslib_1.__importDefault)(require("aws-sdk"));
|
|
|
7
7
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
8
8
|
const mime_1 = (0, tslib_1.__importDefault)(require("mime"));
|
|
9
9
|
const glob_1 = require("glob");
|
|
10
|
+
const error_1 = require("@dotcom-tool-kit/error");
|
|
11
|
+
const logger_1 = require("@dotcom-tool-kit/logger");
|
|
10
12
|
class UploadAssetsToS3 extends types_1.Task {
|
|
11
13
|
async run() {
|
|
12
|
-
await uploadAssetsToS3(this.options);
|
|
14
|
+
await this.uploadAssetsToS3(this.options);
|
|
15
|
+
}
|
|
16
|
+
async uploadFile(file, options, s3) {
|
|
17
|
+
const basename = file.split('/').splice(1).join('/'); // remove first directory only
|
|
18
|
+
const type = getFileType(basename);
|
|
19
|
+
const encoding = getFileEncoding(basename);
|
|
20
|
+
const key = path_1.default.posix.join(options.destination, basename);
|
|
21
|
+
const params = {
|
|
22
|
+
Bucket: '',
|
|
23
|
+
Key: key,
|
|
24
|
+
Body: fs.createReadStream(file),
|
|
25
|
+
ACL: 'public-read',
|
|
26
|
+
ContentType: `${type}; charset=utf-8`,
|
|
27
|
+
ContentEncoding: encoding,
|
|
28
|
+
CacheControl: options.cacheControl
|
|
29
|
+
};
|
|
30
|
+
const bucketByEnv = process.env.NODE_ENV === 'branch' ? options.reviewBucket : options.prodBucket;
|
|
31
|
+
let currentBucket = '';
|
|
32
|
+
try {
|
|
33
|
+
if (typeof bucketByEnv === 'string') {
|
|
34
|
+
const params = {
|
|
35
|
+
Bucket: bucketByEnv,
|
|
36
|
+
Key: key,
|
|
37
|
+
Body: fs.createReadStream(file),
|
|
38
|
+
ACL: 'public-read',
|
|
39
|
+
ContentType: `${type}; charset=utf-8`,
|
|
40
|
+
ContentEncoding: encoding,
|
|
41
|
+
CacheControl: options.cacheControl
|
|
42
|
+
};
|
|
43
|
+
currentBucket = params.Bucket;
|
|
44
|
+
const data = await s3.upload(params).promise();
|
|
45
|
+
this.logger.info(`Uploaded ${logger_1.styles.filepath(basename)} to ${logger_1.styles.URL(data.Location)}`);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
for (const bucket of bucketByEnv) {
|
|
49
|
+
const params = {
|
|
50
|
+
Bucket: bucket,
|
|
51
|
+
Key: key,
|
|
52
|
+
Body: fs.createReadStream(file),
|
|
53
|
+
ACL: 'public-read',
|
|
54
|
+
ContentType: `${type}; charset=utf-8`,
|
|
55
|
+
ContentEncoding: encoding,
|
|
56
|
+
CacheControl: options.cacheControl
|
|
57
|
+
};
|
|
58
|
+
currentBucket = params.Bucket;
|
|
59
|
+
const data = await s3.upload(params).promise();
|
|
60
|
+
this.logger.info(`Uploaded ${logger_1.styles.filepath(basename)} to ${logger_1.styles.URL(data.Location)}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
const error = new error_1.ToolKitError(`Upload of ${basename} to ${currentBucket} failed`);
|
|
66
|
+
if (err instanceof Error) {
|
|
67
|
+
error.details = err.message;
|
|
68
|
+
}
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async uploadAssetsToS3(options) {
|
|
73
|
+
// Wrap extensions in braces if there are multiple
|
|
74
|
+
const extensions = options.extensions.includes(',') ? `{${options.extensions}}` : options.extensions;
|
|
75
|
+
const globFile = `${options.directory}/**/*${extensions}`;
|
|
76
|
+
const files = glob_1.glob.sync(globFile);
|
|
77
|
+
const s3 = new aws_sdk_1.default.S3({
|
|
78
|
+
accessKeyId: options.accessKeyId,
|
|
79
|
+
secretAccessKey: options.secretAccessKey
|
|
80
|
+
});
|
|
81
|
+
await Promise.all(files.map((file) => this.uploadFile(file, options, s3)));
|
|
13
82
|
}
|
|
14
83
|
}
|
|
15
84
|
exports.default = UploadAssetsToS3;
|
|
@@ -39,67 +108,3 @@ const getFileEncoding = (filename) => {
|
|
|
39
108
|
return 'br';
|
|
40
109
|
}
|
|
41
110
|
};
|
|
42
|
-
const uploadFile = async (file, options, s3) => {
|
|
43
|
-
const basename = file.split('/').splice(1).join('/'); // remove first directory only
|
|
44
|
-
const type = getFileType(basename);
|
|
45
|
-
const encoding = getFileEncoding(basename);
|
|
46
|
-
const key = path_1.default.posix.join(options.destination, basename);
|
|
47
|
-
const params = {
|
|
48
|
-
Bucket: '',
|
|
49
|
-
Key: key,
|
|
50
|
-
Body: fs.createReadStream(file),
|
|
51
|
-
ACL: 'public-read',
|
|
52
|
-
ContentType: `${type}; charset=utf-8`,
|
|
53
|
-
ContentEncoding: encoding,
|
|
54
|
-
CacheControl: options.cacheControl
|
|
55
|
-
};
|
|
56
|
-
const bucketByEnv = process.env.NODE_ENV === 'branch' ? options.reviewBucket : options.prodBucket;
|
|
57
|
-
let currentBucket = '';
|
|
58
|
-
try {
|
|
59
|
-
if (typeof bucketByEnv === 'string') {
|
|
60
|
-
const params = {
|
|
61
|
-
Bucket: bucketByEnv,
|
|
62
|
-
Key: key,
|
|
63
|
-
Body: fs.createReadStream(file),
|
|
64
|
-
ACL: 'public-read',
|
|
65
|
-
ContentType: `${type}; charset=utf-8`,
|
|
66
|
-
ContentEncoding: encoding,
|
|
67
|
-
CacheControl: options.cacheControl
|
|
68
|
-
};
|
|
69
|
-
currentBucket = params.Bucket;
|
|
70
|
-
const data = await s3.upload(params).promise();
|
|
71
|
-
console.log(`Uploaded ${basename} to ${data.Location}`);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
for (const bucket of bucketByEnv) {
|
|
75
|
-
const params = {
|
|
76
|
-
Bucket: bucket,
|
|
77
|
-
Key: key,
|
|
78
|
-
Body: fs.createReadStream(file),
|
|
79
|
-
ACL: 'public-read',
|
|
80
|
-
ContentType: `${type}; charset=utf-8`,
|
|
81
|
-
ContentEncoding: encoding,
|
|
82
|
-
CacheControl: options.cacheControl
|
|
83
|
-
};
|
|
84
|
-
currentBucket = params.Bucket;
|
|
85
|
-
const data = await s3.upload(params).promise();
|
|
86
|
-
console.log(`Uploaded ${basename} to ${data.Location}`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
console.error(`Upload of ${basename} to ${currentBucket} failed`);
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
async function uploadAssetsToS3(options) {
|
|
96
|
-
// Wrap extensions in braces if there are multiple
|
|
97
|
-
const extensions = options.extensions.includes(',') ? `{${options.extensions}}` : options.extensions;
|
|
98
|
-
const globFile = `${options.directory}/**/*${extensions}`;
|
|
99
|
-
const files = glob_1.glob.sync(globFile);
|
|
100
|
-
const s3 = new aws_sdk_1.default.S3({
|
|
101
|
-
accessKeyId: options.accessKeyId,
|
|
102
|
-
secretAccessKey: options.secretAccessKey
|
|
103
|
-
});
|
|
104
|
-
return Promise.all(files.map((file) => uploadFile(file, options, s3)));
|
|
105
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcom-tool-kit/upload-assets-to-s3",
|
|
3
|
-
"version": "1.2
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
"author": "FT.com Platforms Team <platforms-team.customer-products@ft.com>",
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@dotcom-tool-kit/
|
|
13
|
+
"@dotcom-tool-kit/error": "^1.4.2",
|
|
14
|
+
"@dotcom-tool-kit/logger": "^1.4.2",
|
|
15
|
+
"@dotcom-tool-kit/types": "^1.4.2",
|
|
14
16
|
"aws-sdk": "^2.901.0",
|
|
15
17
|
"glob": "^7.1.6",
|
|
16
18
|
"mime": "^2.5.2"
|
|
@@ -24,10 +26,11 @@
|
|
|
24
26
|
"homepage": "https://github.com/financial-times/dotcom-tool-kit/tree/main/plugins/upload-assets-to-s3",
|
|
25
27
|
"devDependencies": {
|
|
26
28
|
"@aws-sdk/types": "^3.13.1",
|
|
27
|
-
"@jest/globals": "^
|
|
29
|
+
"@jest/globals": "^27.4.6",
|
|
28
30
|
"@types/glob": "^7.1.3",
|
|
29
|
-
"@types/jest": "^
|
|
30
|
-
"@types/mime": "^2.0.3"
|
|
31
|
+
"@types/jest": "^27.4.0",
|
|
32
|
+
"@types/mime": "^2.0.3",
|
|
33
|
+
"winston": "^3.4.0"
|
|
31
34
|
},
|
|
32
35
|
"files": [
|
|
33
36
|
"/lib",
|