UNPKG

4.34 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 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.streamFiles = exports.streamFilesFrom = exports.copyFiles = exports.copyFilesFrom = exports.copyFileFromUrl = void 0;
19const configuration_1 = require("@atomist/automation-client/lib/configuration");
20const GitCommandGitProject_1 = require("@atomist/automation-client/lib/project/git/GitCommandGitProject");
21const httpClient_1 = require("@atomist/automation-client/lib/spi/http/httpClient");
22const logger_1 = require("@atomist/automation-client/lib/util/logger");
23/**
24 * Add the downloaded content to the given project
25 * @param url url of the content. Must be publicly accessible
26 * @param path
27 */
28function copyFileFromUrl(url, path) {
29 return async (p) => {
30 const http = configuration_1.configurationValue("http.client.factory", httpClient_1.defaultHttpClientFactory());
31 const response = await http.create(url).exchange(url, { method: httpClient_1.HttpMethod.Get });
32 return p.addFile(path, response.body);
33 };
34}
35exports.copyFileFromUrl = copyFileFromUrl;
36/**
37 * Take the specified files from the donor project
38 * @param {RemoteRepoRef} donorProjectId
39 * @param {FileMapping[]} fileMappings
40 * @param {ProjectOperationCredentials} credentials
41 * @return {SimpleProjectEditor}
42 */
43function copyFilesFrom(donorProjectId, fileMappings, credentials) {
44 return async (p, i) => {
45 const donorProject = await GitCommandGitProject_1.GitCommandGitProject.cloned(credentials, donorProjectId);
46 return copyFiles(donorProject, fileMappings)(p, i);
47 };
48}
49exports.copyFilesFrom = copyFilesFrom;
50function copyFiles(donorProject, fileMappings) {
51 return async (p) => {
52 for (const m of fileMappings) {
53 const fm = typeof m === "string" ? { donorPath: m, recipientPath: m } : m;
54 const found = await donorProject.getFile(fm.donorPath);
55 if (found) {
56 await p.addFile(fm.recipientPath, await found.getContent());
57 }
58 else {
59 logger_1.logger.debug("Path '%s' not found in donor project %s:%s", fm.donorPath, donorProject.id.owner, donorProject.id.repo);
60 }
61 }
62 return p;
63 };
64}
65exports.copyFiles = copyFiles;
66/**
67 * Take the specified files from the donor project
68 * @param {RemoteRepoRef} donorProjectId
69 * @param {FileGlobMapping} fileGlobMapping - treated as globs as defined in Project.streamFiles
70 * @return {SimpleProjectEditor}
71 */
72function streamFilesFrom(donorProjectId, fileGlobMapping) {
73 return async (p, i) => {
74 const donorProject = await GitCommandGitProject_1.GitCommandGitProject.cloned(i.credentials, donorProjectId);
75 return streamFiles(donorProject, fileGlobMapping)(p, i);
76 };
77}
78exports.streamFilesFrom = streamFilesFrom;
79function streamFiles(donorProject, fileGlobMapping) {
80 return async (p) => {
81 const fileStream = donorProject.streamFiles(...fileGlobMapping.globPatterns);
82 await new Promise((resolve, reject) => {
83 fileStream
84 .on("end", () => {
85 logger_1.logger.debug("end of file stream reached, using glob: ", fileGlobMapping);
86 resolve();
87 })
88 .on("data", donorFile => {
89 const newPath = (fileGlobMapping.recipientPath || "") + donorFile.path;
90 p.addFileSync(newPath, donorFile.getContentSync());
91 logger_1.logger.log("silly", "file added: ", donorFile.path);
92 })
93 .on("error", e => {
94 logger_1.logger.debug("Error copying file: ", e);
95 reject(e);
96 });
97 });
98 };
99}
100exports.streamFiles = streamFiles;
101//# sourceMappingURL=fileCopy.js.map
\No newline at end of file