Browse Source

Add `uninstall` command (#98)

Adds an `uninstall` command to uninstall node versions 👏
remotes/origin/add-simple-redirecting-site
Corentin Leruth 6 years ago committed by Gal Schlezinger
parent
commit
d15139839e
  1. 22
      executable/FnmApp.re
  2. 43
      executable/Uninstall.re
  3. 21
      library/Fs.re
  4. 2
      library/Versions.re

22
executable/FnmApp.re

@ -6,6 +6,7 @@ module Commands = { @@ -6,6 +6,7 @@ module Commands = {
let listRemote = () => Lwt_main.run(ListRemote.run());
let listLocal = () => Lwt_main.run(ListLocal.run());
let install = version => Lwt_main.run(Install.run(~version));
let uninstall = version => Lwt_main.run(Uninstall.run(~version));
let env =
(
isFishShell,
@ -74,6 +75,25 @@ let install = { @@ -74,6 +75,25 @@ let install = {
);
};
let uninstall = {
let doc = "Uninstall a node version";
let man = [];
let selectedVersion = {
let doc = "Uninstall the node version specified in $(docv).";
Arg.(
required
& pos(0, some(string), None)
& info([], ~docv="VERSION", ~doc)
);
};
(
Term.(const(Commands.uninstall) $ selectedVersion),
Term.info("uninstall", ~version, ~doc, ~exits=Term.default_exits, ~man),
);
};
let listLocal = {
let doc = "List all the installed versions";
let man = [];
@ -248,6 +268,6 @@ let defaultCmd = { @@ -248,6 +268,6 @@ let defaultCmd = {
let _ =
Term.eval_choice(
defaultCmd,
[install, use, alias, listLocal, listRemote, env],
[install, uninstall, use, alias, listLocal, listRemote, env],
)
|> Term.exit;

43
executable/Uninstall.re

@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
open Fnm;
open Lwt;
let run = (~version) => {
Versions.getInstalledVersions()
>|= List.find_opt(x => Versions.Local.(x.name == version))
>>= (
installedVersion =>
switch (installedVersion) {
| None =>
Logger.log(
<Pastel>
"The version "
<Pastel color=Pastel.Cyan> version </Pastel>
" is not installed."
</Pastel>,
);
exit(1);
| Some(installedVersion) =>
{
Logger.log(
<Pastel>
"Uninstalling node "
<Pastel color=Pastel.Cyan>
Versions.Local.(installedVersion.name)
</Pastel>
</Pastel>,
);
};
let%lwt _ = Versions.Local.remove(installedVersion);
Logger.log(
<Pastel>
"Node version "
<Pastel color=Pastel.Cyan>
Versions.Local.(installedVersion.name)
</Pastel>
" has correctly been removed."
</Pastel>,
)
|> Lwt.return;
}
);
};

21
library/Fs.re

@ -43,6 +43,27 @@ let readlink = path => @@ -43,6 +43,27 @@ let readlink = path =>
| err => Lwt.return_error(err)
};
// Credit: https://github.com/fastpack/fastpack/blob/9f6aa7d5b83ffef03e73a15679200576ff9dbcb7/FastpackUtil/FS.re#L94
let rec rmdir = dir => {
let%lwt files = Lwt_unix.files_of_directory(dir) |> Lwt_stream.to_list;
let%lwt () =
Lwt_list.iter_s(
filename =>
switch (filename) {
| "."
| ".." => Lwt.return_unit
| _ =>
let path = Filename.concat(dir, filename);
switch%lwt (Lwt_unix.stat(path)) {
| {st_kind: Lwt_unix.S_DIR, _} => rmdir(path)
| _ => Lwt_unix.unlink(path)
};
},
files,
);
Lwt_unix.rmdir(dir);
};
type path =
| Exists(string)
| Missing(string);

2
library/Versions.re

@ -35,6 +35,8 @@ module Local = { @@ -35,6 +35,8 @@ module Local = {
let toDirectory = name => Filename.concat(Directories.nodeVersions, name);
let remove = version => Fs.rmdir(version.fullPath);
let getLatestInstalledNameByPrefix = prefix => {
open Lwt;
let%lwt versions =

Loading…
Cancel
Save