( plugin: TaskNotesPlugin, input: CliTaskLookupInput )
| 20 | } |
| 21 | |
| 22 | export async function resolveTaskForCli( |
| 23 | plugin: TaskNotesPlugin, |
| 24 | input: CliTaskLookupInput |
| 25 | ): Promise<TaskInfo> { |
| 26 | const path = normalizeLookupValue(input.path); |
| 27 | const title = normalizeLookupValue(input.title); |
| 28 | const query = normalizeLookupValue(input.query); |
| 29 | |
| 30 | if (path) { |
| 31 | const task = await plugin.cacheManager.getTaskInfo(path); |
| 32 | if (!task) { |
| 33 | throw new Error(`Task not found for path: ${path}`); |
| 34 | } |
| 35 | |
| 36 | return task; |
| 37 | } |
| 38 | |
| 39 | const allTasks = await plugin.cacheManager.getAllTasks(); |
| 40 | |
| 41 | if (title) { |
| 42 | const exactMatches = allTasks.filter((task) => task.title === title); |
| 43 | if (exactMatches.length === 1) { |
| 44 | return exactMatches[0]; |
| 45 | } |
| 46 | |
| 47 | if (exactMatches.length > 1) { |
| 48 | throw new Error( |
| 49 | `Multiple tasks matched title "${title}": ${formatAmbiguousTaskMatches(exactMatches)}` |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | throw new Error(`Task not found for title: ${title}`); |
| 54 | } |
| 55 | |
| 56 | if (query) { |
| 57 | const loweredQuery = query.toLowerCase(); |
| 58 | const matches = allTasks.filter((task) => { |
| 59 | return ( |
| 60 | task.title.toLowerCase().includes(loweredQuery) || |
| 61 | task.path.toLowerCase().includes(loweredQuery) |
| 62 | ); |
| 63 | }); |
| 64 | |
| 65 | if (matches.length === 1) { |
| 66 | return matches[0]; |
| 67 | } |
| 68 | |
| 69 | if (matches.length > 1) { |
| 70 | throw new Error( |
| 71 | `Multiple tasks matched query "${query}": ${formatAmbiguousTaskMatches(matches)}` |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | throw new Error(`Task not found for query: ${query}`); |
| 76 | } |
| 77 | |
| 78 | throw new Error("A task reference is required: pass path, title, or query"); |
| 79 | } |
no test coverage detected