Skip to content

Commit 6689700

Browse files
committed
migrate-all command
1 parent ae6fc83 commit 6689700

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tools/bin/main.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Usage: rescript-tools [command]
3333
Commands:
3434

3535
migrate <file> [--stdout] Runs the migration tool on the given file
36+
migrate-all <root> Runs migrations for all project sources under <root>
3637
doc <file> Generate documentation
3738
format-codeblocks <file> Format ReScript code blocks
3839
[--stdout] Output to stdout
@@ -76,6 +77,64 @@ let main () =
7677
| Ok content, `Stdout -> print_endline content
7778
| result, `File -> logAndExit result
7879
| Error e, _ -> logAndExit (Error e))
80+
| "migrate-all" :: root :: _opts -> (
81+
let rootPath =
82+
if Filename.is_relative root then Unix.realpath root else root
83+
in
84+
(* Ensure no project config cache is used; process files exactly once. *)
85+
Analysis.Cfg.readProjectConfigCache := false;
86+
(* Discover project package and files from the given root. *)
87+
match Analysis.Packages.newBsPackage ~rootPath with
88+
| None ->
89+
logAndExit
90+
(Error
91+
(Printf.sprintf
92+
"error: failed to load ReScript project at %s (missing \
93+
bsconfig.json/rescript.json?)"
94+
rootPath))
95+
| Some package ->
96+
let moduleNames =
97+
Analysis.SharedTypes.FileSet.elements package.projectFiles
98+
in
99+
let files =
100+
moduleNames
101+
|> List.filter_map (fun modName ->
102+
Hashtbl.find_opt package.pathsForModule modName
103+
|> Option.map Analysis.SharedTypes.getSrc)
104+
|> List.concat
105+
|> List.filter (fun path ->
106+
Filename.check_suffix path ".res"
107+
|| Filename.check_suffix path ".resi")
108+
in
109+
let total = List.length files in
110+
if total = 0 then logAndExit (Ok "No source files found to migrate")
111+
else
112+
let process_one file =
113+
(file, Tools.Migrate.migrate ~entryPointFile:file ~outputMode:`File)
114+
in
115+
let results = List.map process_one files in
116+
let failures =
117+
results
118+
|> List.fold_left
119+
(fun acc (_file, res) ->
120+
match res with
121+
| Ok _ -> acc
122+
| Error _ -> acc + 1)
123+
0
124+
in
125+
results
126+
|> List.iter (fun (_file, res) ->
127+
match res with
128+
| Ok msg -> print_endline msg
129+
| Error err -> prerr_endline err);
130+
if failures > 0 then
131+
logAndExit
132+
(Error
133+
(Printf.sprintf "Completed with %d failure(s) across %d file(s)"
134+
failures total))
135+
else
136+
logAndExit
137+
(Ok (Printf.sprintf "Migrated %d file(s) successfully" total)))
79138
| "format-codeblocks" :: rest -> (
80139
match rest with
81140
| ["-h"] | ["--help"] -> logAndExit (Ok formatCodeblocksHelp)

0 commit comments

Comments
 (0)