open Nsw; let lwtIgnore = lwt => Lwt.catch(() => lwt, _ => Lwt.return()); exception Version_Not_Installed(string); let switchVersion = version => { let versionDir = Filename.concat(Directories.nodeVersions, version); let%lwt _ = if%lwt (Lwt_unix.file_exists(versionDir) |> Lwt.map(x => !x)) { Lwt.fail(Version_Not_Installed(version)); }; let destination = Filename.concat(versionDir, "installation"); let source = Directories.currentVersion; Console.log( "Linking " source " to " destination , ); let%lwt _ = Lwt_unix.unlink(Directories.currentVersion) |> lwtIgnore; let%lwt _ = Lwt_unix.symlink(destination, Directories.currentVersion); Console.log( "Using " version , ); Lwt.return(); }; let main = (~version as providedVersion) => { let%lwt version = switch (providedVersion) { | Some(version) => Lwt.return(version) | None => Nvmrc.getVersion() }; switchVersion(Versions.format(version)); }; let run = version => try%lwt (main(~version)) { | Version_Not_Installed(version) => Console.log( "The following version is not installed: " version , ) |> Lwt.return | Nvmrc.Version_Not_Provided => Console.log( "No .nvmrc was found in the current directory. Please provide a version number." , ) |> Lwt.return };