UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

101 lines 4.34 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.streamFiles = exports.streamFilesFrom = exports.copyFiles = exports.copyFilesFrom = exports.copyFileFromUrl = void 0; const configuration_1 = require("@atomist/automation-client/lib/configuration"); const GitCommandGitProject_1 = require("@atomist/automation-client/lib/project/git/GitCommandGitProject"); const httpClient_1 = require("@atomist/automation-client/lib/spi/http/httpClient"); const logger_1 = require("@atomist/automation-client/lib/util/logger"); /** * Add the downloaded content to the given project * @param url url of the content. Must be publicly accessible * @param path */ function copyFileFromUrl(url, path) { return async (p) => { const http = configuration_1.configurationValue("http.client.factory", httpClient_1.defaultHttpClientFactory()); const response = await http.create(url).exchange(url, { method: httpClient_1.HttpMethod.Get }); return p.addFile(path, response.body); }; } exports.copyFileFromUrl = copyFileFromUrl; /** * Take the specified files from the donor project * @param {RemoteRepoRef} donorProjectId * @param {FileMapping[]} fileMappings * @param {ProjectOperationCredentials} credentials * @return {SimpleProjectEditor} */ function copyFilesFrom(donorProjectId, fileMappings, credentials) { return async (p, i) => { const donorProject = await GitCommandGitProject_1.GitCommandGitProject.cloned(credentials, donorProjectId); return copyFiles(donorProject, fileMappings)(p, i); }; } exports.copyFilesFrom = copyFilesFrom; function copyFiles(donorProject, fileMappings) { return async (p) => { for (const m of fileMappings) { const fm = typeof m === "string" ? { donorPath: m, recipientPath: m } : m; const found = await donorProject.getFile(fm.donorPath); if (found) { await p.addFile(fm.recipientPath, await found.getContent()); } else { logger_1.logger.debug("Path '%s' not found in donor project %s:%s", fm.donorPath, donorProject.id.owner, donorProject.id.repo); } } return p; }; } exports.copyFiles = copyFiles; /** * Take the specified files from the donor project * @param {RemoteRepoRef} donorProjectId * @param {FileGlobMapping} fileGlobMapping - treated as globs as defined in Project.streamFiles * @return {SimpleProjectEditor} */ function streamFilesFrom(donorProjectId, fileGlobMapping) { return async (p, i) => { const donorProject = await GitCommandGitProject_1.GitCommandGitProject.cloned(i.credentials, donorProjectId); return streamFiles(donorProject, fileGlobMapping)(p, i); }; } exports.streamFilesFrom = streamFilesFrom; function streamFiles(donorProject, fileGlobMapping) { return async (p) => { const fileStream = donorProject.streamFiles(...fileGlobMapping.globPatterns); await new Promise((resolve, reject) => { fileStream .on("end", () => { logger_1.logger.debug("end of file stream reached, using glob: ", fileGlobMapping); resolve(); }) .on("data", donorFile => { const newPath = (fileGlobMapping.recipientPath || "") + donorFile.path; p.addFileSync(newPath, donorFile.getContentSync()); logger_1.logger.log("silly", "file added: ", donorFile.path); }) .on("error", e => { logger_1.logger.debug("Error copying file: ", e); reject(e); }); }); }; } exports.streamFiles = streamFiles; //# sourceMappingURL=fileCopy.js.map