@jjdenhertog/ai-driven-development
Version:
AI-driven development workflow with learning capabilities for Claude
70 lines • 4.06 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureWorktree = ensureWorktree;
const fs_extra_1 = require("fs-extra");
const promises_1 = require("node:fs/promises");
const node_path_1 = require("node:path");
const config_1 = require("../../config");
const addCommands_1 = require("../claude/addCommands");
const logger_1 = require("../logger");
const addToGitignore_1 = require("./addToGitignore");
const getGitInstance_1 = require("./getGitInstance");
const getWorktrees_1 = require("./getWorktrees");
function ensureWorktree(options) {
return __awaiter(this, void 0, void 0, function* () {
const { branch, path, skipSymlink = false, skipClaudeCommands = false } = options;
(0, logger_1.log)(`Ensuring worktree for branch '${branch}' at path '${path}'...`, 'info');
const git = (0, getGitInstance_1.getGitInstance)();
const worktrees = yield (0, getWorktrees_1.getWorktrees)();
// Find worktrees related to our branch and path
const branchWorktrees = worktrees.filter(w => w.branch.includes(branch));
const [branchWorktree] = branchWorktrees;
const pathWorktrees = worktrees.filter(w => w.path === path);
const [pathWorktree] = pathWorktrees;
// Check if already correctly linked
if (branchWorktree && pathWorktree && branchWorktree.path === path)
return;
// Check for conflicting states - invalid if:
// 1. Branch is linked to a different path
// 2. Path is linked to a different branch
// 3. Either exists but not both
const invalidWorktree = (branchWorktree || pathWorktree);
if (invalidWorktree)
throw new Error(`Invalid worktree found for branch '${branch}' at path '${path}'. Remove the worktree and try again.`);
// Create the worktree
(0, logger_1.log)(`Creating worktree at ${path}...`, 'info');
yield git.raw(['worktree', 'add', path, branch]);
if (!skipClaudeCommands)
(0, addCommands_1.addCommands)(path);
if (!skipSymlink) {
// Create a symlink to storage branch
const symlinkPath = (0, node_path_1.join)(path, '.aidev-storage');
try {
// Remove existing symlink if it exists
if ((0, fs_extra_1.existsSync)(symlinkPath))
(0, fs_extra_1.rmSync)(symlinkPath, { force: true, recursive: true });
// Calculate relative path from the worktree to the storage
const relativeStoragePath = (0, node_path_1.relative)(path, config_1.STORAGE_PATH);
// Create symlink to storage path using relative path
yield (0, promises_1.symlink)(relativeStoragePath, symlinkPath, 'dir');
(0, logger_1.log)(`Created symlink to storage at ${symlinkPath} -> ${relativeStoragePath}`, 'info');
// Make sure the .aidev-storage is never pushed
(0, addToGitignore_1.addToGitignore)(path, '.aidev-storage', '# AIdev worktree (local data storage)');
}
catch (error) {
(0, logger_1.log)(`Failed to create symlink to storage: ${String(error)}`, 'warn');
}
}
(0, logger_1.log)('Worktree setup completed successfully!', 'success');
});
}
//# sourceMappingURL=ensureWorktree.js.map