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