@@ -51,6 +51,7 @@ type cxt = {
51
51
cut_generators : bool ;
52
52
traverse : bool ;
53
53
namespace : string option ;
54
+ clean_staled_bs_js : bool ;
54
55
}
55
56
56
57
(* * [public] has a list of modules, we do a sanity check to see if all the listed
@@ -217,6 +218,9 @@ let try_unlink s =
217
218
Bsb_log. info " @{<info>Failed to remove %s}@." s
218
219
219
220
221
+ (* * This is the only place where we do some removal during scanning,
222
+ configurabl
223
+ *)
220
224
let clean_staled_bs_js_files
221
225
(context : cxt )
222
226
(cur_sources : _ String_map.t )
@@ -369,7 +373,10 @@ let rec
369
373
| Some s , _ -> parse_sources cxt s
370
374
in
371
375
(* * Do some clean up *)
372
- clean_staled_bs_js_files cxt cur_sources (Lazy. force file_array );
376
+ if cxt.clean_staled_bs_js then
377
+ begin
378
+ clean_staled_bs_js_files cxt cur_sources (Lazy. force file_array )
379
+ end ;
373
380
Bsb_file_groups. merge {
374
381
files = [ { dir ;
375
382
sources = cur_sources;
@@ -396,7 +403,8 @@ and parsing_single_source ({not_dev; dir_index ; cwd} as cxt ) (x : Ext_json_typ
396
403
| Obj {map} ->
397
404
let current_dir_index =
398
405
match String_map. find_opt Bsb_build_schemas. type_ map with
399
- | Some (Str {str ="dev" } ) -> Bsb_dir_index. get_dev_index ()
406
+ | Some (Str {str ="dev" } ) ->
407
+ Bsb_dir_index. get_dev_index ()
400
408
| Some _ -> Bsb_exception. config_error x {| type field expect " dev" literal | }
401
409
| None -> dir_index in
402
410
if not_dev && not (Bsb_dir_index. is_lib_dir current_dir_index) then
@@ -430,13 +438,89 @@ and parse_sources ( cxt : cxt) (sources : Ext_json_types.t ) =
430
438
431
439
432
440
433
- let scan ~not_dev ~root ~cut_generators ~namespace x =
441
+ let scan ~not_dev ~root ~cut_generators ~namespace ~ clean_staled_bs_js x =
434
442
parse_sources {
435
443
not_dev;
436
444
dir_index = Bsb_dir_index. lib_dir_index;
437
445
cwd = Filename. current_dir_name;
438
446
root ;
439
447
cut_generators;
440
448
namespace;
449
+ clean_staled_bs_js;
441
450
traverse = false
442
- } x
451
+ } x
452
+
453
+
454
+
455
+ type walk_cxt = {
456
+ cwd : string ;
457
+ root : string ;
458
+ traverse : bool ;
459
+ }
460
+ let rec walk_sources (cxt : walk_cxt ) (sources : Ext_json_types.t ) =
461
+ match sources with
462
+ | Arr {content = file_groups } ->
463
+ Array. iter (fun x -> walk_single_source cxt x) file_groups
464
+ | x -> walk_single_source cxt x
465
+ and walk_single_source cxt (x : Ext_json_types.t ) =
466
+ match x with
467
+ | Str {str = dir}
468
+ ->
469
+ walk_source_dir_map
470
+ {cxt with
471
+ cwd =
472
+ Ext_path. concat cxt.cwd
473
+ (Ext_filename. simple_convert_node_path_to_os_path dir)
474
+ }
475
+ String_map. empty
476
+ | Obj {map} ->
477
+ begin match String_map. find_opt Bsb_build_schemas. dir map with
478
+ | Some (Str{str} ) ->
479
+ let dir = Ext_filename. simple_convert_node_path_to_os_path str in
480
+ walk_source_dir_map
481
+ {cxt with cwd = Ext_path. concat cxt.cwd dir} map
482
+ | _ -> ()
483
+ end
484
+ | _ -> ()
485
+ and walk_source_dir_map (cxt : walk_cxt ) (input : Ext_json_types.t String_map.t ) =
486
+ let working_dir = Filename. concat cxt.root cxt.cwd in
487
+ let file_array = Sys. readdir working_dir in
488
+ file_array |> Array. iter begin fun file ->
489
+ if Ext_string. ends_with file " .re.js" then
490
+ Sys. remove file
491
+ end;
492
+ let sub_dirs_field =
493
+ String_map. find_opt Bsb_build_schemas. subdirs input in
494
+ let cxt_traverse = cxt.traverse in
495
+ match sub_dirs_field, cxt_traverse with
496
+ | None , true
497
+ | Some (True _ ), _ ->
498
+ file_array |> Array. iter begin fun f ->
499
+ if Sys. is_directory (Filename. concat working_dir f ) then
500
+ walk_source_dir_map
501
+ {cxt with
502
+ cwd =
503
+ Ext_path. concat cxt.cwd
504
+ (Ext_filename. simple_convert_node_path_to_os_path f);
505
+ traverse = true
506
+ } String_map. empty
507
+ end
508
+ | None , _
509
+ | Some (False _ ), _ -> ()
510
+ | Some s , _ -> walk_sources cxt s
511
+
512
+ let clean_re_js root =
513
+ match Ext_json_parse. parse_json_from_file
514
+ (Filename. concat root Literals. bsconfig_json) with
515
+ | Obj {map ; loc} ->
516
+ begin match String_map. find_opt Bsb_build_schemas. sources map with
517
+ | Some config ->
518
+ (try
519
+ walk_sources
520
+ { root ; traverse = true ; cwd = Filename. current_dir_name} config
521
+ with _ -> () )
522
+ | None -> ()
523
+ end
524
+ | _ -> ()
525
+ | exception _ -> ()
526
+
0 commit comments