From 8a6ecd02ae7e70928030a12c71119615a163aaf7 Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Sun, 17 Mar 2024 18:31:14 +0000 Subject: :sparkles: Let users provide a custom html file during dev. --- src/lustre/cli/dev.gleam | 54 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/lustre/cli/dev.gleam b/src/lustre/cli/dev.gleam index d2d4f5f..f7e12c2 100644 --- a/src/lustre/cli/dev.gleam +++ b/src/lustre/cli/dev.gleam @@ -4,6 +4,7 @@ import filepath import gleam/dict import gleam/io import gleam/package_interface.{type Type, Fn, Named, Variable} +import gleam/result import gleam/string import glint.{type Command, CommandInput} import glint/flag @@ -26,6 +27,7 @@ pub fn run() -> Command(Nil) { let assert Ok(port) = flag.get_string(flags, "port") let assert Ok(use_lustre_ui) = flag.get_bool(flags, "use-lustre-ui") let assert Ok(spa) = flag.get_bool(flags, "spa") + let custom_html = flag.get_string(flags, "html") let script = { use <- step.new("Building your project") @@ -49,12 +51,30 @@ pub fn run() -> Command(Nil) { }) |> string.replace("{app_name}", interface.name) - let html = - template(case use_lustre_ui { - True -> "index-with-lustre-ui.html" - False -> "index.html" - }) - |> string.replace("{app_name}", interface.name) + use html <- step.try( + case custom_html { + Ok(custom_html_path) -> + custom_html_path + |> simplifile.read + |> result.map_error(CouldntOpenCustomHtml(_, custom_html_path)) + |> result.map(string.replace( + _, + " + " + |> string.trim_right + + flag.string() + |> flag.description(description) + }) } // ERROR HANDLING -------------------------------------------------------------- @@ -117,6 +149,7 @@ pub fn run() -> Command(Nil) { type Error { BuildError BundleError(esbuild.Error) + CouldntOpenCustomHtml(error: simplifile.FileError, path: String) MainMissing(module: String) MainIncorrectType(module: String, got: Type) MainBadAppType(module: String, got: Type) @@ -129,6 +162,9 @@ fn explain(error: Error) -> Nil { BundleError(error) -> esbuild.explain(error) + CouldntOpenCustomHtml(_, path) -> io.println(" +I couldn't open the custom HTML file at `" <> path <> "`.") + MainMissing(module) -> io.println(" Module `" <> module <> "` doesn't have a public `main` function I can preview.") -- cgit v1.2.3