Skip to content

Commit

Permalink
Merge pull request #401 from mnxn/fix-windows-which
Browse files Browse the repository at this point in the history
Fix path handling on Windows
  • Loading branch information
mnxn authored Oct 9, 2020
2 parents d98056c + 245cd40 commit 7597e9f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix incomplete error message for missing commands (#400)
- Fix highlighting of quoted string literals that contain quotes (#403)
- Fix path handling for global sandboxes on Windows (#401)

## 1.3.1

Expand Down
18 changes: 18 additions & 0 deletions src/bindings/node/node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ module Stream = struct
val end_ : t -> unit [@@js.call]
end

module Path = struct
val delimiter : string [@@js.global "path.delimiter"]

let delimiter =
assert (String.length delimiter = 1);
delimiter.[0]

val basename : string -> string [@@js.global "path.basename"]

val dirname : string -> string [@@js.global "path.dirname"]

val extname : string -> string [@@js.global "path.extname"]

val isAbsolute : string -> bool [@@js.global "path.isAbsolute"]

val join : (string list[@js.variadic]) -> string [@@js.global "path.join"]
end

module Fs = struct
val read_dir : string -> string list Promise.t [@@js.global "fs.readDir"]

Expand Down
14 changes: 14 additions & 0 deletions src/bindings/node/node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ module Stream : sig
val end_ : t -> unit
end

module Path : sig
val delimiter : char

val basename : string -> string

val dirname : string -> string

val extname : string -> string

val isAbsolute : string -> bool

val join : string list -> string
end

module Fs : sig
val read_dir : string -> (string list, string) result Promise.t

Expand Down
8 changes: 3 additions & 5 deletions src/bindings/node/node_stub.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var fs = require("fs");
var child_process = require("child_process");
var promisify = require("util").promisify;

joo_global_object.fs = {
Expand All @@ -8,7 +7,6 @@ joo_global_object.fs = {
exists: promisify(fs.exists),
};

joo_global_object.child_process = {
exec: child_process.exec,
spawn: child_process.spawn,
};
joo_global_object.child_process = require("child_process");

joo_global_object.path = require("path");
22 changes: 10 additions & 12 deletions src/cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ let path_missing_from_env = "'PATH' variable not found in the environment"

let append { bin; args = args1 } args2 = { bin; args = args1 @ args2 }

let candidate_fns =
if Sys.unix then
fun x ->
[| x |]
else
fun x ->
[| ".exe"; ".cmd" |] |> Array.map ~f:(fun ext -> Path.with_ext x ~ext)
let candidates bin =
let bin_ext ext = Path.with_ext bin ~ext in
match Platform.t with
| Win32 -> [ bin_ext ".exe"; bin_ext ".cmd" ]
| _ -> [ bin ]

let which path fn =
let candidates = candidate_fns fn in
String.split ~on:env_sep path
let which path bin =
String.split ~on:Path.delimiter path
|> Promise.List.find_map (fun p ->
let p = Path.of_string p in
Array.map candidates ~f:(Path.join p)
|> Promise.Array.find_map (fun p ->
candidates bin
|> List.map ~f:(Path.join p)
|> Promise.List.find_map (fun p ->
let open Promise.Syntax in
Fs.exists (Path.to_string p) >>| function
| true -> Some p
Expand Down
5 changes: 0 additions & 5 deletions src/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ module Process = Node.Process
module ChildProcess = Node.ChildProcess
module Fs = Node.Fs

let env_sep =
match Sys.unix with
| true -> ':'
| false -> ';'

let property_exists json property =
Ojs.has_property (Jsonoo.t_to_js json) property

Expand Down
25 changes: 12 additions & 13 deletions src/path.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,34 @@ let of_string s = s

let to_string s = s

let is_absolute t = Filename.is_absolute t
let delimiter = Node.Path.delimiter

let is_absolute t = Node.Path.isAbsolute t

let compare = String.compare

let dirname = Filename.dirname
let dirname = Node.Path.dirname

let extname t =
match Filename.split_extension t with
| _, Some ext -> ext
| _, None -> ""
let extname = Node.Path.extname

let basename t = Filename.basename t
let basename = Node.Path.basename

let ( / ) = Filename.concat
let join x y = Node.Path.join [ x; y ]

let relative = ( / )
let ( / ) = join

let relative_all p xs = List.fold_left xs ~f:Filename.concat ~init:p
let relative = join

let join x y = Filename.concat x y
let relative_all p xs = List.fold_left xs ~f:join ~init:p

let with_ext x ~ext = x ^ ext

let is_root = function
| "" -> true
| x -> String.equal (Filename.dirname x) x
| x -> equal (dirname x) x

let parent x =
if is_root x then
None
else
Some (Filename.dirname x)
Some (dirname x)
6 changes: 4 additions & 2 deletions src/path.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ val of_string : string -> t

val to_string : t -> string

val delimiter : char

val is_absolute : t -> bool

val compare : t -> t -> int
Expand All @@ -18,14 +20,14 @@ val extname : t -> string

val basename : t -> string

val join : t -> t -> t

val ( / ) : t -> string -> t

val relative : t -> string -> t

val relative_all : t -> string list -> t

val join : t -> t -> t

val with_ext : t -> ext:string -> t

val parent : t -> t option
Expand Down

0 comments on commit 7597e9f

Please sign in to comment.