UNPKG

@afuteam-nx/plugin-downloadrepos

Version:

用于下载远程仓库的插件,支持批量下载。

90 lines 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path = require("path"); const child_process_1 = require("child_process"); const fs = require("fs"); function runExecutor(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { console.log('\n@afuteam-nx/plugin-downloadrepos 下载插件开始运行,参数如下\n', options); const { localFileReposWithRemotePath, localFilesDownLoadPath } = options; const successRepositories = []; const failedRepositories = []; function loadFileList() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const data = yield fs.promises.readFile(localFileReposWithRemotePath, 'utf-8'); return JSON.parse(data); }); } function downReopFromGit(repositories) { let currentIndex = 0; // 初始化索引计数器 const allLength = repositories.length; for (const repositoryInfo of repositories) { const { ssh_url_to_repo, name, default_branch } = repositoryInfo; console.log(`处理进度 ${currentIndex + 1}/${allLength}${repositoryInfo.name}(${default_branch})`); // 有 空项目情况 if (!default_branch || !ssh_url_to_repo) { console.log(`跳过 ${name},默认分支: ${default_branch}; ssh_url_to_repo地址是${ssh_url_to_repo}`); failedRepositories.push(repositoryInfo); currentIndex++; continue; } const repositoryPath = path.join(localFilesDownLoadPath, name); if (!fs.existsSync(repositoryPath)) { console.log(`Cloning repository ${ssh_url_to_repo}...`); try { (0, child_process_1.execSync)(`git clone ${ssh_url_to_repo} ${repositoryPath}`); console.log(`Repository ${ssh_url_to_repo} cloned successfully. \n`); successRepositories.push(repositoryInfo); } catch (error) { console.error(`Error cloning repository ${ssh_url_to_repo}:`, error.message); failedRepositories.push(repositoryInfo); continue; } } else { console.log(`Updating repository ${ssh_url_to_repo}...`); try { (0, child_process_1.execSync)(`git -C ${repositoryPath} pull`); // 也可以切换指定tag // execSync(`git -C ${repositoryPath} checkout tags/tag-code-manage-base-line-2024`); // execSync(`git -C ${repositoryPath} describe --tags --exact-match`); console.log(`Repository ${ssh_url_to_repo} updated successfully. \n`); successRepositories.push(repositoryInfo); } catch (error) { console.error(`Error updating repository ${ssh_url_to_repo}:`, error.message); failedRepositories.push(repositoryInfo); continue; } } currentIndex++; } console.log(`共处理了 ${repositories.length} 个项目 \n其中成功 ${successRepositories.length}个;\n异常 ${failedRepositories.length}个\n`); if (failedRepositories.length > 0) { console.log('失败的项目列表:'); failedRepositories.forEach((repositoryInfo) => { console.log(`${repositoryInfo.ssh_url_to_repo}\n`); }); } } function downloadFile() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const allProjects = yield loadFileList(); if (allProjects.length === 0) { console.log('没有项目可以处理'); return false; } console.log(`一共要处理 ${allProjects.length}个项目\n`); downReopFromGit(allProjects); }); } yield downloadFile(); return { success: true, }; }); } exports.default = runExecutor; //# sourceMappingURL=executor.js.map