From ffb9484332616b6f1d740a4fd2046913e4554c57 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 7 Jul 2021 21:13:17 -0700 Subject: [PATCH] Full build functionality is obsolete now as workers run single-page mode renders. Remove some unused paths. --- generator/src/dir-helpers.js | 10 ++ generator/src/render-worker.js | 2 +- generator/src/render.js | 4 - src/Pages/Internal/Platform/Cli.elm | 146 +------------------- src/Pages/Internal/Platform/ToJsPayload.elm | 43 +----- src/RenderRequest.elm | 25 ++-- 6 files changed, 25 insertions(+), 205 deletions(-) diff --git a/generator/src/dir-helpers.js b/generator/src/dir-helpers.js index e8cb4ab5..02ecdffc 100644 --- a/generator/src/dir-helpers.js +++ b/generator/src/dir-helpers.js @@ -22,6 +22,15 @@ async function tryMkdir(dirName) { } } +/** + * @param {string} filePath + * @param {string} data + */ +function writeFileSyncSafe(filePath, data) { + fsSync.mkdirSync(path.dirname(filePath), { recursive: true }); + fs.writeFileSync(filePath, data); +} + const path = require("path"); /** @@ -67,6 +76,7 @@ module.exports = { readFileSync: fsSync.readFileSync, copyFile: fs.copyFile, exists: fs.exists, + writeFileSyncSafe, tryMkdir, copyDirFlat, copyDirNested, diff --git a/generator/src/render-worker.js b/generator/src/render-worker.js index 1014c330..91aa292f 100644 --- a/generator/src/render-worker.js +++ b/generator/src/render-worker.js @@ -69,7 +69,7 @@ async function outputString( case "api-response": { const body = fromElm.body; console.log(`Generated ${pathname}`); - fs.writeFileSync(path.join("dist", pathname), body); + fs.writeFileSyncSafe(path.join("dist", pathname), body); if (pathname === "/all-paths.json") { parentPort.postMessage(body); } else { diff --git a/generator/src/render.js b/generator/src/render.js index 9edcf831..4ea9c522 100755 --- a/generator/src/render.js +++ b/generator/src/render.js @@ -75,10 +75,6 @@ function runElmApp(elmModule, pagePath, request, addDataSourceWatcher) { async function portHandler(/** @type { FromElm } */ fromElm) { if (fromElm.command === "log") { console.log(fromElm.value); - } else if (fromElm.tag === "InitialData") { - const args = fromElm.args[0]; - // console.log(`InitialData`, args); - writeGeneratedFiles(args.filesToGenerate); } else if (fromElm.tag === "ApiResponse") { const args = fromElm.args[0]; global.staticHttpCache = args.staticHttpCache; diff --git a/src/Pages/Internal/Platform/Cli.elm b/src/Pages/Internal/Platform/Cli.elm index 7e400475..659d3f76 100644 --- a/src/Pages/Internal/Platform/Cli.elm +++ b/src/Pages/Internal/Platform/Cli.elm @@ -89,7 +89,7 @@ cliApplication config = renderRequest : RenderRequest (Maybe route) renderRequest = Decode.decodeValue (RenderRequest.decoder config) flags - |> Result.withDefault RenderRequest.FullBuild + |> Result.withDefault RenderRequest.default in init renderRequest contentCache config flags |> Tuple.mapSecond (perform renderRequest config config.toJsPort) @@ -409,9 +409,6 @@ initLegacy renderRequest { secrets, mode, staticHttpCache } contentCache config StaticResponses.renderApiRequest (DataSource.succeed []) - RenderRequest.FullBuild -> - StaticResponses.init config - unprocessedPages : List ( Path, route ) unprocessedPages = case renderRequest of @@ -426,9 +423,6 @@ initLegacy renderRequest { secrets, mode, staticHttpCache } contentCache config RenderRequest.NotFound path -> [] - RenderRequest.FullBuild -> - [] - unprocessedPagesState : Maybe (List ( Path, route )) unprocessedPagesState = case renderRequest of @@ -443,9 +437,6 @@ initLegacy renderRequest { secrets, mode, staticHttpCache } contentCache config RenderRequest.NotFound path -> Just [] - RenderRequest.FullBuild -> - Nothing - initialModel : Model route initialModel = { staticResponses = staticResponses @@ -769,35 +760,6 @@ nextStepToEffect contentCache config model ( updatedStaticResponsesModel, nextSt StaticResponses.Finish toJsPayload -> case model.mode of Mode.ElmToHtmlBeta -> - let - sendManifestIfNeeded : Effect - sendManifestIfNeeded = - if - List.length model.unprocessedPages - == (model.staticRoutes - |> Maybe.map List.length - |> Maybe.withDefault -1 - ) - && model.maybeRequestJson - == RenderRequest.FullBuild - then - case toJsPayload of - ToJsPayload.Success value -> - Effect.SendSinglePage True - (ToJsPayload.InitialData - { filesToGenerate = value.filesToGenerate - } - ) - - ToJsPayload.Errors _ -> - Effect.SendJsData toJsPayload - - ToJsPayload.ApiResponse -> - Effect.NoEffect - - else - Effect.NoEffect - in case toJsPayload of ToJsPayload.ApiResponse -> let @@ -971,9 +933,6 @@ nextStepToEffect contentCache config model ( updatedStaticResponsesModel, nextSt RenderRequest.NotFound path -> render404Page config model path NotFoundReason.NoMatchingRoute - - RenderRequest.FullBuild -> - [] |> ToJsPayload.Errors |> Effect.SendJsData in ( { model | staticRoutes = Just [] } , apiResponse @@ -998,10 +957,7 @@ nextStepToEffect contentCache config model ( updatedStaticResponsesModel, nextSt |> (\cmds -> ( model |> popProcessedRequest - , Effect.Batch - (sendManifestIfNeeded - :: cmds - ) + , Effect.Batch cmds ) ) @@ -1142,104 +1098,6 @@ sendSinglePageProgress toJsPayload config model = |> ToJsPayload.Errors |> Effect.SendJsData - RenderRequest.FullBuild -> - let - staticData : Dict String String - staticData = - toJsPayload.pages - |> Dict.get (Path.toRelative page) - |> Maybe.withDefault Dict.empty - - currentPage : { path : Path, frontmatter : route } - currentPage = - { path = page, frontmatter = config.urlToRoute currentUrl } - - pageDataResult : Result BuildError pageData - pageDataResult = - StaticHttpRequest.resolve ApplicationType.Browser - (config.data (config.urlToRoute currentUrl)) - (staticData |> Dict.map (\_ v -> Just v)) - |> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path) - - sharedDataResult : Result BuildError sharedData - sharedDataResult = - StaticHttpRequest.resolve ApplicationType.Browser - config.sharedData - (staticData |> Dict.map (\_ v -> Just v)) - |> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path) - - allRoutes : List route - allRoutes = - -- TODO - [] - - currentUrl : { protocol : Url.Protocol, host : String, port_ : Maybe Int, path : String, query : Maybe String, fragment : Maybe String } - currentUrl = - { protocol = Url.Https - , host = config.site allRoutes |> .canonicalUrl - , port_ = Nothing - , path = page |> Path.toRelative - , query = Nothing - , fragment = Nothing - } - - siteDataResult : Result BuildError siteData - siteDataResult = - StaticHttpRequest.resolve ApplicationType.Cli - (config.site allRoutes |> .data) - (staticData |> Dict.map (\_ v -> Just v)) - |> Result.mapError (StaticHttpRequest.toBuildError "Site.elm") - in - case Result.map3 (\a b c -> ( a, b, c )) sharedDataResult pageDataResult siteDataResult of - Ok ( sharedData, pageData, siteData ) -> - let - pageModel : userModel - pageModel = - config.init - Pages.Flags.PreRenderFlags - sharedData - pageData - Nothing - (Just - { path = - { path = currentPage.path - , query = Nothing - , fragment = Nothing - } - , metadata = currentPage.frontmatter - , pageUrl = Nothing - } - ) - |> Tuple.first - - viewValue : { title : String, body : Html userMsg } - viewValue = - (config.view currentPage Nothing sharedData pageData |> .view) pageModel - - headTags : List Head.Tag - headTags = - (config.view currentPage Nothing sharedData pageData |> .head) - ++ (siteData |> (config.site allRoutes |> .head)) - in - { route = page |> Path.toRelative - , contentJson = - toJsPayload.pages - |> Dict.get (Path.toRelative page) - |> Maybe.withDefault Dict.empty - , html = viewValue.body |> HtmlPrinter.htmlToString - , errors = [] - , head = headTags - , title = viewValue.title - , staticHttpCache = model.allRawResponses |> Dict.Extra.filterMap (\_ v -> v) - , is404 = False - } - |> sendProgress - - Err error -> - [ error ] - |> ToJsPayload.Errors - |> Effect.SendJsData - popProcessedRequest : Model route -> Model route popProcessedRequest model = diff --git a/src/Pages/Internal/Platform/ToJsPayload.elm b/src/Pages/Internal/Platform/ToJsPayload.elm index 1454a204..b63e7a14 100644 --- a/src/Pages/Internal/Platform/ToJsPayload.elm +++ b/src/Pages/Internal/Platform/ToJsPayload.elm @@ -1,6 +1,5 @@ module Pages.Internal.Platform.ToJsPayload exposing ( FileToGenerate - , InitialDataRecord , ToJsPayload(..) , ToJsSuccessPayload , ToJsSuccessPayloadNew @@ -181,7 +180,6 @@ headCodec canonicalSiteUrl currentPagePath = type ToJsSuccessPayloadNewCombined = PageProgress ToJsSuccessPayloadNew - | InitialData InitialDataRecord | SendApiResponse { body : String, staticHttpCache : Dict String String, statusCode : Int } | ReadFile String | Glob String @@ -189,22 +187,14 @@ type ToJsSuccessPayloadNewCombined | Port String -type alias InitialDataRecord = - { filesToGenerate : List FileToGenerate - } - - successCodecNew2 : String -> String -> Codec ToJsSuccessPayloadNewCombined successCodecNew2 canonicalSiteUrl currentPagePath = Codec.custom - (\success initialData vReadFile vGlob vDoHttp vSendApiResponse vPort value -> + (\success vReadFile vGlob vDoHttp vSendApiResponse vPort value -> case value of PageProgress payload -> success payload - InitialData payload -> - initialData payload - ReadFile filePath -> vReadFile filePath @@ -221,7 +211,6 @@ successCodecNew2 canonicalSiteUrl currentPagePath = vPort string ) |> Codec.variant1 "PageProgress" PageProgress (successCodecNew canonicalSiteUrl currentPagePath) - |> Codec.variant1 "InitialData" InitialData initialDataCodec |> Codec.variant1 "ReadFile" ReadFile Codec.string |> Codec.variant1 "Glob" Glob Codec.string |> Codec.variant1 "DoHttp" @@ -243,33 +232,3 @@ successCodecNew2 canonicalSiteUrl currentPagePath = ) |> Codec.variant1 "Port" Port Codec.string |> Codec.buildCustom - - -filesToGenerateCodec : Codec (List { path : List String, content : String }) -filesToGenerateCodec = - Codec.build - (\list -> - list - |> Json.Encode.list - (\item -> - Json.Encode.object - [ ( "path", item.path |> String.join "/" |> Json.Encode.string ) - , ( "content", item.content |> Json.Encode.string ) - ] - ) - ) - (Decode.list - (Decode.map2 (\path content -> { path = path, content = content }) - (Decode.string |> Decode.map (String.split "/") |> Decode.field "path") - (Decode.string |> Decode.field "content") - ) - ) - - -initialDataCodec : Codec InitialDataRecord -initialDataCodec = - Codec.object InitialDataRecord - |> Codec.field "filesToGenerate" - .filesToGenerate - filesToGenerateCodec - |> Codec.buildObject diff --git a/src/RenderRequest.elm b/src/RenderRequest.elm index df7395be..7d089a58 100644 --- a/src/RenderRequest.elm +++ b/src/RenderRequest.elm @@ -3,6 +3,7 @@ module RenderRequest exposing , RenderRequest(..) , RequestPayload(..) , decoder + , default , maybeRequestPayload ) @@ -10,6 +11,7 @@ import ApiRoute import HtmlPrinter import Internal.ApiRoute import Json.Decode as Decode +import Json.Encode as Encode import Pages.ProgramConfig exposing (ProgramConfig) import Path exposing (Path) import Regex @@ -24,15 +26,19 @@ type RequestPayload route type RenderRequest route = SinglePage IncludeHtml (RequestPayload route) Decode.Value - | FullBuild + + +default : RenderRequest route +default = + SinglePage + HtmlAndJson + (NotFound (Path.fromString "/error")) + Encode.null maybeRequestPayload : RenderRequest route -> Maybe Decode.Value maybeRequestPayload renderRequest = case renderRequest of - FullBuild -> - Nothing - SinglePage _ _ rawJson -> Just rawJson @@ -46,7 +52,7 @@ decoder : ProgramConfig userMsg userModel (Maybe route) siteData pageData sharedData -> Decode.Decoder (RenderRequest (Maybe route)) decoder config = - optionalField "request" + Decode.field "request" (Decode.map3 (\includeHtml requestThing payload -> SinglePage includeHtml requestThing payload @@ -73,15 +79,6 @@ decoder config = (requestPayloadDecoder config) (Decode.field "payload" Decode.value) ) - |> Decode.map - (\maybeRequest -> - case maybeRequest of - Just request -> - request - - Nothing -> - FullBuild - )