1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat(uninstall): alias to 'deno remove' if -g flag missing (#25461)

Close https://github.com/denoland/deno/issues/25457
This commit is contained in:
Bartek Iwańczuk 2024-09-05 12:51:37 +01:00 committed by GitHub
parent 7d95c5c062
commit 5319b85f14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 96 additions and 54 deletions

View file

@ -253,7 +253,6 @@ pub enum InstallFlagsLocal {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct InstallFlags { pub struct InstallFlags {
pub global: bool,
pub kind: InstallKind, pub kind: InstallKind,
} }
@ -277,14 +276,12 @@ pub struct UninstallFlagsGlobal {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum UninstallKind { pub enum UninstallKind {
#[allow(unused)] Local(RemoveFlags),
Local,
Global(UninstallFlagsGlobal), Global(UninstallFlagsGlobal),
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct UninstallFlags { pub struct UninstallFlags {
pub global: bool,
pub kind: UninstallKind, pub kind: UninstallKind,
} }
@ -2493,11 +2490,12 @@ fn jupyter_subcommand() -> Command {
fn uninstall_subcommand() -> Command { fn uninstall_subcommand() -> Command {
command( command(
"uninstall", "uninstall",
cstr!("Uninstalls an executable script in the installation root's bin directory. cstr!("Uninstalls a dependency or an executable script in the installation root's bin directory.
<p(245)>deno uninstall serve</> <p(245)>deno uninstall @std/dotenv chalk</>
<p(245)>deno uninstall --global file_server</>
To change the installation root, use <c>--root</> flag: To change the installation root, use <c>--root</> flag:
<p(245)>deno uninstall --root /usr/local serve</> <p(245)>deno uninstall --global --root /usr/local serve</>
The installation root is determined, in order of precedence: The installation root is determined, in order of precedence:
- <p(245)>--root</> option - <p(245)>--root</> option
@ -2507,11 +2505,12 @@ The installation root is determined, in order of precedence:
) )
.defer(|cmd| { .defer(|cmd| {
cmd cmd
.arg(Arg::new("name").required_unless_present("help")) .arg(Arg::new("name-or-package").required_unless_present("help"))
.arg( .arg(
Arg::new("root") Arg::new("root")
.long("root") .long("root")
.help("Installation root") .help("Installation root")
.requires("global")
.value_hint(ValueHint::DirPath), .value_hint(ValueHint::DirPath),
) )
.arg( .arg(
@ -2521,6 +2520,13 @@ The installation root is determined, in order of precedence:
.help("Remove globally installed package or module") .help("Remove globally installed package or module")
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg(
Arg::new("additional-packages")
.help("List of additional packages to remove")
.conflicts_with("global")
.num_args(1..)
.action(ArgAction::Append)
)
}) })
} }
@ -4430,8 +4436,6 @@ fn install_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let args = cmd_values.collect(); let args = cmd_values.collect();
flags.subcommand = DenoSubcommand::Install(InstallFlags { flags.subcommand = DenoSubcommand::Install(InstallFlags {
// TODO(bartlomieju): remove for 2.0
global,
kind: InstallKind::Global(InstallFlagsGlobal { kind: InstallKind::Global(InstallFlagsGlobal {
name, name,
module_url, module_url,
@ -4448,7 +4452,6 @@ fn install_parse(flags: &mut Flags, matches: &mut ArgMatches) {
if matches.get_flag("entrypoint") { if matches.get_flag("entrypoint") {
let entrypoints = matches.remove_many::<String>("cmd").unwrap_or_default(); let entrypoints = matches.remove_many::<String>("cmd").unwrap_or_default();
flags.subcommand = DenoSubcommand::Install(InstallFlags { flags.subcommand = DenoSubcommand::Install(InstallFlags {
global,
kind: InstallKind::Local(InstallFlagsLocal::Entrypoints( kind: InstallKind::Local(InstallFlagsLocal::Entrypoints(
entrypoints.collect(), entrypoints.collect(),
)), )),
@ -4458,12 +4461,10 @@ fn install_parse(flags: &mut Flags, matches: &mut ArgMatches) {
.map(|packages| add_parse_inner(matches, Some(packages))) .map(|packages| add_parse_inner(matches, Some(packages)))
{ {
flags.subcommand = DenoSubcommand::Install(InstallFlags { flags.subcommand = DenoSubcommand::Install(InstallFlags {
global,
kind: InstallKind::Local(InstallFlagsLocal::Add(add_files)), kind: InstallKind::Local(InstallFlagsLocal::Add(add_files)),
}) })
} else { } else {
flags.subcommand = DenoSubcommand::Install(InstallFlags { flags.subcommand = DenoSubcommand::Install(InstallFlags {
global,
kind: InstallKind::Local(InstallFlagsLocal::TopLevel), kind: InstallKind::Local(InstallFlagsLocal::TopLevel),
}); });
} }
@ -4554,15 +4555,24 @@ fn jupyter_parse(flags: &mut Flags, matches: &mut ArgMatches) {
} }
fn uninstall_parse(flags: &mut Flags, matches: &mut ArgMatches) { fn uninstall_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let name = matches.remove_one::<String>("name-or-package").unwrap();
let kind = if matches.get_flag("global") {
let root = matches.remove_one::<String>("root"); let root = matches.remove_one::<String>("root");
let global = matches.get_flag("global"); UninstallKind::Global(UninstallFlagsGlobal { name, root })
let name = matches.remove_one::<String>("name").unwrap(); } else {
flags.subcommand = DenoSubcommand::Uninstall(UninstallFlags { let packages: Vec<_> = vec![name]
// TODO(bartlomieju): remove once `deno uninstall` supports both local and .into_iter()
// global installs .chain(
global, matches
kind: UninstallKind::Global(UninstallFlagsGlobal { name, root }), .remove_many::<String>("additional-packages")
}); .unwrap_or_default(),
)
.collect();
UninstallKind::Local(RemoveFlags { packages })
};
flags.subcommand = DenoSubcommand::Uninstall(UninstallFlags { kind });
} }
fn lsp_parse(flags: &mut Flags, _matches: &mut ArgMatches) { fn lsp_parse(flags: &mut Flags, _matches: &mut ArgMatches) {
@ -7896,7 +7906,6 @@ mod tests {
root: None, root: None,
force: false, force: false,
}), }),
global: true,
}), }),
..Flags::default() ..Flags::default()
} }
@ -7919,7 +7928,6 @@ mod tests {
root: None, root: None,
force: false, force: false,
}), }),
global: true,
}), }),
..Flags::default() ..Flags::default()
} }
@ -7941,7 +7949,6 @@ mod tests {
root: Some("/foo".to_string()), root: Some("/foo".to_string()),
force: true, force: true,
}), }),
global: true,
}), }),
import_map_path: Some("import_map.json".to_string()), import_map_path: Some("import_map.json".to_string()),
no_remote: true, no_remote: true,
@ -7968,16 +7975,31 @@ mod tests {
#[test] #[test]
fn uninstall() { fn uninstall() {
let r = flags_from_vec(svec!["deno", "uninstall", "file_server"]); let r = flags_from_vec(svec!["deno", "uninstall"]);
assert!(r.is_err(),);
let r = flags_from_vec(svec!["deno", "uninstall", "@std/load"]);
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Uninstall(UninstallFlags { subcommand: DenoSubcommand::Uninstall(UninstallFlags {
kind: UninstallKind::Global(UninstallFlagsGlobal { kind: UninstallKind::Local(RemoveFlags {
name: "file_server".to_string(), packages: vec!["@std/load".to_string()],
root: None, }),
}),
..Flags::default()
}
);
let r =
flags_from_vec(svec!["deno", "uninstall", "file_server", "@std/load"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Uninstall(UninstallFlags {
kind: UninstallKind::Local(RemoveFlags {
packages: vec!["file_server".to_string(), "@std/load".to_string()],
}), }),
global: false,
}), }),
..Flags::default() ..Flags::default()
} }
@ -7992,7 +8014,27 @@ mod tests {
name: "file_server".to_string(), name: "file_server".to_string(),
root: None, root: None,
}), }),
global: true, }),
..Flags::default()
}
);
let r = flags_from_vec(svec![
"deno",
"uninstall",
"-g",
"--root",
"/user/foo/bar",
"file_server"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Uninstall(UninstallFlags {
kind: UninstallKind::Global(UninstallFlagsGlobal {
name: "file_server".to_string(),
root: Some("/user/foo/bar".to_string()),
}),
}), }),
..Flags::default() ..Flags::default()
} }

