2019-08-20 01:20:20 +03:00
|
|
|
const fs = require("fs");
|
|
|
|
const runElm = require("./compile-elm.js");
|
|
|
|
const copyModifiedElmJson = require("./rewrite-elm-json.js");
|
2020-05-03 03:22:53 +03:00
|
|
|
const { elmPagesCliFile, elmPagesUiFile } = require("./elm-file-constants.js");
|
2019-09-24 02:11:59 +03:00
|
|
|
const path = require("path");
|
2020-01-04 01:48:42 +03:00
|
|
|
const { ensureDirSync, deleteIfExists } = require('./file-helpers.js')
|
2020-05-03 21:46:11 +03:00
|
|
|
let wasEqualBefore = false
|
2020-01-04 01:48:42 +03:00
|
|
|
|
2019-08-20 01:20:20 +03:00
|
|
|
|
2019-09-06 06:33:11 +03:00
|
|
|
module.exports = function run(
|
2019-11-13 07:14:34 +03:00
|
|
|
mode,
|
2019-09-06 06:33:11 +03:00
|
|
|
staticRoutes,
|
2020-04-23 04:00:46 +03:00
|
|
|
markdownContent
|
2019-09-06 06:33:11 +03:00
|
|
|
) {
|
2019-09-25 01:07:21 +03:00
|
|
|
ensureDirSync("./elm-stuff");
|
2020-05-04 03:38:34 +03:00
|
|
|
ensureDirSync("./gen");
|
2019-09-25 01:07:21 +03:00
|
|
|
ensureDirSync("./elm-stuff/elm-pages");
|
2019-08-20 01:20:20 +03:00
|
|
|
|
2020-01-04 01:48:42 +03:00
|
|
|
// prevent compilation errors if migrating from previous elm-pages version
|
|
|
|
deleteIfExists("./elm-stuff/elm-pages/Pages/ContentCache.elm");
|
|
|
|
deleteIfExists("./elm-stuff/elm-pages/Pages/Platform.elm");
|
2020-04-20 04:32:16 +03:00
|
|
|
|
2020-01-04 01:48:42 +03:00
|
|
|
|
2020-05-03 03:22:53 +03:00
|
|
|
const uiFileContent = elmPagesUiFile(staticRoutes, markdownContent)
|
2020-05-03 21:46:11 +03:00
|
|
|
|
|
|
|
// TODO should just write it once, but webpack doesn't seem to pick up the changes
|
|
|
|
// so this wasEqualBefore code causes it to get written twice to make sure the changes come through for HMR
|
|
|
|
if (wasEqualBefore) {
|
|
|
|
fs.writeFileSync(
|
|
|
|
"./gen/Pages.elm",
|
|
|
|
uiFileContent
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (global.previousUiFileContent === uiFileContent) {
|
|
|
|
wasEqualBefore = false
|
|
|
|
} else {
|
|
|
|
wasEqualBefore = true
|
2020-05-03 03:22:53 +03:00
|
|
|
fs.writeFileSync(
|
|
|
|
"./gen/Pages.elm",
|
|
|
|
uiFileContent
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
global.previousUiFileContent = uiFileContent
|
|
|
|
|
2019-09-16 07:51:16 +03:00
|
|
|
// write `Pages.elm` with cli interface
|
2019-08-20 19:31:04 +03:00
|
|
|
fs.writeFileSync(
|
2019-09-16 07:51:16 +03:00
|
|
|
"./elm-stuff/elm-pages/Pages.elm",
|
2020-04-20 04:32:16 +03:00
|
|
|
elmPagesCliFile(staticRoutes, markdownContent)
|
2019-08-20 19:31:04 +03:00
|
|
|
);
|
2019-08-20 01:20:20 +03:00
|
|
|
|
|
|
|
// write modified elm.json to elm-stuff/elm-pages/
|
|
|
|
copyModifiedElmJson();
|
|
|
|
|
|
|
|
// run Main.elm from elm-stuff/elm-pages with `runElm`
|
2020-04-23 04:00:46 +03:00
|
|
|
return runElm(mode);
|
2019-08-20 01:20:20 +03:00
|
|
|
};
|