UNPKG

gitly

Version:

An API to download and/or extract git repositories

60 lines (59 loc) 1.98 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = download; const shelljs_1 = require("shelljs"); const archive_1 = require("./archive"); const execute_1 = __importDefault(require("./execute")); const exists_1 = __importDefault(require("./exists")); const fetch_1 = __importDefault(require("./fetch")); const offline_1 = require("./offline"); const parse_1 = __importDefault(require("./parse")); /** * Download the tar file from the repository * and store it in a temporary directory * @param repository The repository to download * * @example * ```js * // .. * const path = await download('iwatakeshi/git-copy') * // ... * ``` */ async function download(repository, options = {}) { const info = (0, parse_1.default)(repository, options); const archivePath = (0, archive_1.getArchivePath)(info, options); const url = (0, archive_1.getArchiveUrl)(info, options); const local = async () => (0, exists_1.default)(archivePath); const remote = async () => { // If the repository is cached, remove the old cache if (await (0, exists_1.default)(archivePath)) { /* istanbul ignore next */ (0, shelljs_1.rm)(archivePath); } return (0, fetch_1.default)(url, archivePath, options); }; let order = [local, remote]; if ((await (0, offline_1.isOffline)()) || options.cache) { order = [local]; } else if (options.force || ['master', 'main'].includes(info.type)) { order = [remote, local]; } try { const result = await (0, execute_1.default)(order); if (typeof result === 'boolean') { return archivePath; } return result; } catch (error) { if (options.throw) { throw error; } } return ''; }