Add helper modules to run elm from NodeJS and rewrite elm.json.

This commit is contained in:
Dillon Kearns 2019-08-19 15:20:20 -07:00
parent 07152b8fa3
commit 8aff334828
2 changed files with 96 additions and 0 deletions

View File

@ -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);
});
};

View File

@ -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;
}