Skip to content

Commit

Permalink
Add experimental support for .ts files
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Aug 1, 2022
1 parent fdc6601 commit f5e1d62
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions src/cli/strings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Core
open Lwt.Infix
open Lwt.Syntax

let version = "1.3.1"
let version = "1.4.0"

let header = sprintf "/* Generated by okTurtles/strings v%s */\n\n" version

Expand All @@ -24,12 +24,20 @@ let time () =
let diff = Int63.( - ) t1 t0 in
sprintf "%sms" Int63.(to_string (diff / of_int 1_000_000))

let plural i = if i = 1 then "" else "s"

let pool = Lwt_pool.create 6 (fun () -> Lwt.return_unit)

let read_flags = U.[ O_RDONLY; O_NONBLOCK ]

let write_flags = U.[ O_WRONLY; O_NONBLOCK; O_TRUNC; O_CREAT ]

type counts = {
vue: int ref;
js: int ref;
ts: int ref;
}

let process_file ~root strings count filename ~f:extract =
Lwt_pool.use pool (fun () ->
incr count;
Expand All @@ -40,7 +48,7 @@ let process_file ~root strings count filename ~f:extract =
| None -> String.Set.add String.Set.empty data
| Some set -> String.Set.add set data)))

let rec traverse ~root ~count_vue ~count_js strings js_file_errors directory =
let rec traverse ~root ~parse_ts counts strings js_file_errors directory =
let* entries =
Lwt_pool.use pool (fun () -> Lwt_unix.files_of_directory directory |> Lwt_stream.to_list)
in
Expand All @@ -52,15 +60,21 @@ let rec traverse ~root ~count_vue ~count_js strings js_file_errors directory =
let path = sprintf "%s/%s" directory filename in
Lwt_unix.lstat path >>= function
| { st_kind = S_REG; _ } when String.is_suffix ~suffix:".vue" filename ->
process_file ~root strings count_vue path
process_file ~root strings counts.vue path
~f:(Vue.parse ~filename ~f:Vue.(extract_strings js_file_errors))
| { st_kind = S_REG; _ } when String.is_suffix ~suffix:".js" filename ->
process_file ~root strings count_js path ~f:(fun ic ->
process_file ~root strings counts.js path ~f:(fun ic ->
let* source = Lwt_io.read ic in
let parsed = Queue.create () in
let+ () = Parsing.Js_ast.strings_from_js ~filename:path parsed js_file_errors source in
parsed)
| { st_kind = S_DIR; _ } -> traverse ~root ~count_vue ~count_js strings js_file_errors path
| { st_kind = S_REG; _ } when parse_ts && String.is_suffix ~suffix:".ts" filename ->
process_file ~root strings counts.ts path ~f:(fun ic ->
let* source = Lwt_io.read ic in
let parsed = Queue.create () in
let+ () = Parsing.Js_ast.strings_from_js ~filename:path parsed js_file_errors source in
parsed)
| { st_kind = S_DIR; _ } -> traverse ~root ~parse_ts counts strings js_file_errors path
| _ -> Lwt.return_unit))
entries

Expand Down Expand Up @@ -177,6 +191,11 @@ let main args =
Vue.parse ~filename ic ~f:Vue.debug_pug)
| [ _ ] -> failwith "At least one argument is required"
| _ :: directories ->
let directories, parse_ts =
List.fold directories ~init:([], false) ~f:(fun (acc, parse_ts) -> function
| "--ts" -> acc, true
| s -> s :: acc, parse_ts)
in
(* Check current directory *)
let* strings_dir_files =
let git_dir_p = directory_exists ".git" in
Expand All @@ -195,22 +214,28 @@ let main args =
let js_file_errors = Queue.create () in
let* english =
let english_list = String.Table.create () in
let count_vue = ref 0 in
let count_js = ref 0 in
let counts = { vue = ref 0; js = ref 0; ts = ref 0 } in
let time = time () in
let* () =
Lwt_list.iter_p
(fun directory ->
let root = String.chop_suffix ~suffix:"/" directory |> Option.value ~default:directory in
traverse ~root:(sprintf "%s/" root) ~count_vue ~count_js english_list js_file_errors root)
traverse ~root:(sprintf "%s/" root) ~parse_ts counts english_list js_file_errors 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_io.printlf "✅ [%s] Processed %d .vue files and %d .js files" (time ()) !count_vue !count_js
let f ext i = sprintf "%d %s file%s" i ext (plural i) in
match parse_ts with
| true ->
Lwt_io.printlf "✅ [%s] Processed %s, %s, and %s" (time ()) (f ".vue" !(counts.vue))
(f ".js" !(counts.js)) (f ".ts" !(counts.ts))
| false ->
Lwt_io.printlf "✅ [%s] Processed %s and %s" (time ()) (f ".vue" !(counts.vue))
(f ".js" !(counts.js))
in
let+ () = write_english english in
english
Expand Down Expand Up @@ -243,8 +268,8 @@ let main args =
|> Array.map ~f:(fun { filename; _ } -> sprintf "- %s" filename)
|> String.concat_array ~sep:"\n"
in
let plural = if len = 1 then "" else "s" in
Lwt_io.printlf "❌ Encountered %d JS parsing error%s. File%s:\n%s\n" len plural plural files
Lwt_io.printlf "❌ Encountered %d parsing error%s. File%s:\n%s\n" len (plural len) (plural len)
files
in
let t1 = Time_now.nanoseconds_since_unix_epoch () in
Lwt_io.write_line Lwt_io.stdout
Expand Down

0 comments on commit f5e1d62

Please sign in to comment.