Browse Source

Implement `realpath` instead of binding to C library (#102)

drop Filename.realpath usage, for crossplatform support
remotes/origin/add-simple-redirecting-site
Gal Schlezinger 6 years ago committed by GitHub
parent
commit
c2f0d58b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      executable/ListInstallations.re
  2. 4
      executable/ListLocal.re
  3. 4
      executable/ListRemote.re
  4. 25
      library/Fs.re
  5. 43
      library/Versions.re

37
executable/ListInstallations.re

@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
open Fnm;
let colorizeVersions = (~current, ~versions) => {
let strings =
versions
|> List.map(version => {
open Versions.Local;
let str = "- " ++ version.name;
let color =
current
|> Opt.bind(current =>
current.name == version.name ? Some(Pastel.Green) : None
);
<Pastel ?color> str </Pastel>;
});
<Pastel>
<Pastel color=Pastel.Cyan> "## List of installed versions:\n" </Pastel>
<Pastel> ...strings </Pastel>
</Pastel>;
};
let getVersionsString = () =>
Result.(
{
let%bind versions =
Versions.getInstalledVersions() |> Result.map(Array.to_list);
let current = Versions.getCurrentVersion();
colorizeVersions(~current, ~versions) |> Result.return;
}
);
let run = () => getVersionsString() |> Result.map(Console.log) |> Lwt.return;

4
executable/ListLocal.re

@ -8,8 +8,8 @@ let main = () => @@ -8,8 +8,8 @@ let main = () =>
let%lwt versions =
try%lwt (Versions.getInstalledVersions()) {
| _ => Lwt.fail(Cant_read_local_versions)
};
let currentVersion = Versions.getCurrentVersion();
}
and currentVersion = Versions.getCurrentVersion();
Console.log("The following versions are installed:");

4
executable/ListRemote.re

@ -3,8 +3,8 @@ open Fnm; @@ -3,8 +3,8 @@ open Fnm;
let run = () => {
Console.log("Looking for some node versions upstream...");
let%lwt versions = Versions.getRemoteVersions();
let currentVersion = Versions.getCurrentVersion();
let%lwt versions = Versions.getRemoteVersions()
and currentVersion = Versions.getCurrentVersion();
versions
|> List.iter(version => {

25
library/Fs.re

@ -1,5 +1,3 @@ @@ -1,5 +1,3 @@
open Core;
let readdir = dir => {
let items = ref([]);
let%lwt dir = Lwt_unix.opendir(dir);
@ -40,9 +38,22 @@ let exists = path => { @@ -40,9 +38,22 @@ let exists = path => {
};
};
let realpath = Filename.realpath;
let readlink = path =>
try%lwt (Lwt_unix.readlink(path) |> Lwt.map(x => Ok(x))) {
| err => Lwt.return_error(err)
};
let try_readlink = path =>
try (Ok(Unix.readlink(path))) {
| err => Error(err)
};
type path =
| Exists(string)
| Missing(string);
let rec realpath = path => {
switch%lwt (readlink(path)) {
| Ok(path) => realpath(path)
| Error(_) =>
switch%lwt (exists(path)) {
| true => Exists(path) |> Lwt.return
| false => Missing(path) |> Lwt.return
}
};
};

43
library/Versions.re

@ -72,19 +72,23 @@ module Aliases = { @@ -72,19 +72,23 @@ module Aliases = {
| _ => Lwt.return([])
};
aliases
|> List.map(alias => {
|> Lwt_list.map_p(alias => {
let fullPath = Filename.concat(Directories.aliases, alias);
{
let%lwt realpath =
Filename.concat(Directories.aliases, alias)
|> Fs.realpath
|> Lwt.map(
fun
| Fs.Exists(x) => x
| Fs.Missing(x) => x,
);
Lwt.return({
name: alias,
fullPath,
versionName:
Filename.concat(Directories.aliases, alias)
|> Fs.realpath
|> Filename.dirname
|> Filename.basename,
};
})
|> Lwt.return;
versionName: realpath |> Filename.dirname |> Filename.basename,
});
});
};
let byVersion = () => {
@ -172,24 +176,17 @@ let endsWith = (~suffix, str) => { @@ -172,24 +176,17 @@ let endsWith = (~suffix, str) => {
exception No_Download_For_System(System.NodeOS.t, System.NodeArch.t);
let getCurrentVersion = () => {
switch (Fs.realpath(Directories.currentVersion)) {
| installationPath =>
switch%lwt (Fs.realpath(Directories.currentVersion)) {
| Missing(x) when x == Directories.currentVersion => Lwt.return_none
| Missing(_) => Lwt.return_some(Local.systemVersion)
| Exists(installationPath) =>
let fullPath = Filename.dirname(installationPath);
Some(
Lwt.return_some(
Local.{fullPath, name: Core.Filename.basename(fullPath), aliases: []},
);
| exception (Unix.Unix_error(_, _, _)) =>
switch (Fs.try_readlink(Directories.currentVersion)) {
| Ok(x)
when
Core.String.substr_index(x, ~pattern=Local.systemVersion.fullPath)
== Some(0) =>
Some(Local.systemVersion)
| Ok(_)
| Error(_) => None
}
};
};
let getInstalledVersions = () => {
let%lwt versions =
Fs.readdir(Directories.nodeVersions) |> Lwt.map(List.sort(compare))

Loading…
Cancel
Save