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 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 _ = Versions.throwIfInstalled(versionName); Logger.debug( "Looking for node " versionName " for " {System.NodeOS.toString(os)} " " {System.NodeArch.toString(arch)} , ); let%lwt (versionName, filepath) = Versions.getFileToDownload(~version=versionName, ~os, ~arch); let%lwt _ = Versions.throwIfInstalled(versionName); let tarDestination = Filename.concat( Directories.downloads, versionName ++ 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, versionName); Logger.debug( "Extracting " tarDestination " to " extractionDestination , ); Logger.info( "Version " versionName " was successfuly downloaded" , ); let%lwt _ = Compression.extractFile(tarDestination, ~into=extractionDestination); 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.Already_installed(version) => Logger.error( "Version " version " is already installed." , ) |> Lwt.return | Versions.Version_not_found(version) => Logger.error( "Version " version " not found!" , ); exit(1); };