@@ -36,11 +36,31 @@ let node_current = "."
36
36
37
37
let cwd = lazy (Sys. getcwd () )
38
38
39
+ let (// ) = Filename. concat
40
+
41
+ let combine path1 path2 =
42
+ if path1 = " " then
43
+ path2
44
+ else if path2 = " " then path1
45
+ else
46
+ if Filename. is_relative path2 then
47
+ path1// path2
48
+ else
49
+ path2
50
+
51
+ (* Note that [.//] is the same as [./] *)
52
+ let path_as_directory x =
53
+ if x = " " then x
54
+ else
55
+ if Ext_string. ends_with x Filename. dir_sep then
56
+ x
57
+ else
58
+ x ^ Filename. dir_sep
39
59
40
60
let absolute_path s =
41
61
let s =
42
62
if Filename. is_relative s then
43
- Filename. concat ( Lazy. force cwd) s
63
+ Lazy. force cwd // s
44
64
else s in
45
65
(* Now simplify . and .. components *)
46
66
let rec aux s =
@@ -49,14 +69,15 @@ let absolute_path s =
49
69
if dir = s then dir
50
70
else if base = Filename. current_dir_name then aux dir
51
71
else if base = Filename. parent_dir_name then Filename. dirname (aux dir)
52
- else Filename. concat ( aux dir) base
72
+ else aux dir // base
53
73
in
54
74
aux s
55
75
56
76
let chop_extension ?(loc =" " ) name =
57
77
try Filename. chop_extension name
58
78
with Invalid_argument _ ->
59
- invalid_arg (" Filename.chop_extension (" ^ loc ^ " :" ^ name ^ " )" )
79
+ Ext_pervasives. invalid_argf
80
+ " Filename.chop_extension ( %s : %s )" loc name
60
81
61
82
let try_chop_extension s = try Filename. chop_extension s with _ -> s
62
83
@@ -103,20 +124,26 @@ let node_modules = "node_modules"
103
124
let node_modules_length = String. length " node_modules"
104
125
let package_json = " package.json"
105
126
127
+
128
+
129
+
106
130
(* * path2: a/b
107
131
path1: a
108
132
result: ./b
109
133
TODO: [Filename.concat] with care
134
+
135
+ [file1] is currently compilation file
136
+ [file2] is the dependency
110
137
*)
111
- let node_relative_path path1 path2 =
112
- let v = Ext_string. find path2 ~sub: node_modules in
113
- let len = String. length path2 in
138
+ let node_relative_path file1 file2 =
139
+ let v = Ext_string. find file2 ~sub: node_modules in
140
+ let len = String. length file2 in
114
141
if v > = 0 then
115
142
let rec skip i =
116
143
if i > = len then
117
- failwith ( " invalid path: " ^ path2)
144
+ Ext_pervasives. failwithf " invalid path: %s " file2
118
145
else
119
- match path2 .[i] with
146
+ match file2 .[i] with
120
147
| '/'
121
148
| '.' -> skip (i + 1 )
122
149
| _ -> i
@@ -129,22 +156,22 @@ let node_relative_path path1 path2 =
129
156
This seems weird though
130
157
*)
131
158
in
132
- Ext_string. tail_from path2
159
+ Ext_string. tail_from file2
133
160
(skip (v + node_modules_length))
134
161
else
135
- ( relative_path
136
- (try_chop_extension ( absolute_path path2) )
137
- (try_chop_extension ( absolute_path path1) )
138
- ) ^ node_sep ^
139
- ( try_chop_extension (Filename. basename path2) )
162
+ relative_path
163
+ (absolute_path file2 )
164
+ (absolute_path file1 )
165
+ ^ node_sep ^
166
+ try_chop_extension (Filename. basename file2 )
140
167
141
168
142
169
143
170
(* * [resolve cwd module_name], [cwd] is current working directory, absolute path
144
171
*)
145
172
let resolve ~cwd module_name =
146
173
let rec aux origin cwd module_name =
147
- let v = Filename. concat ( Filename. concat cwd node_modules) module_name
174
+ let v = ( cwd // node_modules) // module_name
148
175
in
149
176
if Sys. is_directory v then v
150
177
else
@@ -158,9 +185,7 @@ let resolve ~cwd module_name =
158
185
159
186
let resolve_package cwd =
160
187
let rec aux cwd =
161
- let v = Filename. concat cwd package_json
162
- in
163
- if Sys. file_exists v then cwd
188
+ if Sys. file_exists (cwd // package_json) then cwd
164
189
else
165
190
let cwd' = Filename. dirname cwd in
166
191
if String. length cwd' < String. length cwd then
0 commit comments