From 8aff334828f57a6be3947b4bf53f96dcca435c9e Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 19 Aug 2019 15:20:20 -0700 Subject: [PATCH] Add helper modules to run elm from NodeJS and rewrite elm.json. --- generator/src/generate-elm-stuff.js | 68 +++++++++++++++++++++++++++++ generator/src/rewrite-elm-json.js | 28 ++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 generator/src/generate-elm-stuff.js create mode 100644 generator/src/rewrite-elm-json.js diff --git a/generator/src/generate-elm-stuff.js b/generator/src/generate-elm-stuff.js new file mode 100644 index 00000000..a08dcc7b --- /dev/null +++ b/generator/src/generate-elm-stuff.js @@ -0,0 +1,68 @@ +const fs = require("fs"); +const runElm = require("./compile-elm.js"); +const copyModifiedElmJson = require("./rewrite-elm-json.js"); + +const elmPagesCliFile = `port module PagesNew exposing (application) + +import Dict exposing (Dict) +import Head +import Html exposing (Html) +import Json.Decode +import Json.Encode +import Mark +import Pages +import Pages.ContentCache exposing (Page) +import Pages.Manifest +import RawContent + + +port toJsPort : Json.Encode.Value -> Cmd msg + + +application : + { init : ( userModel, Cmd userMsg ) + , update : userMsg -> userModel -> ( userModel, Cmd userMsg ) + , subscriptions : userModel -> Sub userMsg + , view : userModel -> List ( List String, metadata ) -> Page metadata view -> { title : String, body : Html userMsg } + , parser : Pages.Parser metadata view + , head : metadata -> List Head.Tag + , frontmatterParser : Json.Decode.Decoder metadata + , markdownToHtml : String -> view + , manifest : Pages.Manifest.Config + } + -> Pages.Program userModel userMsg metadata view +application config = + Pages.cliApplication + { init = config.init + , view = config.view + , update = config.update + , subscriptions = config.subscriptions + , parser = config.parser + , frontmatterParser = config.frontmatterParser + , content = RawContent.content + , markdownToHtml = config.markdownToHtml + , toJsPort = toJsPort + , head = config.head + , manifest = config.manifest + } +`; + +module.exports = function run() { + // mkdir -p elm-stuff/elm-pages/ + // requires NodeJS >= 10.12.0 + fs.mkdirSync("./elm-stuff/elm-pages", { recursive: true }); + + // write `PagesNew.elm` with cli interface + fs.writeFileSync("./elm-stuff/elm-pages/PagesNew.elm", elmPagesCliFile); + + // generate RawContent.elm + // TODO + + // write modified elm.json to elm-stuff/elm-pages/ + copyModifiedElmJson(); + + // run Main.elm from elm-stuff/elm-pages with `runElm` + runElm(function(payload) { + console.log("Received payload!", payload); + }); +}; diff --git a/generator/src/rewrite-elm-json.js b/generator/src/rewrite-elm-json.js new file mode 100644 index 00000000..bdf42b40 --- /dev/null +++ b/generator/src/rewrite-elm-json.js @@ -0,0 +1,28 @@ +const fs = require("fs"); + +module.exports = function() { + var elmJson = JSON.parse(fs.readFileSync("./elm.json").toString()); + + // write new elm.json + fs.writeFileSync( + "./elm-stuff/elm-pages/elm.json", + JSON.stringify(rewriteElmJson(elmJson)) + ); +}; + +function rewriteElmJson(elmJson) { + // The internal generated file will be at: + // ./elm-stuff/elm-pages/ + // So, we need to take the existing elmJson and + // 1. remove existing path that looks at `Pages.elm` + elmJson["source-directories"] = elmJson["source-directories"].filter(item => { + return item != "gen"; + }); + // 2. prepend ../../../ to remaining + elmJson["source-directories"] = elmJson["source-directories"].map(item => { + return "../../" + item; + }); + // 3. add our own secret My.elm module 😈 + elmJson["source-directories"].push("."); + return elmJson; +}