(args: string[])
| 38 | } |
| 39 | |
| 40 | export async function handleMemoryCli(args: string[]): Promise<void> { |
| 41 | // Resolve project root: use CODEBASE_ROOT env or cwd (argv[2] is "memory", not a path) |
| 42 | const cliRoot = process.env.CODEBASE_ROOT || process.cwd(); |
| 43 | const memoryPath = path.join(cliRoot, CODEBASE_CONTEXT_DIRNAME, MEMORY_FILENAME); |
| 44 | const subcommand = args[0]; // list | add | remove |
| 45 | const useJson = args.includes('--json'); |
| 46 | |
| 47 | const listUsage = |
| 48 | 'Usage: codebase-context memory list [--category <cat>] [--type <type>] [--query <text>] [--json]'; |
| 49 | const addUsage = |
| 50 | 'Usage: codebase-context memory add --type <type> --category <category> --memory <text> --reason <text> [--scope-kind global|file|symbol] [--scope-file <path>] [--scope-symbol <name>] [--json]'; |
| 51 | const removeUsage = 'Usage: codebase-context memory remove <id> [--json]'; |
| 52 | |
| 53 | const exitWithUsageError = (message: string, usage?: string): never => { |
| 54 | if (useJson) { |
| 55 | console.log( |
| 56 | JSON.stringify( |
| 57 | { |
| 58 | status: 'error', |
| 59 | message, |
| 60 | ...(usage ? { usage } : {}) |
| 61 | }, |
| 62 | null, |
| 63 | 2 |
| 64 | ) |
| 65 | ); |
| 66 | } else { |
| 67 | console.error(message); |
| 68 | if (usage) console.error(usage); |
| 69 | } |
| 70 | process.exit(1); |
| 71 | }; |
| 72 | |
| 73 | if (subcommand === 'list') { |
| 74 | const memories = await readMemoriesFile(memoryPath); |
| 75 | const opts: { category?: CliMemoryCategory; type?: CliMemoryType; query?: string } = {}; |
| 76 | |
| 77 | for (let i = 1; i < args.length; i++) { |
| 78 | if (args[i] === '--category') { |
| 79 | const value = args[i + 1]; |
| 80 | if (!value || value.startsWith('--')) { |
| 81 | exitWithUsageError( |
| 82 | `Error: --category requires a value. Allowed: ${MEMORY_CATEGORIES.join(', ')}`, |
| 83 | listUsage |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | if (isCliMemoryCategory(value)) { |
| 88 | opts.category = value; |
| 89 | } else { |
| 90 | exitWithUsageError( |
| 91 | `Error: invalid --category "${value}". Allowed: ${MEMORY_CATEGORIES.join(', ')}`, |
| 92 | listUsage |
| 93 | ); |
| 94 | } |
| 95 | i++; |
| 96 | } else if (args[i] === '--type') { |
| 97 | const value = args[i + 1]; |
no test coverage detected