Skip to content

Commit

Permalink
If found package.json doesn't have engines, continue traversing
Browse files Browse the repository at this point in the history
  • Loading branch information
Santeri Hiltunen committed Jan 14, 2019
1 parent 40faace commit a2a4e23
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
22 changes: 15 additions & 7 deletions bin/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,26 @@ let program_version_env_var program =
let s = String.uppercase_ascii (Program.to_string program) in
Printf.sprintf "NODE_SHIM_%s_VERSION" s

let get_version_from_package_json program =
match Package.find_package_json_res () with
let rec get_version_from_package_json ?(dir = Sys.getcwd ()) program =
match Package.find_package_json_res dir with
| Error _ -> None
| Ok path -> (
Logger.debug ("Found package.json: " ^ path);
let ch = open_in path in
let engines = Package.parse_engines_from_chan ch in
Logger.debug ("Found engines: " ^ Package.string_of_engines engines);
match get_version engines program with
| None -> None
| Some semver ->
Some (Shim.find_highest_available_version program semver) )
match engines with
| None ->
if path = File.root_dir then None
else (
Logger.debug "Found package.json without engines. Traversing up...";
let parent_dir = File.parent_dir (Filename.dirname path) in
get_version_from_package_json ~dir:parent_dir program )
| Some engines -> (
Logger.debug ("Found engines: " ^ Package.string_of_engines engines);
match get_version engines program with
| None -> None
| Some semver ->
Some (Shim.find_highest_available_version program semver) ) )

let get_executable_path program =
let global_exec = "/usr/local/bin/" ^ Program.to_string program in
Expand Down
4 changes: 4 additions & 0 deletions lib/file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

val home_dir : string

val root_dir : string

val find_file_by_traversing_up : string -> string -> string

val join : string list -> string

val parent_dir : string -> string
26 changes: 15 additions & 11 deletions lib/package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ let string_of_engines e =
let s o = match o with Some s -> Semver.to_string s | None -> "None" in
Printf.sprintf "node: %s, npm: %s, yarn: %s" (s e.node) (s e.npm) (s e.yarn)

let find_package_json () =
File.find_file_by_traversing_up (Sys.getcwd ()) "package.json"
let find_package_json dir = File.find_file_by_traversing_up dir "package.json"

let find_package_json_res () =
try Ok (find_package_json ()) with e -> Error e
let find_package_json_res dir =
try Ok (find_package_json dir) with e -> Error e

let parse_engines_from_chan ch =
let json = J.from_channel ch in
let open J.Util in
let engines_member = json |> member "engines" in
let parse_semver_from m =
try Some (Semver.of_string (engines_member |> member m |> to_string))
with _ -> None
let engines_missing =
match engines_member with `Null -> true | _ -> false
in
let node = parse_semver_from "node" in
let npm = parse_semver_from "npm" in
let yarn = parse_semver_from "yarn" in
make_engines node npm yarn
if engines_missing then None
else
let parse_semver_from m =
try Some (Semver.of_string (engines_member |> member m |> to_string))
with _ -> None
in
let node = parse_semver_from "node" in
let npm = parse_semver_from "npm" in
let yarn = parse_semver_from "yarn" in
Some (make_engines node npm yarn)

0 comments on commit a2a4e23

Please sign in to comment.