UNPKG

@jjdenhertog/ai-driven-development

Version:

AI-driven development workflow with learning capabilities for Claude

139 lines 8.19 kB
"use strict"; 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.initCommand = initCommand; const fs_extra_1 = require("fs-extra"); const node_path_1 = require("node:path"); const config_1 = require("../config"); const checkGitInitialized_1 = require("../utils/git/checkGitInitialized"); const ensureOrphanBranch_1 = require("../utils/git/ensureOrphanBranch"); const ensureWorktree_1 = require("../utils/git/ensureWorktree"); const isInWorktree_1 = require("../utils/git/isInWorktree"); /* eslint-disable unicorn/prefer-module */ /* eslint-disable @typescript-eslint/restrict-template-expressions */ const logger_1 = require("../utils/logger"); function initCommand(options) { return __awaiter(this, void 0, void 0, function* () { const { force } = options; try { (0, logger_1.log)('Initializing aidev in current directory...', 'info'); // Check if git is initialized if (!(yield (0, checkGitInitialized_1.checkGitInitialized)())) { (0, logger_1.log)('Git repository not initialized. Please run "git init" first.', 'error'); throw new Error('Git repository not initialized'); } if (yield (0, isInWorktree_1.isInWorktree)()) throw new Error('You are in a worktree. Please exit the work tree folder and try again.'); // Create aidev-storage worktree yield (0, ensureOrphanBranch_1.ensureOrphanBranch)(config_1.STORAGE_BRANCH); yield (0, ensureWorktree_1.ensureWorktree)(config_1.STORAGE_BRANCH, config_1.STORAGE_PATH); // Create .aidev directory if it doesn't exist if (!(0, fs_extra_1.existsSync)(config_1.STORAGE_PATH)) throw new Error(`${config_1.STORAGE_PATH} directory not found.`); ///////////////////////////////////////////// // // SETUP CONFIG FILE // - Create .aidev.json // ///////////////////////////////////////////// if (!(0, fs_extra_1.existsSync)(config_1.CONFIG_PATH)) { (0, fs_extra_1.writeFileSync)(config_1.CONFIG_PATH, JSON.stringify({ branchStartingPoint: 'main', mainBranch: 'main', storageBranch: 'aidev-storage' }, null, 2)); (0, logger_1.log)('Config file created', 'success'); } ///////////////////////////////////////////// // // SETUP STORAGE // - Ensure needed folders // - Copy files // ///////////////////////////////////////////// // The templates folder is at the root of the package (../../templates from dist/commands/) const packageRoot = (0, node_path_1.join)(__dirname, '..', '..', '..'); const templatesRoot = (0, node_path_1.join)(packageRoot, 'templates'); // Create subdirectories const subdirs = ['tasks', 'concept', 'examples', 'preferences', 'templates']; for (const subdir of subdirs) { const subdirPath = (0, node_path_1.join)(config_1.STORAGE_PATH, subdir); (0, fs_extra_1.ensureDirSync)(subdirPath); } // Copy examples folder const examplesSourceDir = (0, node_path_1.join)(templatesRoot, 'examples'); const examplesTargetDir = (0, node_path_1.join)(config_1.STORAGE_PATH, 'examples'); (0, fs_extra_1.copySync)(examplesSourceDir, examplesTargetDir, { overwrite: true }); (0, logger_1.log)('Copied examples folder', 'success'); // Copy preferences folder const preferencesSourceDir = (0, node_path_1.join)(templatesRoot, 'preferences'); const preferencesTargetDir = (0, node_path_1.join)(config_1.STORAGE_PATH, 'preferences'); (0, fs_extra_1.copySync)(preferencesSourceDir, preferencesTargetDir, { overwrite: true }); (0, logger_1.log)('Copied preferences folder', 'success'); // Copy templates folder const templatesSourceDir = (0, node_path_1.join)(templatesRoot, 'templates'); const templatesTargetDir = (0, node_path_1.join)(config_1.STORAGE_PATH, 'templates'); (0, fs_extra_1.copySync)(templatesSourceDir, templatesTargetDir, { overwrite: true }); (0, logger_1.log)('Copied templates folder', 'success'); ///////////////////////////////////////////// // // SETUP CLAUDE FILES // - Copy CLAUDE.md // - Copy .devcontainer // - Copy .claude/commands // ///////////////////////////////////////////// // Copy CLAUDE.md to root directory const claudeMdSource = (0, node_path_1.join)(templatesRoot, 'CLAUDE.md'); const claudeMdTarget = (0, node_path_1.join)(process.cwd(), 'CLAUDE.md'); if ((0, fs_extra_1.existsSync)(claudeMdTarget) && !force) { (0, logger_1.log)('CLAUDE.md already exists in root directory. Not overwriting, run --force to overwrite', 'warn'); } else { (0, fs_extra_1.copySync)(claudeMdSource, claudeMdTarget, { overwrite: true }); (0, logger_1.log)('Copied CLAUDE.md to root directory', 'success'); } // Copy .devcontainer to root directory const devcontainerSource = (0, node_path_1.join)(templatesRoot, 'devcontainer'); const devcontainerTarget = (0, node_path_1.join)(process.cwd(), '.devcontainer'); if ((0, fs_extra_1.existsSync)(devcontainerTarget) && !force) { (0, logger_1.log)('.devcontainer already exists in root directory. Not overwriting, run --force to overwrite', 'warn'); } else { (0, fs_extra_1.copySync)(devcontainerSource, devcontainerTarget, { overwrite: true }); (0, logger_1.log)('Copied .devcontainer to root directory', 'success'); } // Create .claude directory if it doesn't exist const claudeDir = (0, node_path_1.join)(process.cwd(), '.claude'); (0, fs_extra_1.ensureDirSync)(claudeDir); const claudeCommandsDir = (0, node_path_1.join)(claudeDir, 'commands'); (0, fs_extra_1.ensureDirSync)(claudeCommandsDir); // Copy command files one by one to .claude/commands const commandsSourceDir = (0, node_path_1.join)(templatesRoot, 'commands'); if ((0, fs_extra_1.existsSync)(commandsSourceDir)) { const commandFiles = (0, fs_extra_1.readdirSync)(commandsSourceDir); for (const file of commandFiles) { const sourceFile = (0, node_path_1.join)(commandsSourceDir, file); const targetFile = (0, node_path_1.join)(claudeCommandsDir, file); // Only copy if target doesn't exist (to preserve custom commands) (0, fs_extra_1.copySync)(sourceFile, targetFile, { overwrite: true }); (0, logger_1.log)(`Copied command file: ${file}`, 'success'); } } } catch (error) { (0, fs_extra_1.removeSync)(config_1.STORAGE_PATH); (0, logger_1.log)(`Failed to initialize aidev: ${error}`, 'error'); throw error; } }); } //# sourceMappingURL=initCommand.js.map