@@ -2,6 +2,7 @@ module Codegen = RescriptRelayRouterCli__Codegen
22module Utils = RescriptRelayRouterCli__Utils
33module Types = RescriptRelayRouterCli__Types
44module Diagnostics = RescriptRelayRouterCli__Diagnostics
5+ module DumpRoutes = RescriptRelayRouterCli__DumpRoutes
56
67open RescriptRelayRouterCli__Bindings
78
@@ -239,8 +240,16 @@ let printRouteInfo = (~url, ~config) => {
239240 }
240241}
241242
242- @val
243- external stringifyFormatted : ('any , @as (json ` null` ) _ , @as (json ` 2` ) _ ) => string = "JSON.stringify"
243+ let rec dumpRoutesSortOrder = options => {
244+ switch options {
245+ | list {"--sort" , "alphabetic" , ... _ }
246+ | list {"--sort=alphabetic" , ... _ } => Types .Alphabetic
247+ | list {"--sort" , "definition" , ... _ }
248+ | list {"--sort=definition" , ... _ } => Types .DefinitionOrder
249+ | list {_ , ... rest } => dumpRoutesSortOrder (rest )
250+ | list {} => Types .DefinitionOrder
251+ }
252+ }
244253
245254let init = () => {
246255 if ! Utils .Config .exists () {
@@ -249,9 +258,9 @@ let init = () => {
249258
250259 Fs .writeFileSync (
251260 path ,
252- ` module.exports = ${{
253- "routesFolderPath" : "./src/routes" ,
254- }->stringifyFormatted }` ,
261+ ` module.exports = ${JSON.Object(dict {
262+ "routesFolderPath" : JSON.String( "./src/routes" ) ,
263+ })->JSON.stringify(~space=2) }` ,
255264 )
256265
257266 Console .log ("[init] Config created at: " ++ path )
@@ -267,17 +276,17 @@ let init = () => {
267276 Console .log ("[init] `routes.json` does not exist, creating..." )
268277 Fs .writeFileSync (
269278 routesJsonPath ,
270- (
271- {
272- "path" : "/" ,
273- "name" : "Root" ,
274- "children" : [] ,
275- },
276- {
277- "path" : "*" ,
278- "name" : "FourOhFour" ,
279- },
280- )-> stringifyFormatted ,
279+ JSON . Array ([
280+ JSON . Object ( dict {
281+ "path" : JSON . String ( "/" ) ,
282+ "name" : JSON . String ( "Root" ) ,
283+ "children" : JSON . Array ([]) ,
284+ }) ,
285+ JSON . Object ( dict {
286+ "path" : JSON . String ( "*" ) ,
287+ "name" : JSON . String ( "FourOhFour" ) ,
288+ }) ,
289+ ] )-> JSON . stringify (~ space = 2 ) ,
281290 )
282291
283292 Console .log ("[init] Basic `routes.json` added at: " ++ routesJsonPath )
@@ -317,7 +326,14 @@ let runCli = args => {
317326 | have a route defined anymore.
318327
319328 find-route <url> | Shows what routes/components will render for a specific route.
320- | Example: find-route /todos/123` )
329+ | Example: find-route /todos/123
330+
331+ dump-routes
332+ [--sort definition|alphabetic]
333+ [--include-query-params]
334+ [--include-name]
335+ [--include-route-renderer-path]
336+ [--include-route-file-path] | Dumps all routes as JSON. Sorts by definition order by default.` )
321337 Done
322338 | list {"scaffold-route-renderers" , ... options } =>
323339 let deleteRemoved = options -> List .has ("-delete-removed" , String .equal )
@@ -384,6 +400,19 @@ let runCli = args => {
384400 let config = Utils .Config .load ()
385401 printRouteInfo (~url = route , ~config )
386402 Done
403+ | list {"dump-routes" , ... options } =>
404+ let config = Utils .Config .load ()
405+ DumpRoutes .run (
406+ ~config ,
407+ ~options = {
408+ includeQueryParams : options -> List .has ("--include-query-params" , String .equal ),
409+ includeName : options -> List .has ("--include-name" , String .equal ),
410+ includeRouteRendererPath : options -> List .has ("--include-route-renderer-path" , String .equal ),
411+ includeRouteFilePath : options -> List .has ("--include-route-file-path" , String .equal ),
412+ sortOrder : dumpRoutesSortOrder (options ),
413+ },
414+ )
415+ Done
387416 | list {"init" } =>
388417 init ()
389418 Done
0 commit comments