open Fnm; let mkDownloadsDir = () => { let exists = Lwt_unix.file_exists(Directories.downloads); if%lwt (exists |> Lwt.map(x => !x)) { Logger.debug( "Creating " Directories.downloads " for the first time" , ); let%lwt _ = System.mkdirp(Directories.downloads); Lwt.return(); } else { Lwt.return(); }; }; let download = (~version, ~filepath) => { let tarDestination = Filename.concat( Directories.downloads, version ++ Versions.Remote.downloadFileSuffix, ); Logger.debug( "Downloading " filepath " to " tarDestination , ); let%lwt _ = System.mkdirp(Filename.dirname(tarDestination)); let%lwt _ = Http.download(filepath, ~into=tarDestination); let extractionDestination = Filename.concat(Directories.nodeVersions, version); Logger.debug( "Extracting " tarDestination " to " extractionDestination , ); Logger.info( "Version " version " was successfully downloaded" , ); 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); let%lwt versionName = switch (versionName) { | "latest-*" => switch%lwt (VersionListingLts.getLatest()) { | Error(err) => raise(VersionListingLts.Problem_with_finding_latest_lts(err)) | Ok({VersionListingLts.lts, _}) => Printf.sprintf("latest-%s", lts) |> Lwt.return } | _ => Lwt.return(versionName) }; Logger.debug( "Looking for node " versionName " for " {System.NodeOS.toString(os)} " " {System.NodeArch.toString(arch)} , ); let%lwt (fullVersionName, filepath) = Versions.getFileToDownload(~version=versionName, ~os, ~arch); let%lwt isAlreadyInstalled = Versions.isInstalled(fullVersionName); let%lwt _ = if (isAlreadyInstalled) { Logger.error( "Version " fullVersionName " is already installed." , ); 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; }; Lwt.return(); }; let run = (~version) => try%lwt(main(~version)) { | Versions.No_Download_For_System(os, arch) => Logger.error( "Version exists, but can't find a file for your system:\n" " OS: " {System.NodeOS.toString(os)} "\n" " Architecture: " {System.NodeArch.toString(arch)} , ); exit(1); | Versions.Version_not_found(version) => Logger.error( "Version " version " not found!" , ); exit(1); | VersionListingLts.Problem_with_finding_latest_lts( VersionListingLts.Cant_find_latest_lts, ) => Logger.error( "Can't find latest LTS" ); exit(1); | VersionListingLts.Problem_with_finding_latest_lts( VersionListingLts.Cant_parse_remote_version_listing(reason), ) => Logger.error( "Can't parse json of the response:\n" reason , ); exit(1); };