Skip to content

Commit

Permalink
Process .js files
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Grondin committed Sep 6, 2020
1 parent 046cdc2 commit c578755
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/cli/strings.ml
Original file line number Diff line number Diff line change
@@ -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 ->
Expand All @@ -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
Expand All @@ -30,16 +30,24 @@ 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" ->
Lwt.return_unit
| 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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 *)
Expand Down

0 comments on commit c578755

Please sign in to comment.