1use std::path::Path;
9
10use anyhow::Result;
11
12use crate::ask_json::LibraryEntry;
13use crate::ignore_files::{manage_ignore_files, IgnoreMode};
14use crate::install::{run_install, RunInstallOptions};
15use crate::io::{read_ask_json, write_ask_json};
16use crate::skill::remove_skill;
17use crate::spec::{library_name_from_spec, parse_spec, split_explicit_version, ParsedSpec};
18
19#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum RemoveOutcome {
23 NoAskJson,
25 NoMatch(String),
27 Removed(String),
29}
30
31pub fn run_remove(project_dir: &Path, target: &str) -> Result<RemoveOutcome> {
34 let Some(mut ask_json) = read_ask_json(project_dir)? else {
35 return Ok(RemoveOutcome::NoAskJson);
36 };
37
38 let idx = ask_json
39 .libraries
40 .iter()
41 .position(|entry| entry_matches(entry, target));
42 let Some(idx) = idx else {
43 return Ok(RemoveOutcome::NoMatch(target.to_string()));
44 };
45
46 let removed = ask_json.libraries[idx].spec().to_string();
47 let (spec_body, _) = split_explicit_version(&removed);
48 let lib_name = library_name_from_spec(spec_body);
49 ask_json.libraries.remove(idx);
50 write_ask_json(project_dir, &ask_json)?;
51
52 remove_skill(project_dir, &lib_name)?;
53
54 run_install(project_dir, &RunInstallOptions::default())?;
56
57 let mode = if ask_json.libraries.is_empty() {
58 IgnoreMode::Remove
59 } else {
60 IgnoreMode::Install
61 };
62 manage_ignore_files(project_dir, mode)?;
63
64 Ok(RemoveOutcome::Removed(removed))
65}
66
67fn entry_matches(entry: &LibraryEntry, target: &str) -> bool {
70 let spec = entry.spec();
71 if spec == target {
72 return true;
73 }
74 let (body, _) = split_explicit_version(spec);
75 if library_name_from_spec(body) == target {
76 return true;
77 }
78 matches!(parse_spec(body), ParsedSpec::Npm { pkg, .. } if pkg == target)
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84 use crate::io::get_ask_json_path;
85
86 fn write_ask(dir: &Path, json: &str) {
87 std::fs::write(get_ask_json_path(dir), json).unwrap();
88 }
89
90 #[test]
91 fn no_ask_json_is_reported() {
92 let dir = tempfile::tempdir().unwrap();
93 assert_eq!(
94 run_remove(dir.path(), "react").unwrap(),
95 RemoveOutcome::NoAskJson
96 );
97 }
98
99 #[test]
100 fn no_match_is_reported() {
101 let dir = tempfile::tempdir().unwrap();
102 write_ask(dir.path(), r#"{"libraries":["npm:react"]}"#);
103 assert_eq!(
104 run_remove(dir.path(), "vue").unwrap(),
105 RemoveOutcome::NoMatch("vue".into())
106 );
107 }
108
109 #[test]
110 fn removes_by_npm_package_name() {
111 let dir = tempfile::tempdir().unwrap();
112 write_ask(dir.path(), r#"{"libraries":["npm:react","npm:vue"]}"#);
113 let out = run_remove(dir.path(), "react").unwrap();
114 assert_eq!(out, RemoveOutcome::Removed("npm:react".into()));
115 let aj = read_ask_json(dir.path()).unwrap().unwrap();
117 assert_eq!(aj.libraries.len(), 1);
118 assert_eq!(aj.libraries[0].spec(), "npm:vue");
119 }
120
121 #[test]
122 fn removes_by_exact_spec_and_deletes_skill() {
123 let dir = tempfile::tempdir().unwrap();
124 write_ask(
125 dir.path(),
126 r#"{"libraries":["github:vercel/next.js@v15.0.3"]}"#,
127 );
128 run_install(dir.path(), &RunInstallOptions::default()).unwrap();
130 let skill = crate::skill::get_skill_dir(dir.path(), "next.js").join("SKILL.md");
131 assert!(skill.exists());
132
133 let out = run_remove(dir.path(), "github:vercel/next.js@v15.0.3").unwrap();
134 assert_eq!(
135 out,
136 RemoveOutcome::Removed("github:vercel/next.js@v15.0.3".into())
137 );
138 assert!(!skill.exists());
139 let agents = std::fs::read_to_string(dir.path().join("AGENTS.md")).unwrap_or_default();
141 assert!(!agents.contains("BEGIN:ask-docs"));
142 }
143
144 #[test]
145 fn removes_by_library_slug() {
146 let dir = tempfile::tempdir().unwrap();
147 write_ask(
148 dir.path(),
149 r#"{"libraries":["github:vercel/next.js@v15.0.3"]}"#,
150 );
151 let out = run_remove(dir.path(), "next.js").unwrap();
153 assert_eq!(
154 out,
155 RemoveOutcome::Removed("github:vercel/next.js@v15.0.3".into())
156 );
157 }
158}