aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2024-03-17 17:10:46 +0000
committerHayleigh Thompson <me@hayleigh.dev>2024-03-17 17:10:46 +0000
commitc67f11499310e874ac07844cabf4f05719da8aed (patch)
tree61178c79be2d8e6f4daf1d6b9df685e24c306e52
parenteb73e7622252e130095d41c0e901feba48ecfaf7 (diff)
downloadlustre-c67f11499310e874ac07844cabf4f05719da8aed.tar.gz
lustre-c67f11499310e874ac07844cabf4f05719da8aed.zip
:recycle: Use external templates for generated files.
-rw-r--r--priv/templates/entry-with-main.mjs3
-rw-r--r--priv/templates/entry-with-start.mjs4
-rw-r--r--priv/templates/index-with-lustre-ui.html19
-rw-r--r--priv/templates/index.html15
-rw-r--r--src/lustre/cli/dev.gleam31
-rw-r--r--src/lustre/cli/utils.gleam10
6 files changed, 64 insertions, 18 deletions
diff --git a/priv/templates/entry-with-main.mjs b/priv/templates/entry-with-main.mjs
new file mode 100644
index 0000000..c0c151c
--- /dev/null
+++ b/priv/templates/entry-with-main.mjs
@@ -0,0 +1,3 @@
+import { main } from "../dev/javascript/{app_name}/{app_name}.mjs";
+
+main();
diff --git a/priv/templates/entry-with-start.mjs b/priv/templates/entry-with-start.mjs
new file mode 100644
index 0000000..785cf0f
--- /dev/null
+++ b/priv/templates/entry-with-start.mjs
@@ -0,0 +1,4 @@
+import { start } from "../dev/javascript/lustre/lustre.mjs";
+import { main } from "../dev/javascript/{app_name}/{app_name}.mjs";
+
+start(main(), "#app", undefined);
diff --git a/priv/templates/index-with-lustre-ui.html b/priv/templates/index-with-lustre-ui.html
new file mode 100644
index 0000000..f7a51f0
--- /dev/null
+++ b/priv/templates/index-with-lustre-ui.html
@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+
+ <title>🚧 {app_name}</title>
+
+ <link
+ rel="stylesheet"
+ href="https://cdn.jsdelivr.net/gh/lustre-labs/ui/priv/styles.css"
+ />
+ <script type="module" src="./index.mjs"></script>
+ </head>
+
+ <body>
+ <div id="app"></div>
+ </body>
+</html>
diff --git a/priv/templates/index.html b/priv/templates/index.html
new file mode 100644
index 0000000..42bd2bf
--- /dev/null
+++ b/priv/templates/index.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+
+ <title>🚧 {app_name}</title>
+
+ <script type="module" src="./index.mjs"></script>
+ </head>
+
+ <body>
+ <div id="app"></div>
+ </body>
+</html>
diff --git a/src/lustre/cli/dev.gleam b/src/lustre/cli/dev.gleam
index 4f49ce1..d1bee68 100644
--- a/src/lustre/cli/dev.gleam
+++ b/src/lustre/cli/dev.gleam
@@ -11,7 +11,7 @@ import lustre/attribute.{attribute}
import lustre/cli/esbuild
import lustre/cli/project.{type Module}
import lustre/cli/step
-import lustre/cli/utils.{guard, keep, map, replace, try}
+import lustre/cli/utils.{guard, keep, map, replace, template, try}
import lustre/element
import lustre/element/html.{html}
import simplifile
@@ -45,23 +45,18 @@ pub fn run() -> Command(Nil) {
let _ = simplifile.create_directory_all(tempdir)
let entry =
- case is_app {
- True ->
- " import { start } from '../dev/javascript/lustre/lustre.mjs';
- import { main } from '../dev/javascript/${app_name}/${app_name}.mjs';
-
- start(main(), ${container_id});
- "
- False ->
- " import { main } from '../dev/javascript/${app_name}/${app_name}.mjs';
-
- main();
- "
- }
- |> string.replace("${app_name}", interface.name)
- |> string.replace("${container_id}", "app")
-
- let html = index_html(interface.name, "app", include_styles)
+ template(case is_app {
+ True -> "entry-with-start.mjs"
+ False -> "entry-with-main.mjs"
+ })
+ |> string.replace("{app_name}", interface.name)
+
+ let html =
+ template(case include_styles {
+ True -> "index-with-lustre-ui.html"
+ False -> "index.html"
+ })
+ |> string.replace("{app_name}", interface.name)
let assert Ok(_) = simplifile.write(tempdir <> "/entry.mjs", entry)
let assert Ok(_) = simplifile.write(tempdir <> "/index.html", html)
diff --git a/src/lustre/cli/utils.gleam b/src/lustre/cli/utils.gleam
index 6dbc110..a677e70 100644
--- a/src/lustre/cli/utils.gleam
+++ b/src/lustre/cli/utils.gleam
@@ -1,3 +1,13 @@
+import simplifile
+import gleam/erlang
+
+pub fn template(name: String) -> String {
+ let assert Ok(priv) = erlang.priv_directory("lustre")
+ let assert Ok(file) = simplifile.read(priv <> "/templates/" <> name)
+
+ file
+}
+
@external(erlang, "cli_ffi", "exec")
pub fn exec(
run command: String,