Add boilerplate for elm-codegen.

This commit is contained in:
Dillon Kearns 2022-09-12 10:16:00 -07:00
parent a0fa6c2188
commit ba6f0903ce
9 changed files with 176 additions and 7 deletions

1
.gitignore vendored
View File

@ -10,4 +10,5 @@ tests/VerifyExamples/
cypress/videos
cypress/screenshots
.idea
generated/

2
codegen/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
Gen/

24
codegen/Generate.elm Normal file
View File

@ -0,0 +1,24 @@
module Generate exposing (main)
{-| -}
import Elm
import Elm.Annotation as Type
import Gen.CodeGen.Generate as Generate
import Gen.Helper
main : Program {} () ()
main =
Generate.run
[ file
]
file : Elm.File
file =
Elm.file [ "Route" ]
[ Elm.customType "Route"
[ Elm.variant "Index"
]
]

13
codegen/elm.codegen.json Normal file
View File

@ -0,0 +1,13 @@
{
"elm-codegen-version": "0.2.0",
"codegen-helpers": {
"packages": {
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"local": [
"codegen/helpers/",
"src/"
]
}
}

34
codegen/elm.json Normal file
View File

@ -0,0 +1,34 @@
{
"type": "application",
"source-directories": [
"."
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"mdgriffith/elm-codegen": "2.0.0"
},
"indirect": {
"elm/parser": "1.1.0",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/basics-extra": "4.1.0",
"elm-community/list-extra": "8.6.0",
"miniBill/elm-unicode": "1.0.2",
"rtfeldman/elm-hex": "1.0.0",
"stil4m/elm-syntax": "7.2.9",
"stil4m/structured-writer": "1.0.3",
"the-sett/elm-pretty-printer": "3.0.0"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}

View File

@ -16,8 +16,11 @@ global.builtAt = new Date();
* @param {string} basePath
*/
async function generate(basePath) {
const cliCode = generateTemplateModuleConnector(basePath, "cli");
const browserCode = generateTemplateModuleConnector(basePath, "browser");
const cliCode = await generateTemplateModuleConnector(basePath, "cli");
const browserCode = await generateTemplateModuleConnector(
basePath,
"browser"
);
ensureDirSync("./elm-stuff");
ensureDirSync("./.elm-pages");
ensureDirSync("./gen");
@ -46,10 +49,13 @@ async function generate(basePath) {
),
fs.promises.writeFile(
"./elm-stuff/elm-pages/.elm-pages/Route.elm",
cliCode.routesModule
cliCode.routesModuleNew
),
fs.promises.writeFile("./.elm-pages/Main.elm", browserCode.mainModule),
fs.promises.writeFile("./.elm-pages/Route.elm", browserCode.routesModule),
fs.promises.writeFile(
"./.elm-pages/Route.elm",
browserCode.routesModuleNew
),
writeFetcherModules("./.elm-pages", browserCode.fetcherModules),
writeFetcherModules(
"./elm-stuff/elm-pages/client/.elm-pages",
@ -83,7 +89,10 @@ async function newCopyBoth(modulePath) {
}
async function generateClientFolder(basePath) {
const browserCode = generateTemplateModuleConnector(basePath, "browser");
const browserCode = await generateTemplateModuleConnector(
basePath,
"browser"
);
const uiFileContent = elmPagesUiFile();
ensureDirSync("./elm-stuff/elm-pages/client/app");
ensureDirSync("./elm-stuff/elm-pages/client/.elm-pages");
@ -102,7 +111,7 @@ async function generateClientFolder(basePath) {
);
await fs.promises.writeFile(
"./elm-stuff/elm-pages/client/.elm-pages/Route.elm",
browserCode.routesModule
browserCode.routesModuleNew
);
await fs.promises.writeFile(
"./elm-stuff/elm-pages/client/.elm-pages/Pages.elm",

View File

@ -2,12 +2,14 @@ const globby = require("globby");
const path = require("path");
const mm = require("micromatch");
const routeHelpers = require("./route-codegen-helpers");
const { runElmCodegenInstall } = require("./elm-codegen");
const { compileCliApp } = require("./compile-elm");
/**
* @param {string} basePath
* @param {'browser' | 'cli'} phase
*/
function generateTemplateModuleConnector(basePath, phase) {
async function generateTemplateModuleConnector(basePath, phase) {
const templates = globby.sync(["app/Route/**/*.elm"], {}).map((file) => {
const captures = mm.capture("app/Route/**/*.elm", file);
if (captures) {
@ -36,6 +38,7 @@ function generateTemplateModuleConnector(basePath, phase) {
],
};
}
const routesModuleNew = await runElmCodegenCli(templates);
return {
mainModule: `port module Main exposing (..)
@ -997,6 +1000,7 @@ decodeBytes bytesDecoder items =
-- Lamdera.Wire3.bytesDecodeStrict bytesDecoder items
|> Result.fromMaybe "Decoding error"
`,
routesModuleNew,
routesModule: `module Route exposing (baseUrlAsPath, Route(..), link, matchers, routeToPath, toLink, urlToRoute, toPath, redirectTo, toString)
{-|
@ -1142,6 +1146,42 @@ redirectTo route =
};
}
async function runElmCodegenCli(templates) {
// await runElmCodegenInstall();
await compileCliApp(
// { debug: true },
{},
`Generate.elm`,
path.join(process.cwd(), "elm-stuff/elm-pages-codegen.js"),
path.join(__dirname, "../../codegen"),
path.join(process.cwd(), "elm-stuff/elm-pages-codegen.js")
);
// TODO use uncached require here to prevent stale code from running
const promise = new Promise((resolve, reject) => {
const elmPagesCodegen = require(path.join(
process.cwd(),
"./elm-stuff/elm-pages-codegen.js"
)).Elm.Generate;
const app = elmPagesCodegen.init({ flags: {} });
if (app.ports.onSuccessSend) {
app.ports.onSuccessSend.subscribe(resolve);
}
if (app.ports.onInfoSend) {
app.ports.onInfoSend.subscribe((info) => console.log(info));
}
if (app.ports.onFailureSend) {
app.ports.onFailureSend.subscribe(reject);
}
});
const filesToGenerate = await promise;
return filesToGenerate[0].contents;
}
function emptyRouteParams(name) {
return routeHelpers.parseRouteParams(name).length === 0;
}

45
package-lock.json generated
View File

@ -43,6 +43,7 @@
"@types/node": "12.20.12",
"@types/serve-static": "^1.15.0",
"cypress": "^10.6.0",
"elm-codegen": "^0.2.0",
"elm-optimize-level-2": "^0.1.5",
"elm-review": "^2.7.4",
"elm-test": "^0.19.1-revision9",
@ -1680,6 +1681,30 @@
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
},
"node_modules/elm-codegen": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/elm-codegen/-/elm-codegen-0.2.0.tgz",
"integrity": "sha512-JXEbEl8wctVf47uH8M9gE5YF59e7YcSsBjofsPihepRSpPya+IYcva0qANlmNp1/N/p4T0HXXPbSiI3ake47VA==",
"dev": true,
"dependencies": {
"chalk": "^4.1.1",
"chokidar": "^3.5.1",
"commander": "^8.3.0",
"node-elm-compiler": "^5.0.6"
},
"bin": {
"elm-codegen": "bin/elm-codegen"
}
},
"node_modules/elm-codegen/node_modules/commander": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
"integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
"dev": true,
"engines": {
"node": ">= 12"
}
},
"node_modules/elm-doc-preview": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/elm-doc-preview/-/elm-doc-preview-5.0.5.tgz",
@ -8111,6 +8136,26 @@
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
},
"elm-codegen": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/elm-codegen/-/elm-codegen-0.2.0.tgz",
"integrity": "sha512-JXEbEl8wctVf47uH8M9gE5YF59e7YcSsBjofsPihepRSpPya+IYcva0qANlmNp1/N/p4T0HXXPbSiI3ake47VA==",
"dev": true,
"requires": {
"chalk": "^4.1.1",
"chokidar": "^3.5.1",
"commander": "^8.3.0",
"node-elm-compiler": "^5.0.6"
},
"dependencies": {
"commander": {
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
"integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
"dev": true
}
}
},
"elm-doc-preview": {
"version": "5.0.5",
"resolved": "https://registry.npmjs.org/elm-doc-preview/-/elm-doc-preview-5.0.5.tgz",

View File

@ -54,6 +54,7 @@
"@types/node": "12.20.12",
"@types/serve-static": "^1.15.0",
"cypress": "^10.6.0",
"elm-codegen": "^0.2.0",
"elm-optimize-level-2": "^0.1.5",
"elm-review": "^2.7.4",
"elm-test": "^0.19.1-revision9",