From a1e9266f1c599344c63acd7165cb6db1a692c8f3 Mon Sep 17 00:00:00 2001 From: kentac55 Date: Thu, 14 Feb 2019 17:12:07 +0900 Subject: [PATCH] skip installation if the version is already installed (#27) * remove ansi codes from provided version string * add check func whether the specify version is already installed or not * Revert "remove ansi codes from provided version string" This reverts commit 9e5b27f942e8be94c1bffda13a254f46cbf3dece. * apply code review(rename check function) * run esy fmt * move short path to the top of the switch case * use Result.fold instead of switch case * use Array.exists instead of List.exists --- executable/Install.re | 1 + library/Versions.re | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/executable/Install.re b/executable/Install.re index 3c935e8..cc0b2dd 100644 --- a/executable/Install.re +++ b/executable/Install.re @@ -27,6 +27,7 @@ let main = (~version as versionName) => { }; let versionName = Versions.format(versionName); + let%lwt _ = Versions.throwIfInstalled(versionName); Console.log( diff --git a/library/Versions.re b/library/Versions.re index 100ad54..8f19926 100644 --- a/library/Versions.re +++ b/library/Versions.re @@ -8,6 +8,7 @@ module Local = { }; exception Version_not_found(string); +exception Already_installed(string); module Remote = { type t = { @@ -145,3 +146,15 @@ let getRemoteVersions = () => { ) |> Lwt.return; }; + +let throwIfInstalled = versionName => { + getInstalledVersions() + |> Result.fold( + _ => Lwt.return(), + xs => + Array.exists(x => Local.(x.name == versionName), xs) + |> ( + x => x ? Lwt.fail(Already_installed(versionName)) : Lwt.return() + ), + ); +};