1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
18 | return new (P || (P = Promise))(function (resolve, reject) {
|
19 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
20 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
21 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
22 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
23 | });
|
24 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const automation_client_1 = require("@atomist/automation-client");
|
27 | const projectUtils_1 = require("@atomist/automation-client/lib/project/util/projectUtils");
|
28 | const mime = require("mime-types");
|
29 | const path = require("path");
|
30 | function putFiles(project, inv, s3, params) {
|
31 | return __awaiter(this, void 0, void 0, function* () {
|
32 | const { bucketName, filesToPublish, pathTranslation } = params;
|
33 | let fileCount = 0;
|
34 | const log = inv.progressLog;
|
35 | const keys = [];
|
36 | const warnings = [];
|
37 | yield projectUtils_1.doWithFiles(project, filesToPublish, (file) => __awaiter(this, void 0, void 0, function* () {
|
38 | const key = pathTranslation(file.path, inv);
|
39 | const contentType = mime.lookup(file.path) || "text/plain";
|
40 | const content = yield file.getContentBuffer();
|
41 | let fileParams = {};
|
42 | if (params.paramsExt) {
|
43 | const [retrievedFileParams, additionalWarnings] = yield gatherParamsFromCompanionFile(project, log, file, params.paramsExt);
|
44 | fileParams = retrievedFileParams;
|
45 | additionalWarnings.forEach(w => warnings.push(w));
|
46 | }
|
47 | const objectParams = Object.assign({ Bucket: bucketName, Key: key, Body: content, ContentType: contentType }, fileParams);
|
48 | automation_client_1.logger.debug(`File: ${file.path}, key: ${key}, contentType: ${contentType}`);
|
49 | try {
|
50 | yield s3.putObject(objectParams).promise();
|
51 | keys.push(key);
|
52 | fileCount++;
|
53 | log.write(`Put '${file.path}' to 's3://${bucketName}/${key}'`);
|
54 | }
|
55 | catch (e) {
|
56 | const msg = `Failed to put '${file.path}' to 's3://${bucketName}/${key}': ${e.code}: ${e.message}`;
|
57 | log.write(msg);
|
58 | warnings.push(msg);
|
59 | }
|
60 | }));
|
61 | return [fileCount, keys, warnings];
|
62 | });
|
63 | }
|
64 | exports.putFiles = putFiles;
|
65 | function gatherParamsFromCompanionFile(project, log, file, companionFileExtension) {
|
66 | return __awaiter(this, void 0, void 0, function* () {
|
67 | const companionFilePrefix = ".";
|
68 | const paramsPath = path.dirname(file.path) + path.sep +
|
69 | `${companionFilePrefix}${file.name}${companionFileExtension}`;
|
70 | const paramsFile = yield project.getFile(paramsPath);
|
71 | if (!paramsFile) {
|
72 | return [{}, []];
|
73 | }
|
74 | try {
|
75 | const fileParams = JSON.parse(yield paramsFile.getContent());
|
76 | log.write(`Merging in S3 parameters from '${paramsPath}': ${JSON.stringify(fileParams)}`);
|
77 | return [fileParams, []];
|
78 | }
|
79 | catch (e) {
|
80 | const msg = `Failed to read and parse S3 params file '${paramsPath}', using defaults: ${e.message}`;
|
81 | log.write(msg);
|
82 | return [{}, [msg]];
|
83 | }
|
84 | });
|
85 | }
|
86 |
|
\ | No newline at end of file |