UNPKG

repoweaver

Version:

A GitHub App that skillfully weaves multiple templates together to create and update repositories with intelligent merge strategies

172 lines 6.22 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateManager = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const simple_git_1 = require("simple-git"); class TemplateManager { constructor() { this.git = (0, simple_git_1.simpleGit)(); this.tempDir = path.join(process.cwd(), '.repoweaver-temp'); } async fetchTemplate(template) { const templateDir = path.join(this.tempDir, this.sanitizeName(template.name)); await this.ensureDirectoryExists(this.tempDir); await this.cleanDirectory(templateDir); try { await this.git.clone(template.url, templateDir, { '--branch': template.branch || 'main', '--depth': '1', }); if (template.subDirectory) { const subPath = path.join(templateDir, template.subDirectory); const exists = await this.pathExists(subPath); if (!exists) { throw new Error(`Subdirectory '${template.subDirectory}' not found in template`); } return subPath; } return templateDir; } catch (error) { throw new Error(`Failed to fetch template ${template.name}: ${error}`); } } async copyTemplateFiles(sourcePath, targetPath, excludePatterns = []) { const result = { success: true, template: { url: '', name: path.basename(sourcePath) }, filesProcessed: 0, errors: [], }; try { await this.ensureDirectoryExists(targetPath); result.filesProcessed = await this.copyRecursive(sourcePath, targetPath, excludePatterns); } catch (error) { result.success = false; result.errors.push(`Copy failed: ${error}`); } return result; } async processTemplate(template, targetPath, excludePatterns = []) { const result = { success: true, template, filesProcessed: 0, errors: [], }; try { const templatePath = await this.fetchTemplate(template); const copyResult = await this.copyTemplateFiles(templatePath, targetPath, ['.git/**', '.git', ...excludePatterns]); result.filesProcessed = copyResult.filesProcessed; result.errors = copyResult.errors; result.success = copyResult.success; } catch (error) { result.success = false; result.errors.push(`Template processing failed: ${error}`); } return result; } async cleanup() { try { await this.cleanDirectory(this.tempDir); } catch (error) { console.warn(`Failed to cleanup temp directory: ${error}`); } } async copyRecursive(source, target, excludePatterns) { let filesProcessed = 0; const items = await fs.readdir(source); for (const item of items) { const sourcePath = path.join(source, item); const targetPath = path.join(target, item); if (this.shouldExclude(sourcePath, excludePatterns)) { continue; } const stat = await fs.stat(sourcePath); if (stat.isDirectory()) { await this.ensureDirectoryExists(targetPath); filesProcessed += await this.copyRecursive(sourcePath, targetPath, excludePatterns); } else { await fs.copyFile(sourcePath, targetPath); filesProcessed++; } } return filesProcessed; } shouldExclude(filePath, excludePatterns) { return excludePatterns.some((pattern) => { const regex = new RegExp(pattern.replace(/\*\*/g, '.*').replace(/\*/g, '[^/]*')); return regex.test(filePath); }); } async ensureDirectoryExists(dirPath) { try { await fs.mkdir(dirPath, { recursive: true }); } catch (error) { if (error.code !== 'EEXIST') { throw error; } } } async cleanDirectory(dirPath) { try { await fs.rm(dirPath, { recursive: true, force: true }); } catch (error) { // Directory might not exist, which is fine } } async pathExists(filePath) { try { await fs.access(filePath); return true; } catch { return false; } } sanitizeName(name) { return name.replace(/[^a-zA-Z0-9-_]/g, '_'); } } exports.TemplateManager = TemplateManager; //# sourceMappingURL=template-manager.js.map