You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.0 KiB

open Fnm;
6 years ago
let lwtIgnore = lwt => Lwt.catch(() => lwt, _ => Lwt.return());
exception Version_Not_Installed(string);
let log = (~quiet, arg) =>
if (!quiet) {
Console.log(arg);
};
6 years ago
let switchVersion = (~version, ~quiet) => {
open Lwt;
let log = log(~quiet);
let%lwt parsedVersion =
Versions.parse(version) >>= Opt.toLwt(Version_Not_Installed(version));
let%lwt versionPath =
switch (parsedVersion) {
| Local(version) => Versions.Local.toDirectory(version) |> Lwt.return
| Alias(alias) => Versions.Aliases.toDirectory(alias) |> Lwt.return
6 years ago
};
let destination = Filename.concat(versionPath, "installation");
6 years ago
let source = Directories.currentVersion;
log(
6 years ago
<Pastel>
"Linking "
<Pastel color=Pastel.Cyan> source </Pastel>
" to "
<Pastel color=Pastel.Cyan> destination </Pastel>
</Pastel>,
);
let%lwt _ = Lwt_unix.unlink(Directories.currentVersion) |> lwtIgnore;
let%lwt _ = Lwt_unix.symlink(destination, Directories.currentVersion)
and defaultAliasExists = Lwt_unix.file_exists(Directories.defaultVersion);
let%lwt _ =
if (!defaultAliasExists) {
Versions.Aliases.set(~alias="default", ~versionPath=destination);
} else {
Lwt.return();
};
6 years ago
log(
6 years ago
<Pastel> "Using " <Pastel color=Pastel.Cyan> version </Pastel> </Pastel>,
);
Lwt.return();
};
let main = (~version as providedVersion, ~quiet) => {
6 years ago
let%lwt version =
switch (providedVersion) {
| Some(version) => Lwt.return(version)
| None => Nvmrc.getVersion()
};
switchVersion(~version, ~quiet);
6 years ago
};
let run = (~version, ~quiet) =>
try%lwt (main(~version, ~quiet)) {
6 years ago
| Version_Not_Installed(version) =>
log(
~quiet,
6 years ago
<Pastel color=Pastel.Red>
"The following version is not installed: "
version
</Pastel>,
)
|> Lwt.return
| Nvmrc.Version_Not_Provided =>
log(
~quiet,
6 years ago
<Pastel color=Pastel.Red>
"No .nvmrc was found in the current directory. Please provide a version number."
</Pastel>,
)
|> Lwt.return
};