Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lustre/dev.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
39 changes: 39 additions & 0 deletions src/lustre/dev/tools.gleam
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 4 additions & 3 deletions src/lustre_dev_tools/cli/build.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/lustre_dev_tools/cli/start.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/lustre_dev_tools/server.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/lustre_dev_tools/server/live_reload.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) ->
Expand All @@ -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)
Expand Down