Skip to content

Commit

Permalink
[common] Add FLOWLIB_ROOT variable
Browse files Browse the repository at this point in the history
  • Loading branch information
goodmind committed Sep 24, 2019
1 parent f6e4773 commit aab2fd6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 42 deletions.
25 changes: 20 additions & 5 deletions src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ let file_options =
let msg = "Could not locate flowlib files" in
FlowExitStatus.(exit ~msg Could_not_find_flowconfig)
in
let ignores_of_arg root patterns extras =
let ignores_of_arg root default_lib_dir patterns extras =
let patterns = Core_list.rev_append extras patterns in
Core_list.map
~f:(fun s ->
Expand All @@ -578,6 +578,8 @@ let file_options =
|> remove_exclusion
|> Str.split_delim Files.project_root_token
|> String.concat root
|> Str.split_delim Files.flowlib_root_token
|> String.concat default_lib_dir
|> Str.regexp
in
(s, reg))
Expand Down Expand Up @@ -638,17 +640,30 @@ let file_options =
fun ~root ~no_flowlib ~temp_dir ~includes ~ignores ~libs ~untyped ~declarations flowconfig ->
let default_lib_dir =
let no_flowlib = no_flowlib || FlowConfig.no_flowlib flowconfig in
Some (default_lib_dir ~no_flowlib temp_dir)
default_lib_dir ~no_flowlib temp_dir
in
let ignores = ignores_of_arg root (FlowConfig.ignores flowconfig) ignores in
let untyped = ignores_of_arg root (FlowConfig.untyped flowconfig) untyped in
let declarations = ignores_of_arg root (FlowConfig.declarations flowconfig) declarations in
let ignores = ignores_of_arg
root
(Path.to_string default_lib_dir)
(FlowConfig.ignores flowconfig)
ignores in
let untyped = ignores_of_arg
root
(Path.to_string default_lib_dir)
(FlowConfig.untyped flowconfig)
untyped in
let declarations = ignores_of_arg
root
(Path.to_string default_lib_dir)
(FlowConfig.declarations flowconfig)
declarations in
let lib_paths = lib_paths ~root flowconfig libs in
let includes =
includes
|> Core_list.rev_append (FlowConfig.includes flowconfig)
|> includes_of_arg ~root ~lib_paths
in
let default_lib_dir = Some (default_lib_dir) in
{
Files.default_lib_dir;
ignores;
Expand Down
70 changes: 33 additions & 37 deletions src/common/files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,43 +300,6 @@ let get_all =
in
(fun next -> get_all_rec next SSet.empty)

let init ?(flowlibs_only = false) (options : options) =
let node_module_filter = is_node_module options in
let libs =
if flowlibs_only then
[]
else
options.lib_paths
in
let (libs, filter) =
match options.default_lib_dir with
| None -> (libs, is_valid_path ~options)
| Some root ->
let is_in_flowlib = is_prefix (Path.to_string root) in
let is_valid_path = is_valid_path ~options in
let filter path = is_in_flowlib path || is_valid_path path in
(root :: libs, filter)
in
(* preserve enumeration order *)
let libs =
if libs = [] then
[]
else
let get_next lib =
let lib_str = Path.to_string lib in
(* TODO: better to parse json files, not ignore them *)
let filter' path = (path = lib_str || filter path) && not (is_json_file path) in
make_next_files_following_symlinks
~node_module_filter
~path_filter:filter'
~realpath_filter:filter'
~error_filter:(fun _ -> true)
[lib]
in
libs |> Core_list.map ~f:(fun lib -> SSet.elements (get_all (get_next lib))) |> List.flatten
in
(libs, SSet.of_list libs)

(* Local reference to the module exported by a file. Like other local references
to modules imported by the file, it is a member of Context.module_map. *)
let module_ref file = File_key.to_string file
Expand All @@ -353,6 +316,8 @@ let absolute_path_regexp = Str.regexp "^\\(/\\|[A-Za-z]:[/\\\\]\\)"

let project_root_token = Str.regexp_string "<PROJECT_ROOT>"

let flowlib_root_token = Str.regexp_string "<FLOWLIB_ROOT>"

let is_matching path pattern_list =
List.fold_left
(fun current (pattern, rx) ->
Expand Down Expand Up @@ -393,6 +358,37 @@ let wanted ~options lib_fileset =

let watched_paths options = Path_matcher.stems options.includes

let init ?(flowlibs_only=false) (options: options) =
let node_module_filter = is_node_module options in
let libs = if flowlibs_only then [] else options.lib_paths in
let libs, filter = match options.default_lib_dir with
| None -> libs, is_valid_path ~options
| Some root ->
let is_in_flowlib = is_prefix (Path.to_string root) in
let is_valid_path = is_valid_path ~options in
let filter path = (is_in_flowlib path || is_valid_path path) && (not (is_ignored options path)) in
root::libs, filter
in
(* preserve enumeration order *)
let libs = if libs = []
then []
else
let get_next lib =
let lib_str = Path.to_string lib in
let filter' path = path = lib_str || filter path in
make_next_files_following_symlinks
~node_module_filter
~path_filter:filter'
~realpath_filter:filter'
~error_filter:(fun _ -> true)
[lib]
in
libs
|> Core_list.map ~f:(fun lib -> SSet.elements (get_all (get_next lib)))
|> List.flatten
in
(libs, SSet.of_list libs)

(**
* Creates a "next" function (see also: `get_all`) for finding the files in a
* given FlowConfig root. This means all the files under the root and all the
Expand Down
2 changes: 2 additions & 0 deletions src/common/files.mli
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ val absolute_path_regexp : Str.regexp

val project_root_token : Str.regexp

val flowlib_root_token : Str.regexp

val watched_paths : options -> Path.t list

(* given a root, make a filter for file names *)
Expand Down

0 comments on commit aab2fd6

Please sign in to comment.