worktree-tool
Version:
A command-line tool for managing Git worktrees with integrated tmux/shell session management
54 lines • 2.26 kB
JavaScript
import { isCI } from "../platform/detector.js";
import { WorktreeToolError } from "../utils/errors.js";
export function parseExecCommand(args, config, options) {
// Default to exit mode in CI environments to avoid terminal emulator issues
const defaultMode = isCI() ? "exit" : "window";
// Find the -- separator
const separatorIndex = args.indexOf("--");
if (separatorIndex === -1) {
// No separator, must be predefined command
const commandName = args[0];
if (!commandName) {
throw new WorktreeToolError("No command specified", "Usage: wtt exec <command> or wtt exec -- <command>");
}
// Validate commands exist
if (!config.commands || Object.keys(config.commands).length === 0) {
throw new WorktreeToolError("No commands configured", "Add commands to .worktree-config.json under the \"commands\" key");
}
// Check if command exists
const commandConfig = config.commands[commandName];
if (!commandConfig) {
const available = Object.keys(config.commands).join(", ");
throw new WorktreeToolError(`Command "${commandName}" not found in config`, `Available commands: ${available}`);
}
// Parse predefined command
if (typeof commandConfig === "string") {
return {
type: "predefined",
command: commandConfig,
args: args.slice(1),
mode: options.mode ?? defaultMode,
commandName: commandName,
};
}
return {
type: "predefined",
command: commandConfig.command,
args: args.slice(1),
mode: options.mode ?? commandConfig.mode ?? defaultMode,
commandName: commandName,
};
}
// Has separator, inline command
const inlineArgs = args.slice(separatorIndex + 1);
if (inlineArgs.length === 0) {
throw new WorktreeToolError("No command specified after --", "Usage: wtt exec -- <command> [args...]");
}
return {
type: "inline",
command: inlineArgs[0] ?? "",
args: inlineArgs.slice(1),
mode: options.mode ?? "window",
};
}
//# sourceMappingURL=parser.js.map