format output, begin work on route template

This commit is contained in:
Ryan Haskell-Glatz 2019-11-12 17:22:53 -08:00
parent 50b9c78fd9
commit f7613943fe
4 changed files with 188 additions and 41 deletions

View File

@ -2,13 +2,24 @@ module File exposing
( File
, encode
, params
, route
)
import Json.Encode as Json
type alias Filepath =
List String
type alias GroupedFiles =
{ moduleName : String
, paths : List Filepath
}
type alias File =
{ filepath : List String
{ filepath : Filepath
, contents : String
}
@ -22,22 +33,25 @@ encode file =
-- PARAMS TEMPLATE
-- PARAMS
params :
{ moduleName : String
, paths : List (List String)
}
-> String
params : GroupedFiles -> File
params options =
{ filepath = filepathFor options.moduleName "Params"
, contents = paramsContents options
}
paramsContents : GroupedFiles -> String
paramsContents options =
"""
module {{moduleName}} exposing (..)
module {{paramModuleName}} exposing (..)
{{paramsTypeAliases}}
"""
|> String.replace "{{moduleName}}"
|> String.replace "{{paramModuleName}}"
(paramsModuleName options.moduleName)
|> String.replace "{{paramsTypeAliases}}"
(paramsTypeAliases options.paths)
@ -45,10 +59,8 @@ module {{moduleName}} exposing (..)
paramsModuleName : String -> String
paramsModuleName name =
[ "Generated", name, "Params" ]
|> List.filter (String.isEmpty >> not)
|> String.join "."
paramsModuleName =
moduleNameFor "Params"
paramsTypeAliases : List (List String) -> String
@ -77,10 +89,7 @@ paramsRecord path =
|> List.filter ((==) "Dynamic")
|> List.length
in
if dynamicCount < 1 then
"{}"
else
if dynamicCount > 0 then
List.range 1 dynamicCount
|> List.map String.fromInt
|> List.map (\num -> [ "param", num, " : String" ])
@ -88,11 +97,123 @@ paramsRecord path =
|> String.join "\n, "
|> (\str -> "{ " ++ str ++ "\n}")
else
"{}"
-- ROUTES
route : GroupedFiles -> File
route options =
{ filepath = filepathFor options.moduleName "Route"
, contents = routeContents options
}
routeContents : GroupedFiles -> String
routeContents options =
"""
module {{routeModuleName}} exposing
( Route(..)
, toPath
)
{{routeImports}}
"""
|> String.replace "{{routeModuleName}}" (routeModuleName options.moduleName)
|> String.replace "{{routeImports}}" (routeImports options.moduleName)
|> String.trim
routeModuleName : String -> String
routeModuleName =
moduleNameFor "Route"
routeImports moduleName =
"""
import {{paramModuleName}} as Params
{{routeFolderImports}}
"""
|> String.replace "{{paramModuleName}}"
(paramsModuleName moduleName)
|> String.replace "{{routeFolderImports}}"
(routeFolderImports moduleName [ "Docs", "Guide" ])
|> String.trim
routeFolderImports : String -> List String -> String
routeFolderImports moduleName folderNames =
folderNames
|> List.map (routeFolderModuleName moduleName)
|> List.map (String.append "import ")
|> String.join "\n"
|> String.trim
routeFolderModuleName : String -> String -> String
routeFolderModuleName moduleName folderName =
"{{moduleFolder}}.{{folderName}}.Route"
|> String.replace "{{folderName}}" folderName
|> String.replace "{{moduleFolder}}"
(String.join "." <| List.filter nonEmptyString ("Generated" :: String.split "." moduleName))
routeTypes =
"""
type Route
= Top Generated.Params.Top
| Docs Generated.Params.Docs
| NotFound Generated.Params.NotFound
| SignIn Generated.Params.SignIn
| Guide Generated.Params.Guide
| Guide_Folder Generated.Guide.Route.Route
| Docs_Folder Generated.Docs.Route.Route
"""
|> String.trim
routeToPath =
"""
toPath : Route -> String
toPath route =
case route of
Top _ ->
"/"
Docs _ ->
"/docs"
NotFound _ ->
"/not-found"
SignIn _ ->
"/sign-in"
Guide _ ->
"/guide"
Guide_Folder subRoute ->
"/guide" ++ Generated.Guide.Route.toPath subRoute
Docs_Folder subRoute ->
"/docs" ++ Generated.Docs.Route.toPath subRoute
"""
|> String.trim
-- UTILS
fromModuleName : String -> List String
fromModuleName =
String.split "."
>> List.filter (not << String.isEmpty)
last : List a -> Maybe a
last list =
List.drop (List.length list - 1) list |> List.head
@ -104,3 +225,20 @@ indent tabs str =
|> String.split "\n"
|> List.map (\s -> String.concat (List.repeat tabs " " ++ [ s ]))
|> String.join "\n"
filepathFor : String -> String -> List String
filepathFor moduleName name =
fromModuleName moduleName ++ [ name ]
moduleNameFor : String -> String -> String
moduleNameFor ending name =
[ "Generated", name, ending ]
|> List.filter (String.isEmpty >> not)
|> String.join "."
nonEmptyString : String -> Bool
nonEmptyString =
not << String.isEmpty

View File

@ -23,21 +23,29 @@ main =
}
-- ACTUAL CODE
parse : List Filepath -> Cmd msg
parse =
List.foldl groupByFolder Dict.empty
>> (\dict ->
[ paramsFiles dict
>> toGroupedFiles
>> generate
[ File.params
, File.route
]
)
>> List.concat
>> Ports.sendFiles
-- UTILS
generate : List (a -> b) -> List a -> List b
generate fns value =
List.map
(\fn -> List.map fn value)
fns
|> List.concat
folderOf : Filepath -> String
folderOf =
List.reverse
@ -59,19 +67,18 @@ groupByFolder items =
)
paramsFiles :
Dict String (List Filepath)
-> List File
paramsFiles =
type alias GroupedFiles =
{ moduleName : String
, paths : List Filepath
}
toGroupedFiles : Dict String (List Filepath) -> List GroupedFiles
toGroupedFiles =
Dict.toList
>> List.map
(\( moduleName, paths ) ->
{ filepath =
String.split "." moduleName ++ [ "Params" ]
, contents =
File.params
{ moduleName = moduleName
, paths = paths
}
}
)

View File

@ -62,7 +62,10 @@ const Elm = (_ => {
const handlers = {
sendFiles: (data) =>
data.forEach(a => console.log(a.filepath) || console.log(a.contents))
data.forEach(a =>
console.log() ||
console.log(bold(a.filepath.join('/') + '.elm')) ||
console.log('\n' + a.contents + '\n'))
}
const run = paths =>
@ -81,6 +84,8 @@ const Elm = (_ => {
}
})()
const bold = str => '\033[1m' + str + '\033[0m'
module.exports = {
Elm,
File

View File

@ -4,9 +4,6 @@ module Generated.Route exposing
)
import Generated.Docs.Route
import Generated.Guide.Dynamic.Dynamic.Route
import Generated.Guide.Dynamic.Faq.Route
import Generated.Guide.Dynamic.Route
import Generated.Guide.Route
import Generated.Params