diff --git a/src/lustre/dev.gleam b/src/lustre/dev.gleam index 1ab80cb..edf6eaa 100644 --- a/src/lustre/dev.gleam +++ b/src/lustre/dev.gleam @@ -199,9 +199,9 @@ pub fn main() { |> glint.path_help(["add"], add.description) |> glint.add(at: ["add", "esbuild"], do: add.esbuild()) |> glint.add(at: ["add", "tailwind"], do: add.tailwind()) - |> glint.add(at: ["build"], do: build.app()) - |> glint.add(at: ["build", "app"], do: build.app()) + |> glint.add(at: ["build"], do: build.app(fn(){Nil})) + |> glint.add(at: ["build", "app"], do: build.app(fn(){Nil})) |> glint.add(at: ["build", "component"], do: build.component()) - |> glint.add(at: ["start"], do: start.run()) + |> glint.add(at: ["start"], do: start.run(fn(){Nil})) |> glint.run(args) } diff --git a/src/lustre/dev/tools.gleam b/src/lustre/dev/tools.gleam new file mode 100644 index 0000000..e7c791d --- /dev/null +++ b/src/lustre/dev/tools.gleam @@ -0,0 +1,39 @@ +import lustre_dev_tools/cli/start +import glint +import gleam/option.{type Option, Some, None} +import gleam/int.{to_string} + +pub opaque type Config { + Config( + port: Option(Int), + build_hook: Option(fn()->Nil), + ) +} + +pub fn new_config() -> Config { + Config(None, None) +} + +pub fn with_port(cfg: Config, port: Int) -> Config { + Config(..cfg, port: Some(port)) +} + +pub fn with_build_hook(cfg: Config, build_hook: fn()->Nil) -> Config { + Config(..cfg, build_hook: Some(build_hook)) +} + +pub fn run(cfg: Config) -> Nil { + let options = { + case cfg.port { + Some(port) -> ["--port", to_string(port)] + None -> [] + } + } + let build_hook = case cfg.build_hook { + Some(f) -> f + None -> fn(){Nil} + } + glint.new() + |> glint.add(at: [], do: start.run(build_hook)) + |> glint.run(options) +} diff --git a/src/lustre_dev_tools/cli/build.gleam b/src/lustre_dev_tools/cli/build.gleam index 22751a7..2c233d0 100644 --- a/src/lustre_dev_tools/cli/build.gleam +++ b/src/lustre_dev_tools/cli/build.gleam @@ -30,7 +30,7 @@ integration with other build tools. // COMMANDS -------------------------------------------------------------------- -pub fn app() -> Command(Nil) { +pub fn app(build_hook: fn()->Nil) -> Command(Nil) { let description = " Build and bundle an entire Lustre application. The generated JavaScript module @@ -62,7 +62,7 @@ JavaScript module for you to host or distribute. use entry <- do(cli.get_string("entry", project_name, ["build"], entry)) - do_app(entry, minify, detect_tailwind) + do_app(entry, minify, detect_tailwind, build_hook) } case cli.run(script, flags) { @@ -75,9 +75,10 @@ pub fn do_app( entry_module: String, minify: Bool, detect_tailwind: Bool, + build_hook: fn()->Nil ) -> Cli(Nil) { use <- cli.log("Building your project") - + build_hook() use <- cli.success("Project compiled successfully") use <- cli.log("Creating the bundle entry file") let root = project.root() diff --git a/src/lustre_dev_tools/cli/start.gleam b/src/lustre_dev_tools/cli/start.gleam index c7d7457..50a1770 100644 --- a/src/lustre_dev_tools/cli/start.gleam +++ b/src/lustre_dev_tools/cli/start.gleam @@ -16,7 +16,7 @@ import simplifile // COMMANDS -------------------------------------------------------------------- -pub fn run() -> Command(Nil) { +pub fn run(build_hook: fn()->Nil) -> Command(Nil) { let description = " Start a development server for your Lustre project. This command will compile your @@ -44,9 +44,9 @@ application and serve it on a local server. use entry <- do(cli.get_string("entry", project_name, ["build"], entry)) use _ <- do(check_otp_version()) - use _ <- do(build.do_app(entry, False, detect_tailwind)) + use _ <- do(build.do_app(entry, False, detect_tailwind, build_hook)) use _ <- do(prepare_html()) - use _ <- do(server.start(entry, port)) + use _ <- do(server.start(entry, port, build_hook)) cli.return(Nil) } diff --git a/src/lustre_dev_tools/server.gleam b/src/lustre_dev_tools/server.gleam index 7b4dbc4..3d97d7d 100644 --- a/src/lustre_dev_tools/server.gleam +++ b/src/lustre_dev_tools/server.gleam @@ -21,7 +21,7 @@ import simplifile import wisp import wisp/wisp_mist -pub fn start(entry: String, port: Int) -> Cli(Nil) { +pub fn start(entry: String, port: Int, build_hook: fn()->Nil) -> Cli(Nil) { let assert Ok(cwd) = cmd.cwd() let assert Ok(root) = filepath.expand(filepath.join(cwd, project.root())) @@ -41,7 +41,7 @@ at https://github.com/lustre-labs/dev-tools/issues/new } use flags <- do(cli.get_flags()) - use make_socket <- try(live_reload.start(entry, root, flags)) + use make_socket <- try(live_reload.start(entry, root, flags, build_hook)) use _ <- try( fn(req: Request(mist.Connection)) -> Response(mist.ResponseData) { use <- proxy.middleware(req, proxy) diff --git a/src/lustre_dev_tools/server/live_reload.gleam b/src/lustre_dev_tools/server/live_reload.gleam index 774b82e..0f4a057 100644 --- a/src/lustre_dev_tools/server/live_reload.gleam +++ b/src/lustre_dev_tools/server/live_reload.gleam @@ -55,8 +55,9 @@ pub fn start( entry: String, root: String, flags: glint.Flags, + build_hook: fn()->Nil, ) -> Result(fn(Request(mist.Connection)) -> Response(mist.ResponseData), Error) { - use watcher <- result.try(start_watcher(entry, root, flags)) + use watcher <- result.try(start_watcher(entry, root, flags, build_hook)) let make_socket = mist.websocket( _, loop_socket, @@ -140,10 +141,11 @@ fn start_watcher( entry: String, root: String, flags: glint.Flags, + build_hook: fn()->Nil, ) -> Result(Subject(WatcherMsg), Error) { actor.start_spec( actor.Spec(fn() { init_watcher(root) }, 1000, fn(msg, state) { - loop_watcher(msg, state, entry, flags) + loop_watcher(msg, state, entry, flags, build_hook) }), ) |> result.map_error(CannotStartFileWatcher) @@ -199,6 +201,7 @@ fn loop_watcher( state: WatcherState, entry: String, flags: glint.Flags, + build_hook: fn()->Nil ) -> actor.Next(WatcherMsg, WatcherState) { case msg { Add(client) -> @@ -222,7 +225,7 @@ fn loop_watcher( glint.get_flag(_, flag.detect_tailwind()), ), ) - use _ <- cli.do(build.do_app(entry, False, detect_tailwind)) + use _ <- cli.do(build.do_app(entry, False, detect_tailwind, build_hook)) use _ <- cli.do(cli.unmute()) cli.return(Nil)