UNPKG

vtex

Version:

The platform for e-commerce apps

40 lines (39 loc) 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clone = void 0; const tslib_1 = require("tslib"); const fs_extra_1 = require("fs-extra"); const pipe_streams_to_promise_1 = tslib_1.__importDefault(require("pipe-streams-to-promise")); const request_1 = tslib_1.__importDefault(require("request")); const unzip_stream_1 = tslib_1.__importDefault(require("unzip-stream")); const axios_1 = tslib_1.__importDefault(require("axios")); const logger_1 = tslib_1.__importDefault(require("../../api/logger")); const Messages_1 = require("../../lib/constants/Messages"); const urlForRepo = async (repo, org) => { /** * If we have internal templates in the future (in a private org repo) we have to figure out * how to make this request authenticated. But, since toolbelt is open-source and so are the * apps templates, we can afford not to take care of this right now. */ try { const { default_branch: branchName } = (await axios_1.default.get(`https://api.github.com/repos/${org}/${repo}`)).data; return { url: `https://github.com/${org}/${repo}/archive/${branchName}.zip`, branchName }; } catch (err) { logger_1.default.debug(err); throw new Error(Messages_1.Messages.ERROR_COULD_NOT_DETERMINE_DEFAULT_BRANCH(org, repo)); } }; const fetchAndUnzip = async (url, path) => pipe_streams_to_promise_1.default([request_1.default(url), unzip_stream_1.default.Extract({ path })]); exports.clone = async (repo, org, projectFolderName) => { const cwd = process.cwd(); const { url, branchName } = await urlForRepo(repo, org); logger_1.default.debug(Messages_1.Messages.DEBUG_DOWNLOAD_TEMPLATE_URL(url)); const destPath = `${cwd}/${projectFolderName !== null && projectFolderName !== void 0 ? projectFolderName : repo}`; const cloned = `${destPath}/${repo}-${branchName}`; await fs_extra_1.ensureDir(destPath); await fs_extra_1.emptyDir(destPath); await fetchAndUnzip(url, destPath); await fs_extra_1.copy(cloned, destPath); await fs_extra_1.remove(cloned); };