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
node_modules
dist

View File

@ -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 }

View File

@ -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

View File

@ -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
}
]

View File

@ -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"