UNPKG

4.18 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17var __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};
25Object.defineProperty(exports, "__esModule", { value: true });
26const automation_client_1 = require("@atomist/automation-client");
27const projectUtils_1 = require("@atomist/automation-client/lib/project/util/projectUtils");
28const mime = require("mime-types");
29const path = require("path");
30function 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}
64exports.putFiles = putFiles;
65function 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//# sourceMappingURL=putS3.js.map
\No newline at end of file