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.

141 lines
3.4 KiB

open Fnm;
6 years ago
let mkDownloadsDir = () => {
let exists = Lwt_unix.file_exists(Directories.downloads);
if%lwt (exists |> Lwt.map(x => !x)) {
Logger.debug(
6 years ago
<Pastel>
"Creating "
<Pastel color=Pastel.Cyan> Directories.downloads </Pastel>
" for the first time"
</Pastel>,
);
let%lwt _ = System.mkdirp(Directories.downloads);
Lwt.return();
} else {
Lwt.return();
};
};
let download = (~version, ~filepath) => {
6 years ago
let tarDestination =
Filename.concat(
Directories.downloads,
version ++ Versions.Remote.downloadFileSuffix,
);
6 years ago
Logger.debug(
6 years ago
<Pastel>
"Downloading "
<Pastel color=Pastel.Cyan> filepath </Pastel>
" to "
<Pastel color=Pastel.Cyan> tarDestination </Pastel>
</Pastel>,
);
let%lwt _ = System.mkdirp(Filename.dirname(tarDestination));
let%lwt _ = Http.download(filepath, ~into=tarDestination);
let extractionDestination =
Filename.concat(Directories.nodeVersions, version);
6 years ago
Logger.debug(
6 years ago
<Pastel>
"Extracting "
<Pastel color=Pastel.Cyan> tarDestination </Pastel>
" to "
<Pastel color=Pastel.Cyan> extractionDestination </Pastel>
</Pastel>,
);
Logger.info(
<Pastel>
"Version "
<Pastel color=Pastel.Cyan> version </Pastel>
6 years ago
" was successfully downloaded"
</Pastel>,
);
6 years ago
let%lwt _ =
Compression.extractFile(tarDestination, ~into=extractionDestination);
Lwt.return_unit;
};
let main = (~version as versionName) => {
let%lwt os = System.NodeOS.get()
and arch = System.NodeArch.get()
and versionName =
switch (versionName) {
| Some(versionName) => Lwt.return(versionName)
| None => Dotfiles.getVersion()
};
let versionName = Versions.format(versionName);
Logger.debug(
<Pastel>
"Looking for node "
<Pastel color=Pastel.Cyan> versionName </Pastel>
" for "
<Pastel color=Pastel.Cyan>
{System.NodeOS.toString(os)}
" "
{System.NodeArch.toString(arch)}
</Pastel>
</Pastel>,
);
let%lwt (fullVersionName, filepath) =
Versions.getFileToDownload(~version=versionName, ~os, ~arch);
let%lwt isAlreadyInstalled = Versions.isInstalled(fullVersionName);
let%lwt _ =
if (isAlreadyInstalled) {
Logger.error(
<Pastel>
"Version "
<Pastel color=Pastel.Cyan> fullVersionName </Pastel>
" is already installed."
</Pastel>,
);
Lwt.return_unit;
} else {
download(~version=fullVersionName, ~filepath);
};
let%lwt _ =
if (Base.String.is_prefix(versionName, ~prefix="latest")) {
let%lwt _ = Alias.run(~name=versionName, ~version=fullVersionName);
Lwt.return_unit;
} else {
Lwt.return_unit;
};
6 years ago
Lwt.return();
};
let run = (~version) =>
try%lwt (main(~version)) {
| Versions.No_Download_For_System(os, arch) =>
Logger.error(
6 years ago
<Pastel>
"Version exists, but can't find a file for your system:\n"
" OS: "
<Pastel color=Pastel.Cyan> {System.NodeOS.toString(os)} </Pastel>
"\n"
" Architecture: "
<Pastel color=Pastel.Cyan> {System.NodeArch.toString(arch)} </Pastel>
</Pastel>,
);
exit(1);
6 years ago
| Versions.Version_not_found(version) =>
Logger.error(
6 years ago
<Pastel>
"Version "
<Pastel color=Pastel.Cyan> version </Pastel>
" not found!"
</Pastel>,
);
exit(1);
};