diff --git a/elm.json b/elm.json index 86a26b2c..26a8092b 100644 --- a/elm.json +++ b/elm.json @@ -22,12 +22,7 @@ "Pages.Secrets", "Pages.Manifest", "Pages.Manifest.Category", - "Pages.Flags", - "Pages.Internal.Platform", - "Pages.Internal.Platform.Cli", - "Pages.Internal.Router", - "Pages.Internal.RoutePattern", - "Pages.Internal.NotFoundReason" + "Pages.Flags" ], "elm-version": "0.19.0 <= v < 0.20.0", "dependencies": { @@ -58,4 +53,4 @@ "avh4/elm-program-test": "3.1.0 <= v < 4.0.0", "elm-explorations/test": "1.2.2 <= v < 2.0.0" } -} +} \ No newline at end of file diff --git a/src/Pages/Internal/NotFoundReason.elm b/generator/src/Pages/Internal/NotFoundReason.elm similarity index 100% rename from src/Pages/Internal/NotFoundReason.elm rename to generator/src/Pages/Internal/NotFoundReason.elm diff --git a/src/Pages/Internal/Platform.elm b/generator/src/Pages/Internal/Platform.elm similarity index 100% rename from src/Pages/Internal/Platform.elm rename to generator/src/Pages/Internal/Platform.elm diff --git a/src/Pages/Internal/Platform/Cli.elm b/generator/src/Pages/Internal/Platform/Cli.elm similarity index 100% rename from src/Pages/Internal/Platform/Cli.elm rename to generator/src/Pages/Internal/Platform/Cli.elm diff --git a/src/Pages/Internal/Platform/Effect.elm b/generator/src/Pages/Internal/Platform/Effect.elm similarity index 100% rename from src/Pages/Internal/Platform/Effect.elm rename to generator/src/Pages/Internal/Platform/Effect.elm diff --git a/src/Pages/Internal/Platform/StaticResponses.elm b/generator/src/Pages/Internal/Platform/StaticResponses.elm similarity index 100% rename from src/Pages/Internal/Platform/StaticResponses.elm rename to generator/src/Pages/Internal/Platform/StaticResponses.elm diff --git a/src/Pages/Internal/Platform/ToJsPayload.elm b/generator/src/Pages/Internal/Platform/ToJsPayload.elm similarity index 100% rename from src/Pages/Internal/Platform/ToJsPayload.elm rename to generator/src/Pages/Internal/Platform/ToJsPayload.elm diff --git a/src/Pages/Internal/RoutePattern.elm b/generator/src/Pages/Internal/RoutePattern.elm similarity index 100% rename from src/Pages/Internal/RoutePattern.elm rename to generator/src/Pages/Internal/RoutePattern.elm diff --git a/src/Pages/Internal/Router.elm b/generator/src/Pages/Internal/Router.elm similarity index 100% rename from src/Pages/Internal/Router.elm rename to generator/src/Pages/Internal/Router.elm diff --git a/generator/src/codegen.js b/generator/src/codegen.js index e69e4077..8558d2d2 100644 --- a/generator/src/codegen.js +++ b/generator/src/codegen.js @@ -19,36 +19,17 @@ async function generate(basePath) { ensureDirSync("./elm-stuff/elm-pages/.elm-pages"); const uiFileContent = elmPagesUiFile(); + await Promise.all([ - fs.promises.copyFile( - path.join(__dirname, `./Page.elm`), - `./.elm-pages/Page.elm` - ), + copyToBoth("Page.elm"), + copyToBoth("SharedTemplate.elm"), + copyToBoth("SiteConfig.elm"), + + fs.promises.writeFile("./.elm-pages/Pages.elm", uiFileContent), fs.promises.copyFile( path.join(__dirname, `./elm-application.json`), `./elm-stuff/elm-pages/elm-application.json` ), - fs.promises.copyFile( - path.join(__dirname, `./Page.elm`), - `./elm-stuff/elm-pages/.elm-pages/Page.elm` - ), - fs.promises.copyFile( - path.join(__dirname, `./SharedTemplate.elm`), - `./.elm-pages/SharedTemplate.elm` - ), - fs.promises.copyFile( - path.join(__dirname, `./SharedTemplate.elm`), - `./elm-stuff/elm-pages/.elm-pages/SharedTemplate.elm` - ), - fs.promises.copyFile( - path.join(__dirname, `./SiteConfig.elm`), - `./.elm-pages/SiteConfig.elm` - ), - fs.promises.copyFile( - path.join(__dirname, `./SiteConfig.elm`), - `./elm-stuff/elm-pages/.elm-pages/SiteConfig.elm` - ), - fs.promises.writeFile("./.elm-pages/Pages.elm", uiFileContent), // write `Pages.elm` with cli interface fs.promises.writeFile( "./elm-stuff/elm-pages/.elm-pages/Pages.elm", @@ -69,8 +50,66 @@ async function generate(basePath) { fs.promises.writeFile("./.elm-pages/Route.elm", browserCode.routesModule), // write modified elm.json to elm-stuff/elm-pages/ copyModifiedElmJson(), + ...(await listFiles("./Pages/Internal")).map(copyToBoth), ]); +} +/** + * @param {string} moduleToCopy + * @returns { Promise } + */ +async function copyToBoth(moduleToCopy) { + await Promise.all([ + fs.promises.mkdir(path.dirname(path.join(`./.elm-pages/`, moduleToCopy)), { + recursive: true, + }), + fs.promises.mkdir( + path.dirname( + path.join(`./elm-stuff/elm-pages/.elm-pages/`, moduleToCopy) + ), + { recursive: true } + ), + ]); + await Promise.all([ + fs.promises.copyFile( + path.join(__dirname, moduleToCopy), + path.join(`./.elm-pages/`, moduleToCopy) + ), + fs.promises.copyFile( + path.join(__dirname, moduleToCopy), + path.join(`./elm-stuff/elm-pages/.elm-pages/`, moduleToCopy) + ), + ]); +} + +/** + * @param {string} dir + * @returns {Promise} + */ +async function listFiles(dir) { + const fullDir = path.join(__dirname, dir); + const files = await fs.promises.readdir(fullDir); + return merge( + await Promise.all( + files.flatMap(async (file_) => { + const file = path.join(dir, path.basename(file_)); + if ( + (await fs.promises.stat(path.join(__dirname, file))).isDirectory() + ) { + return await listFiles(file); + } else { + return [file]; + } + }) + ) + ); +} + +/** + * @param {any[]} arrays + */ +function merge(arrays) { + return [].concat.apply([], arrays); } module.exports = { generate };