UNPKG

sync-worktrees

Version:

Automatically synchronize Git worktrees with remote branches - perfect for multi-branch development workflows

112 lines (111 loc) 4.43 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.generateConfigFile = generateConfigFile; exports.getDefaultConfigPath = getDefaultConfigPath; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const git_url_1 = require("./git-url"); /** * Serializes a JavaScript object to a clean module.exports format */ function serializeToModuleExports(obj, indent = 0) { const spaces = " ".repeat(indent); const innerSpaces = " ".repeat(indent + 2); if (typeof obj === "string") { return `"${obj}"`; } if (typeof obj === "number" || typeof obj === "boolean") { return String(obj); } if (Array.isArray(obj)) { if (obj.length === 0) return "[]"; const items = obj.map((item) => `${innerSpaces}${serializeToModuleExports(item, indent + 2)}`).join(",\n"); return `[\n${items}\n${spaces}]`; } if (obj && typeof obj === "object") { const entries = Object.entries(obj) .filter(([_, value]) => value !== undefined) .map(([key, value]) => { const serializedValue = serializeToModuleExports(value, indent + 2); return `${innerSpaces}${key}: ${serializedValue}`; }); if (entries.length === 0) return "{}"; return `{\n${entries.join(",\n")}\n${spaces}}`; } return String(obj); } async function generateConfigFile(config, configPath) { const configDir = path.dirname(configPath); await fs.mkdir(configDir, { recursive: true }); // Calculate relative paths from config file location const worktreeDirRelative = path.relative(configDir, config.worktreeDir); const useRelativeWorktree = !worktreeDirRelative.startsWith("../../../"); const repoName = (0, git_url_1.extractRepoNameFromUrl)(config.repoUrl); // Build the repository object const repository = { name: repoName, repoUrl: config.repoUrl, worktreeDir: useRelativeWorktree ? `./${worktreeDirRelative}` : config.worktreeDir, }; // Add bareRepoDir if provided if (config.bareRepoDir) { const bareRepoDirRelative = path.relative(configDir, config.bareRepoDir); const useRelativeBare = !bareRepoDirRelative.startsWith("../../../"); repository.bareRepoDir = useRelativeBare ? `./${bareRepoDirRelative}` : config.bareRepoDir; } // Build the complete config object const configObject = { defaults: { cronSchedule: config.cronSchedule, runOnce: config.runOnce, }, repositories: [repository], }; // Generate the config file content const configContent = `/** * Sync-worktrees configuration file * Generated on ${new Date().toISOString()} */ module.exports = ${serializeToModuleExports(configObject)}; `; await fs.writeFile(configPath, configContent, "utf-8"); } function getDefaultConfigPath() { return path.join(process.cwd(), "sync-worktrees.config.js"); } //# sourceMappingURL=config-generator.js.map