From c578755dfc8470c73e463f44ab56a8670a2f604a Mon Sep 17 00:00:00 2001 From: Simon Grondin Date: Sat, 5 Sep 2020 20:16:38 -0500 Subject: [PATCH] Process .js files --- src/cli/strings.ml | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/cli/strings.ml b/src/cli/strings.ml index a53f120..1f2ba3d 100644 --- a/src/cli/strings.ml +++ b/src/cli/strings.ml @@ -1,7 +1,7 @@ open Core open Lwt.Infix -let version = "1.0.4" +let version = "1.1.0" let header = sprintf "/* Generated by okTurtles/strings v%s */\n\n" version let () = Lwt.async_exception_hook := (fun ex -> @@ -16,10 +16,10 @@ let pool = Lwt_pool.create 6 (fun () -> Lwt.return_unit) let read_flags = Unix.[O_RDONLY; O_NONBLOCK] let write_flags = Unix.[O_WRONLY; O_NONBLOCK; O_TRUNC; O_CREAT] -let process_file ~root (strings, count) filename = +let process_file ~root strings count filename ~f = Lwt_pool.use pool (fun () -> incr count; - let%lwt parsed = Lwt_io.with_file ~mode:Input ~flags:read_flags filename (Vue.parse ~filename ~f:Vue.extract_strings) in + let%lwt parsed = Lwt_io.with_file ~mode:Input ~flags:read_flags filename f in Queue.iter parsed ~f:(fun string -> let data = String.chop_prefix filename ~prefix:root |> Option.value ~default:filename in String.Table.update strings string ~f:(function @@ -30,7 +30,7 @@ let process_file ~root (strings, count) filename = Lwt.return_unit ) -let rec traverse ~root strings directory = +let rec traverse ~root ~count_vue ~count_js strings directory = let%lwt entries = Lwt_pool.use pool (fun () -> Lwt_unix.files_of_directory directory |> Lwt_stream.to_list) in Lwt_list.iter_p (function | filename when String.is_prefix ~prefix:"." filename || String.(=) filename "node_modules" -> @@ -38,8 +38,16 @@ let rec traverse ~root strings directory = | filename -> let path = sprintf "%s/%s" directory filename in begin match%lwt Lwt_unix.lstat path with - | { st_kind = S_REG; _ } when String.is_suffix ~suffix:".vue" filename -> process_file ~root strings path - | { st_kind = S_DIR; _ } -> traverse ~root strings path + | { st_kind = S_REG; _ } when String.is_suffix ~suffix:".vue" filename -> + process_file ~root strings count_vue path ~f:(Vue.parse ~filename ~f:Vue.extract_strings) + | { st_kind = S_REG; _ } when String.is_suffix ~suffix:".js" filename -> + process_file ~root strings count_js path ~f:(fun ic -> + let%lwt source = Lwt_io.read ic in + let parsed = Queue.create () in + Parsing.Js_ast.strings parsed source; + Lwt.return parsed + ) + | { st_kind = S_DIR; _ } -> traverse ~root ~count_vue ~count_js strings path | _ -> Lwt.return_unit end ) entries @@ -48,7 +56,7 @@ let fmt s = Yojson.Basic.to_string (`String s) let json_pair left right first = sprintf "%s\n %s: %s" (if !first then begin first := false; "" end else ",") left right -let write_english english count = +let write_english english = let path_strings = "strings/english.strings" in let path_json = "strings/english.json" in let first = ref true in @@ -72,9 +80,8 @@ let write_english english count = ) ) in - sprintf "✅ Processed %d .vue files\n\n✅ Generated '%s' and '%s' with:\n- %d unique strings\n" - count path_strings path_json (String.Table.length english) - |> Lwt_io.write_line Lwt_io.stdout + Lwt_io.printlf "✅ Generated '%s' and '%s' with:\n- %d unique strings\n" + path_strings path_json (String.Table.length english) let write_other ~language english other = let path_strings = sprintf "strings/%s.strings" language in @@ -134,9 +141,8 @@ let write_other ~language english other = ) ) in - sprintf "✅ Generated '%s' and '%s' with:\n- %d new strings\n- %d existing strings\n- %d unused strings\n" + Lwt_io.printlf "✅ Generated '%s' and '%s' with:\n- %d new strings\n- %d existing strings\n- %d unused strings\n" path_strings path_json n_left n_both n_right - |> Lwt_io.write_line Lwt_io.stdout let directory_exists path = begin match%lwt Lwt_unix.stat path with @@ -175,17 +181,20 @@ let main args = (* English *) let%lwt english = let english_list = String.Table.create () in - let count = ref 0 in + let count_vue = ref 0 in + let count_js = ref 0 in let%lwt () = Lwt_list.iter_p (fun directory -> let root = (String.chop_suffix ~suffix:"/" directory |> Option.value ~default:directory) in - traverse ~root:(sprintf "%s/" root) (english_list, count) root + traverse ~root:(sprintf "%s/" root) ~count_vue ~count_js english_list root ) directories in let english = String.Table.map english_list ~f:(fun set -> String.Set.to_array set |> String.concat_array ~sep:", " ) in - let%lwt () = write_english english !count in + let%lwt () = Lwt_io.printlf "✅ Processed %d .vue files" !count_vue in + let%lwt () = Lwt_io.printlf "✅ Processed %d .js files" !count_js in + let%lwt () = write_english english in Lwt.return english in (* Other languages *)