Skip to content

Commit

Permalink
Add fnm exec implementation. wip.
Browse files Browse the repository at this point in the history
  • Loading branch information
Schniz committed Feb 24, 2020
1 parent 4e25a10 commit e838a16
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
30 changes: 30 additions & 0 deletions executable/Exec.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
open Fnm;

let startsWith = (~prefix, str) =>
Base.String.prefix(str, String.length(prefix)) != prefix;

let run = (~cmd) => {
let fnmPath = Filename.concat(Directories.currentVersion, "bin");
let path = Opt.(Sys.getenv_opt("PATH") or "");
let pathEnv = Printf.sprintf("PATH=%s:%s", fnmPath, path);
let env =
Unix.environment()
|> Array.copy
|> Base.Array.filter(~f=startsWith(~prefix="PATH="))
|> Array.append([|pathEnv|]);
let%lwt exitCode =
Lwt_process.exec(
~stdin=`Keep,
~stdout=`Keep,
~stderr=`Keep,
~env,
("", cmd),
);

switch (exitCode) {
| Unix.WEXITED(0) => Lwt.return_ok()
| Unix.WEXITED(x)
| Unix.WSTOPPED(x)
| Unix.WSIGNALED(x) => Lwt.return_error(x)
};
};
37 changes: 36 additions & 1 deletion executable/FnmApp.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let runCmd = lwt => {
};

module Commands = {
let exec = cmd => Exec.run(~cmd=Array.of_list(cmd)) |> runCmd;
let use = (version, quiet) => Use.run(~version, ~quiet) |> runCmd;
let alias = (version, name) => Alias.run(~name, ~version) |> runCmd;
let default = version => Alias.run(~name="default", ~version) |> runCmd;
Expand Down Expand Up @@ -233,6 +234,30 @@ let alias = {
);
};

let exec = {
let doc = "Execute a binary with the current Node.js in the PATH";
let man = help_secs;
let sdocs = Manpage.s_common_options;

let command = {
let doc = "The $(docv) to execute";
Arg.(non_empty & pos_all(string, []) & info([], ~docv="COMMAND", ~doc));
};

(
Term.(const(Commands.exec) $ command),
Term.info(
"exec",
~envs,
~version,
~doc,
~exits=Term.default_exits,
~man,
~sdocs,
),
);
};

let default = {
let doc = "Alias a version as default";
let man = help_secs;
Expand Down Expand Up @@ -378,7 +403,17 @@ let argv =
let _ =
Term.eval_choice(
defaultCmd,
[install, uninstall, use, alias, default, listLocal, listRemote, env],
[
install,
uninstall,
use,
alias,
default,
listLocal,
listRemote,
env,
exec,
],
~argv,
)
|> Term.exit;
2 changes: 1 addition & 1 deletion test/TestListRemote.re
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ let allVersions6_11 = [
"v6.11.5",
];

describe("List Remote", ({test}) => {
describe("List Remote", ({test, _}) => {
let versionRegExp = Str.regexp(".*[0-9]+\\.[0-9]+\\.[0-9]+\\|.*latest-*");

let filterVersionNumbers = response =>
Expand Down

0 comments on commit e838a16

Please sign in to comment.