diff --git a/cli/src/elm/File.elm b/cli/src/elm/File.elm index 28fa2b3..2ac8774 100644 --- a/cli/src/elm/File.elm +++ b/cli/src/elm/File.elm @@ -117,15 +117,20 @@ paramsRecord path = -- ROUTE -route : Details -> File -route details = - { filepath = filepathFor details.moduleName "Route" - , contents = routeContents details +type alias Context = + { shouldImportParams : String -> Bool } -routeContents : Details -> String -routeContents details = +route : Context -> Details -> File +route context details = + { filepath = filepathFor details.moduleName "Route" + , contents = routeContents context details + } + + +routeContents : Context -> Details -> String +routeContents context details = """ module {{routeModuleName}} exposing ( Route(..) @@ -143,7 +148,7 @@ module {{routeModuleName}} exposing |> String.replace "{{routeModuleName}}" (routeModuleName details.moduleName) |> String.replace "{{routeImports}}" - (routeImports details) + (routeImports context details) |> String.replace "{{routeTypes}}" (routeTypes details) |> String.replace "{{routeToPath}}" @@ -161,19 +166,29 @@ routeModuleNameFromFilepath = String.join "." >> routeModuleName -routeImports : Details -> String -routeImports details = +routeImports : Context -> Details -> String +routeImports context details = """ -import {{paramModuleName}} as Params +{{paramImport}} {{routeFolderImports}} """ - |> String.replace "{{paramModuleName}}" - (paramsModuleName details.moduleName) + |> String.replace "{{paramImport}}" + (paramImport context details) |> String.replace "{{routeFolderImports}}" (routeFolderImports details.folders) |> String.trim +paramImport : Context -> Details -> String +paramImport context details = + if context.shouldImportParams details.moduleName then + paramsModuleName details.moduleName + |> (\str -> "import " ++ str ++ " as Params") + + else + "" + + routeFolderImports : List Filepath -> String routeFolderImports folderNames = folderNames @@ -325,15 +340,15 @@ toItems { folders, files } = -- PAGES -pages : Details -> File -pages details = +pages : Context -> Details -> File +pages context details = { filepath = filepathFor details.moduleName "Pages" - , contents = pagesContents details + , contents = pagesContents context details } -pagesContents : Details -> String -pagesContents details = +pagesContents : Context -> Details -> String +pagesContents context details = """ module {{pagesModuleName}} exposing ( Model @@ -346,7 +361,7 @@ import Spa.Page import Spa.Path exposing (Path, static, dynamic) import {{layoutModuleName}} as Layout import Utils.Spa as Spa -import {{paramsModuleName}} as Params +{{paramImport}} import {{routeModuleName}} as Route exposing (Route) {{pagesPageImports}} {{pagesFolderRouteImports}} @@ -421,7 +436,7 @@ bundle bigModel = """ |> String.replace "{{pagesModuleName}}" (pagesModuleName details.moduleName) |> String.replace "{{layoutModuleName}}" (pagesLayoutModuleName details.moduleName) - |> String.replace "{{paramsModuleName}}" (paramsModuleName details.moduleName) + |> String.replace "{{paramImport}}" (paramImport context details) |> String.replace "{{routeModuleName}}" (routeModuleName details.moduleName) |> String.replace "{{pagesPageImports}}" (pagesPageImports details.files) |> String.replace "{{pagesFolderRouteImports}}" (pagesFolderImports "Route" details.folders) diff --git a/cli/src/elm/Main.elm b/cli/src/elm/Main.elm index 89e1b31..da50e8f 100644 --- a/cli/src/elm/Main.elm +++ b/cli/src/elm/Main.elm @@ -98,12 +98,27 @@ build { paths } = [ [ File [ "Routes" ] (File.routes paths) ] , paths |> List.foldl groupByFolder Dict.empty + |> Debug.log "grouped" |> toDetails - |> generate - [ File.params - , File.route - , File.pages - ] + |> (\items -> + let + itemsWithFiles : List File.Details + itemsWithFiles = + List.filter + (.files >> List.isEmpty >> not) + items + + shouldImportParams : String -> Bool + shouldImportParams filepath = + List.member filepath + (List.map .moduleName itemsWithFiles) + in + List.concat + [ List.map File.params itemsWithFiles + , List.map (File.route { shouldImportParams = shouldImportParams }) items + , List.map (File.pages { shouldImportParams = shouldImportParams }) items + ] + ) ] |> List.map (\file -> { file | filepath = List.append [ "elm-stuff", ".elm-spa", "Generated" ] file.filepath }) |> Ports.createFiles @@ -152,10 +167,10 @@ layoutsToCreate { path, existingLayouts } = |> List.filter (\list -> not (List.member list existingLayouts)) -generate : List (a -> b) -> List a -> List b +generate : List (a -> Maybe b) -> List a -> List b generate fns value = List.map - (\fn -> List.map fn value) + (\fn -> List.filterMap fn value) fns |> List.concat @@ -219,7 +234,27 @@ toDetails : Dict String Items -> List File.Details toDetails dict = - Dict.toList dict + [ ( "" + , { files = Set.fromList [ [ "NotFound" ], [ "Top" ] ] + , folders = Set.fromList [ [ "Authors" ] ] + } + ) + , ( "Authors" + , { files = Set.fromList [] + , folders = Set.fromList [ [ "Authors", "Dynamic" ] ] + } + ) + , ( "Authors.Dynamic" + , { files = Set.fromList [] + , folders = Set.fromList [ [ "Authors", "Dynamic", "Posts" ] ] + } + ) + , ( "Authors.Dynamic.Posts" + , { files = Set.fromList [ [ "Authors", "Dynamic", "Posts", "Dynamic" ] ] + , folders = Set.fromList [] + } + ) + ] |> List.map (\( moduleName, { files, folders } ) -> { moduleName = moduleName diff --git a/cli/src/index.js b/cli/src/index.js index ce84056..9520159 100755 --- a/cli/src/index.js +++ b/cli/src/index.js @@ -4,6 +4,8 @@ const path = require('path') const cwd = process.cwd() const { File, Elm, bold } = require('./utils.js') +const debug = (a) => console.log(a) || a + const main = ([ command, ...args ] = []) => commands[command] ? commands[command](args) @@ -77,6 +79,7 @@ const build = ([ relative = '.' ]) => { return Elm.checkForElmSpaJson(dir) .then(json => File.paths(path.join(dir, 'src', 'Pages')) + .then(debug) .then(Elm.run('build', { relative }, json['elm-spa'])) .then(Elm.formatOutput) ) @@ -85,27 +88,31 @@ const build = ([ relative = '.' ]) => { .catch(console.error) } +const version = + `${bold('elm-spa')} ${package.version}` + // elm-spa help const help = () => console.info(` +${version} + usage: ${bold('elm-spa')} [...] commands: - ${bold('init')} [options] create a new project at + ${bold('init')} [options] create a new project at options: - ${bold('--ui=')} the module your \`view\` uses (default: Element) + ${bold('--ui=')} the ui module your \`view\` uses + (default: Element) - examples: - ${bold('elm-spa init your-project')} - ${bold('elm-spa init --ui=Element your-project')} + examples: + ${bold('elm-spa init your-project')} + ${bold('elm-spa init --ui=Html your-project')} - - ${bold('build')} generate pages and routes - - examples: - ${bold('elm-spa build .')} + ${bold('build')} generate pages and routes + examples: + ${bold('elm-spa build .')} ${bold('add')} static create a new static page sandbox create a new sandbox page @@ -116,13 +123,11 @@ commands: ${bold('elm-spa add static AboutUs')} ${bold('elm-spa add element Settings.Index')} - ${bold('help')} print this help screen examples: ${bold('elm-spa help')} ${bold('elm-spa wat')} - ${bold('elm-spa huh?')} `) const commands = { @@ -130,7 +135,7 @@ const commands = { add, build, help, - '-v': _ => console.info(`elm-spa version ${package.version}`) + '-v': _ => console.info(version) } main(process.argv.slice(2)) \ No newline at end of file