use correct routing order

This commit is contained in:
Ryan Haskell-Glatz 2020-03-28 01:16:04 -05:00
parent 1b6d96f347
commit d90c95ad02
5 changed files with 86 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.DS_Store
elm-stuff elm-stuff
node_modules node_modules
dist dist

View File

@ -1,5 +1,6 @@
module Main exposing (main) module Main exposing (main)
import Path
import Platform import Platform
import Ports import Ports
@ -20,7 +21,7 @@ main =
( () ( ()
, case command of , case command of
"build" -> "build" ->
Ports.build filepaths Ports.build (filepaths |> List.map Path.fromFilepath |> List.sortWith Path.routingOrder)
"add" -> "add" ->
Ports.add { pageType = pageType, name = name } Ports.add { pageType = pageType, name = name }

View File

@ -4,6 +4,7 @@ module Path exposing
, fromModuleName , fromModuleName
, hasParams , hasParams
, optionalParams , optionalParams
, routingOrder
, toFilepath , toFilepath
, toFlags , toFlags
, toList , toList
@ -237,6 +238,44 @@ hasParams path =
dynamicCount path > 0 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 -- HELPERS

View File

@ -6,7 +6,7 @@ import Add.Sandbox
import Add.Static import Add.Static
import Generators.Pages as Pages import Generators.Pages as Pages
import Generators.Route as Route import Generators.Route as Route
import Path import Path exposing (Path)
port addPort : { filepath : String, content : String } -> Cmd msg port addPort : { filepath : String, content : String } -> Cmd msg
@ -56,11 +56,9 @@ add { name, pageType } =
uhhh () uhhh ()
build : List String -> Cmd msg build : List Path -> Cmd msg
build filepaths = build paths =
filepaths buildPort
|> List.map Path.fromFilepath
|> (\paths ->
[ { filepath = "Generated/Route.elm" [ { filepath = "Generated/Route.elm"
, content = Route.generate paths , content = Route.generate paths
} }
@ -68,5 +66,3 @@ build filepaths =
, content = Pages.generate paths , content = Pages.generate paths
} }
] ]
)
|> buildPort

View File

@ -8,7 +8,39 @@ import Test exposing (..)
suite : Test suite : Test
suite = suite =
describe "Path" 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" <| [ test "ignores first slash" <|
\_ -> \_ ->
"/Top.elm" "/Top.elm"