diff --git a/.gitignore b/.gitignore index c32621b..fd7ac02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store elm-stuff node_modules dist diff --git a/cli/src/Main.elm b/cli/src/Main.elm index 464faa4..bf69e57 100644 --- a/cli/src/Main.elm +++ b/cli/src/Main.elm @@ -1,5 +1,6 @@ module Main exposing (main) +import Path import Platform import Ports @@ -20,7 +21,7 @@ main = ( () , case command of "build" -> - Ports.build filepaths + Ports.build (filepaths |> List.map Path.fromFilepath |> List.sortWith Path.routingOrder) "add" -> Ports.add { pageType = pageType, name = name } diff --git a/cli/src/Path.elm b/cli/src/Path.elm index b7b0f39..b97f517 100644 --- a/cli/src/Path.elm +++ b/cli/src/Path.elm @@ -4,6 +4,7 @@ module Path exposing , fromModuleName , hasParams , optionalParams + , routingOrder , toFilepath , toFlags , toList @@ -237,6 +238,44 @@ hasParams path = dynamicCount path > 0 +routingOrder : Path -> Path -> Order +routingOrder (Internals list1) (Internals list2) = + let + endsIn : String -> List String -> Bool + endsIn str list = + list + |> List.reverse + |> List.head + |> (==) (Just str) + + endsInTop = + endsIn "Top" + + endsInDynamic = + endsIn "Dynamic" + in + if List.length list1 < List.length list2 then + LT + + else if List.length list1 > List.length list2 then + GT + + else if endsInTop list1 && not (endsInTop list2) then + LT + + else if not (endsInTop list1) && endsInTop list2 then + GT + + else if not (endsInDynamic list1) && endsInDynamic list2 then + LT + + else if endsInDynamic list1 && not (endsInDynamic list2) then + GT + + else + EQ + + -- HELPERS diff --git a/cli/src/Ports.elm b/cli/src/Ports.elm index 6951334..349e0cc 100644 --- a/cli/src/Ports.elm +++ b/cli/src/Ports.elm @@ -6,7 +6,7 @@ import Add.Sandbox import Add.Static import Generators.Pages as Pages import Generators.Route as Route -import Path +import Path exposing (Path) port addPort : { filepath : String, content : String } -> Cmd msg @@ -56,17 +56,13 @@ add { name, pageType } = uhhh () -build : List String -> Cmd msg -build filepaths = - filepaths - |> List.map Path.fromFilepath - |> (\paths -> - [ { filepath = "Generated/Route.elm" - , content = Route.generate paths - } - , { filepath = "Generated/Pages.elm" - , content = Pages.generate paths - } - ] - ) - |> buildPort +build : List Path -> Cmd msg +build paths = + buildPort + [ { filepath = "Generated/Route.elm" + , content = Route.generate paths + } + , { filepath = "Generated/Pages.elm" + , content = Pages.generate paths + } + ] diff --git a/cli/tests/Tests/Path.elm b/cli/tests/Tests/Path.elm index d498b7e..288de4f 100644 --- a/cli/tests/Tests/Path.elm +++ b/cli/tests/Tests/Path.elm @@ -8,7 +8,39 @@ import Test exposing (..) suite : Test suite = describe "Path" - [ describe "fromFilepath" + [ describe "routingOrder" + [ test "prioritizes dynamic last" <| + \_ -> + Path.routingOrder + (Path.fromFilepath "Example/Top.elm") + (Path.fromFilepath "Example/Dynamic.elm") + |> Expect.equal LT + , test "prioritizes files over folders with top" <| + \_ -> + Path.routingOrder + (Path.fromFilepath "Example/Top.elm") + (Path.fromFilepath "Example.elm") + |> Expect.equal GT + , test "works with List.sortWith as expected" <| + \_ -> + [ Path.fromFilepath "Top.elm" + , Path.fromFilepath "About.elm" + , Path.fromFilepath "Authors/Dynamic/Posts/Dynamic.elm" + , Path.fromFilepath "Posts/Dynamic.elm" + , Path.fromFilepath "Posts.elm" + , Path.fromFilepath "Posts/Top.elm" + ] + |> List.sortWith Path.routingOrder + |> Expect.equalLists + [ Path.fromFilepath "Top.elm" + , Path.fromFilepath "About.elm" + , Path.fromFilepath "Posts.elm" + , Path.fromFilepath "Posts/Top.elm" + , Path.fromFilepath "Posts/Dynamic.elm" + , Path.fromFilepath "Authors/Dynamic/Posts/Dynamic.elm" + ] + ] + , describe "fromFilepath" [ test "ignores first slash" <| \_ -> "/Top.elm"