Permalink
Browse files

[enhance] System: moved 'which' function to System module instead of …

…a BSL
  • Loading branch information...
1 parent 5a3ed84 commit 2f4f14d3854c1be37df8bc5d2d6447e72250f636 @Aqua-Ye Aqua-Ye committed Sep 24, 2012
Showing with 20 additions and 18 deletions.
  1. +0 −16 lib/plugins/unix/bslSys.nodejs
  2. +1 −1 lib/stdlib/database/mongo/db.opa
  3. +19 −1 lib/stdlib/system/system.opa
@@ -188,19 +188,3 @@ function get_env_var(key) {
var r = process.env[key];
return typeof r == 'undefined' ? js_none : js_some(r);
}
-
-/**
- * @register {string -> opa[option(string)]}
- */
-function which(command) {
- var env_path = process.env.PATH;
- if (!env_path) return js_none;
- var env_path_array = env_path.split(':');
- for (i in env_path_array) {
- var resolved_path = path.resolve(env_path_array[i]+"/"+command);
- if (fs.existsSync(resolved_path)) {
- return js_some(resolved_path);
- }
- };
- return js_none;
-}
@@ -497,7 +497,7 @@ Then use option --db-remote instead of --db-local.
error("Can not create directory \'{path}\'")
)
/* Check for MongoDB binary */
- which_mongod = %%BslSys.which%%("mongod")
+ which_mongod = System.which("mongod")
mngpath =
match which_mongod with
{none} ->
@@ -149,7 +149,6 @@ System = {{
*/
exec = @may_cps(%%bslSys.process.exec%%) : string, string -> string
-
@private
async_shell_exec = %%bslSys.process.async_shell_exec%% : string, string, (System.process.out->void) -> System.process
@@ -168,4 +167,23 @@ System = {{
r
~{p result}
+ /**
+ * Equivalent of UNIX `which` command
+ */
+ which(command) =
+ env_path_opt = %%BslSys.get_env_var%%("PATH")
+ match env_path_opt
+ {none} -> none
+ {some=env_path} ->
+ env_path_list = String.explode(":", env_path)
+ List.fold(path, acc ->
+ match acc
+ {none} ->
+ bin = path + "/" + command
+ exists = %%BslFile.exists%%(bin)
+ if exists then some(bin)
+ else acc
+ {some=_} -> acc
+ , env_path_list, none)
+
}}

0 comments on commit 2f4f14d

Please sign in to comment.