UNPKG

sync-worktrees

Version:

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

195 lines • 7.71 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.promptForConfig = promptForConfig; const path = __importStar(require("path")); const prompts_1 = require("@inquirer/prompts"); const config_generator_1 = require("./config-generator"); const git_url_1 = require("./git-url"); async function promptForConfig(partialConfig) { console.log("šŸ”§ Welcome to sync-worktrees interactive setup!\n"); let repoUrl = partialConfig.repoUrl; if (!repoUrl) { repoUrl = await (0, prompts_1.input)({ message: "Enter the Git repository URL (e.g., https://github.com/user/repo.git):", validate: (value) => { if (!value.trim()) { return "Repository URL is required"; } try { // Basic URL validation if (!value.match(/^(https?:\/\/|ssh:\/\/|git@|file:\/\/).*$/)) { return "Please enter a valid Git URL (https://, ssh://, git@, or file://)"; } return true; } catch { return "Please enter a valid URL"; } }, }); } let worktreeDir = partialConfig.worktreeDir; if (!worktreeDir) { // Extract repository name from URL to suggest as default const repoName = repoUrl ? (0, git_url_1.extractRepoNameFromUrl)(repoUrl) : ""; const defaultWorktreeDir = repoName ? `./${repoName}` : ""; worktreeDir = await (0, prompts_1.input)({ message: "Enter the directory for storing worktrees:", default: defaultWorktreeDir, validate: (value) => { // Allow empty input to use default value if (!value.trim() && !defaultWorktreeDir) { return "Worktree directory is required"; } return true; }, }); // Use default if empty input if (!worktreeDir.trim() && defaultWorktreeDir) { worktreeDir = defaultWorktreeDir; } if (!path.isAbsolute(worktreeDir)) { worktreeDir = path.resolve(worktreeDir); } } let bareRepoDir = partialConfig.bareRepoDir; const askForBareDir = await (0, prompts_1.confirm)({ message: "Would you like to specify a custom location for the bare repository?", default: false, }); if (askForBareDir) { bareRepoDir = await (0, prompts_1.input)({ message: "Enter the directory for the bare repository:", default: "", validate: (value) => { if (!value.trim()) { return "Bare repository directory is required"; } return true; }, }); if (!path.isAbsolute(bareRepoDir)) { bareRepoDir = path.resolve(bareRepoDir); } } let runOnce = partialConfig.runOnce; let cronSchedule = partialConfig.cronSchedule || "0 * * * *"; if (runOnce === undefined) { const runMode = await (0, prompts_1.select)({ message: "How would you like to run the sync?", choices: [ { name: "Run once", value: "once" }, { name: "Schedule with cron", value: "scheduled" }, ], }); runOnce = runMode === "once"; if (!runOnce && !partialConfig.cronSchedule) { cronSchedule = await (0, prompts_1.input)({ message: "Enter the cron schedule (or press enter for default):", default: "0 * * * *", validate: (value) => { if (!value.trim()) { return "Cron schedule is required"; } const parts = value.trim().split(" "); if (parts.length < 5) { return "Invalid cron pattern. Expected format: '* * * * *'"; } return true; }, }); } } const finalConfig = { repoUrl, worktreeDir, cronSchedule, runOnce: runOnce || false, bareRepoDir, }; console.log("\nšŸ“‹ Configuration summary:"); console.log(` Repository URL: ${finalConfig.repoUrl}`); console.log(` Worktrees: ${finalConfig.worktreeDir}`); if (finalConfig.bareRepoDir) { console.log(` Bare repo: ${finalConfig.bareRepoDir}`); } else { console.log(` Bare repo: .bare/<repo-name> (default)`); } if (finalConfig.runOnce) { console.log(` Mode: Run once`); } else { console.log(` Mode: Scheduled (${finalConfig.cronSchedule})`); } console.log(""); // Ask if user wants to save configuration to a file const saveConfig = await (0, prompts_1.confirm)({ message: "Would you like to save this configuration to a file for future use?", default: true, }); if (saveConfig) { const defaultConfigPath = (0, config_generator_1.getDefaultConfigPath)(); let configPath = await (0, prompts_1.input)({ message: "Enter the path for the config file:", default: defaultConfigPath, validate: (value) => { if (!value.trim()) { return "Config file path is required"; } if (!value.endsWith(".js")) { return "Config file must have a .js extension"; } return true; }, }); if (!path.isAbsolute(configPath)) { configPath = path.resolve(configPath); } try { await (0, config_generator_1.generateConfigFile)(finalConfig, configPath); console.log(`\nāœ… Configuration saved to: ${configPath}`); console.log(`\nšŸ’” You can now use this config file with:`); console.log(` sync-worktrees --config ${path.relative(process.cwd(), configPath)}`); console.log(""); } catch (error) { console.error(`\nāŒ Failed to save config file: ${error.message}`); } } return finalConfig; } //# sourceMappingURL=interactive.js.map