View file

@ -161,7 +161,7 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::jupyter::kernel(flags, jupyter_flags).await tools::jupyter::kernel(flags, jupyter_flags).await
}), }),
DenoSubcommand::Uninstall(uninstall_flags) => spawn_subcommand(async { DenoSubcommand::Uninstall(uninstall_flags) => spawn_subcommand(async {
tools::installer::uninstall(uninstall_flags) tools::installer::uninstall(flags, uninstall_flags).await
}), }),
DenoSubcommand::Lsp => spawn_subcommand(async { lsp::start().await }), DenoSubcommand::Lsp => spawn_subcommand(async { lsp::start().await }),
DenoSubcommand::Lint(lint_flags) => spawn_subcommand(async { DenoSubcommand::Lint(lint_flags) => spawn_subcommand(async {

View file

@ -198,14 +198,15 @@ pub async fn infer_name_from_url(
Some(stem.to_string()) Some(stem.to_string())
} }
pub fn uninstall(uninstall_flags: UninstallFlags) -> Result<(), AnyError> { pub async fn uninstall(
if !uninstall_flags.global { flags: Arc<Flags>,
log::warn!("⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag."); uninstall_flags: UninstallFlags,
} ) -> Result<(), AnyError> {
let uninstall_flags = match uninstall_flags.kind { let uninstall_flags = match uninstall_flags.kind {
UninstallKind::Global(flags) => flags, UninstallKind::Global(flags) => flags,
UninstallKind::Local => unreachable!(), UninstallKind::Local(remove_flags) => {
return super::registry::remove(flags, remove_flags).await;
}
}; };
let cwd = std::env::current_dir().context("Unable to get CWD")?; let cwd = std::env::current_dir().context("Unable to get CWD")?;
@ -332,10 +333,6 @@ pub async fn install_command(
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
match install_flags.kind { match install_flags.kind {
InstallKind::Global(global_flags) => { InstallKind::Global(global_flags) => {
if !install_flags.global {
log::warn!("⚠️ `deno install` behavior will change in Deno 2. To preserve the current behavior use the `-g` or `--global` flag.");
}
install_global(flags, global_flags).await install_global(flags, global_flags).await
} }
InstallKind::Local(local_flags) => { InstallKind::Local(local_flags) => {
@ -1512,8 +1509,8 @@ mod tests {
assert!(content.contains(&expected_string)); assert!(content.contains(&expected_string));
} }
#[test] #[tokio::test]
fn uninstall_basic() { async fn uninstall_basic() {
let temp_dir = TempDir::new(); let temp_dir = TempDir::new();
let bin_dir = temp_dir.path().join("bin"); let bin_dir = temp_dir.path().join("bin");
std::fs::create_dir(&bin_dir).unwrap(); std::fs::create_dir(&bin_dir).unwrap();
@ -1540,13 +1537,16 @@ mod tests {
File::create(file_path).unwrap(); File::create(file_path).unwrap();
} }
uninstall(UninstallFlags { uninstall(
Default::default(),
UninstallFlags {
kind: UninstallKind::Global(UninstallFlagsGlobal { kind: UninstallKind::Global(UninstallFlagsGlobal {
name: "echo_test".to_string(), name: "echo_test".to_string(),
root: Some(temp_dir.path().to_string()), root: Some(temp_dir.path().to_string()),
}), }),
global: false, },
}) )
.await
.unwrap(); .unwrap();
assert!(!file_path.exists()); assert!(!file_path.exists());

View file

@ -62,7 +62,7 @@ fn install_basic() {
// now uninstall // now uninstall
context context
.new_command() .new_command()
.args("uninstall echo_test") .args("uninstall -g echo_test")
.envs([ .envs([
("HOME", temp_dir_str.as_str()), ("HOME", temp_dir_str.as_str()),
("USERPROFILE", temp_dir_str.as_str()), ("USERPROFILE", temp_dir_str.as_str()),
@ -139,7 +139,7 @@ fn install_basic_global() {
// now uninstall // now uninstall
context context
.new_command() .new_command()
.args("uninstall echo_test") .args("uninstall -g echo_test")
.envs([ .envs([
("HOME", temp_dir_str.as_str()), ("HOME", temp_dir_str.as_str()),
("USERPROFILE", temp_dir_str.as_str()), ("USERPROFILE", temp_dir_str.as_str()),
@ -274,7 +274,7 @@ fn installer_test_remote_module_run() {
// now uninstall with the relative path // now uninstall with the relative path
context context
.new_command() .new_command()
.args("uninstall --root ./root echo_test") .args("uninstall -g --root ./root echo_test")
.run() .run()
.skip_output_check() .skip_output_check()
.assert_exit_code(0); .assert_exit_code(0);