UNPKG

4.26 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 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};
26Object.defineProperty(exports, "__esModule", { value: true });
27const automation_client_1 = require("@atomist/automation-client");
28const projectUtils_1 = require("@atomist/automation-client/lib/project/util/projectUtils");
29const mime = require("mime-types");
30const path = require("path");
31function 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}
65exports.putFiles = putFiles;
66function 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//# sourceMappingURL=putS3.js.map
\No newline at end of file