mirror of
https://github.com/ryan-haskell/elm-spa.git
synced 2024-09-11 12:35:44 +03:00
use correct routing order
This commit is contained in:
parent
1b6d96f347
commit
d90c95ad02
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.DS_Store
|
||||
elm-stuff
|
||||
node_modules
|
||||
dist
|
||||
|
@ -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 }
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
]
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user