aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2024-03-11 07:27:09 +0100
committerHayleigh Thompson <me@hayleigh.dev>2024-03-12 08:04:11 +0100
commit62b20febd3fa3d4a853778f665ad94c0238736b3 (patch)
treeab42ce9b93adb79a7cd8677a7dffef125c387442
parent7d3eafbffca65e3620e96e697ad48664df96dbcd (diff)
downloadlustre-62b20febd3fa3d4a853778f665ad94c0238736b3.tar.gz
lustre-62b20febd3fa3d4a853778f665ad94c0238736b3.zip
:bug: Fixed bug with shellout spawning zombie processes.
-rw-r--r--gleam.toml2
-rw-r--r--src/cli_ffi.erl28
-rw-r--r--src/lustre/cli/esbuild.gleam21
-rw-r--r--src/lustre/cli/project.gleam22
-rw-r--r--src/lustre/cli/utils.gleam7
5 files changed, 48 insertions, 32 deletions
diff --git a/gleam.toml b/gleam.toml
index abad8d7..a76a755 100644
--- a/gleam.toml
+++ b/gleam.toml
@@ -49,7 +49,6 @@ gleam_otp = "~> 0.9"
gleam_stdlib = "~> 0.34 or ~> 1.0"
glint = "0.16.0"
justin = "~> 1.0"
-shellout = "~> 1.6"
simplifile = "~> 1.4"
spinner = "~> 1.1"
tom = "~> 0.3"
@@ -58,3 +57,4 @@ gleam_package_interface = "~> 1.0"
[dev-dependencies]
birdie = "~> 1.0"
gleeunit = "~> 1.0"
+shellout = "~> 1.6"
diff --git a/src/cli_ffi.erl b/src/cli_ffi.erl
index 1c1b6f0..b23dec3 100644
--- a/src/cli_ffi.erl
+++ b/src/cli_ffi.erl
@@ -3,7 +3,8 @@
get_cpu/0,
get_esbuild/1,
get_os/0,
- unzip_esbuild/1
+ unzip_esbuild/1,
+ exec/3
]).
get_os() ->
@@ -47,3 +48,28 @@ unzip_esbuild(Zip) ->
{ok, Res} -> {error, Res};
{error, Err} -> {error, Err}
end.
+
+exec(Command, Args, Cwd) ->
+ Command_ = binary_to_list(Command),
+ Args_ = lists:map(fun(Arg) -> binary_to_list(Arg) end, Args),
+ Cwd_ = binary_to_list(Cwd),
+
+ Name = case Command_ of
+ "./" ++ _ -> {spawn_executable, Command_};
+ "/" ++ _ -> {spawn_executable, Command_};
+ _ -> {spawn_executable, os:find_executable(Command_)}
+ end,
+
+ Port = open_port(Name, [exit_status, binary, hide, stream,
+ {args, Args_},
+ {cd, Cwd_}
+ ]),
+
+ do_exec(Port, []).
+
+do_exec(Port, Acc) ->
+ receive
+ {Port, {data, Data}} -> do_exec(Port, [Data | Acc]);
+ {Port, {exit_status, 0}} -> {ok, lists:reverse(Acc)};
+ {Port, {exit_status, Code}} -> {error, {Code, lists:reverse(Acc)}}
+ end.
diff --git a/src/lustre/cli/esbuild.gleam b/src/lustre/cli/esbuild.gleam
index bf824b2..c8e4204 100644
--- a/src/lustre/cli/esbuild.gleam
+++ b/src/lustre/cli/esbuild.gleam
@@ -10,8 +10,7 @@ import gleam/set
import gleam/string
import lustre/cli/project
import lustre/cli/step.{type Step}
-import lustre/cli/utils.{keep, replace}
-import shellout
+import lustre/cli/utils.{exec, keep, replace}
import simplifile.{type FilePermissions, Execute, FilePermissions, Read, Write}
// COMMANDS --------------------------------------------------------------------
@@ -70,12 +69,7 @@ pub fn bundle(
use <- step.new("Bundling with esbuild")
use _ <- step.try(
- shellout.command(
- run: "./build/.lustre/bin/esbuild",
- in: root,
- with: options,
- opt: [],
- ),
+ exec(run: "./build/.lustre/bin/esbuild", in: root, with: options),
on_error: fn(pair) { BundleError(pair.1) },
)
@@ -93,12 +87,7 @@ pub fn serve(host: String, port: String) -> Step(Nil, Error) {
use <- step.done("\nStarting dev server at " <> host <> ":" <> port <> "...")
use _ <- step.try(
- shellout.command(
- run: "./build/.lustre/bin/esbuild",
- in: root,
- with: flags,
- opt: [],
- ),
+ exec(run: "./build/.lustre/bin/esbuild", in: root, with: flags),
on_error: fn(pair) { BundleError(pair.1) },
)
@@ -173,7 +162,7 @@ fn configure_node_tree_shaking(root) {
// effects.
//
// This is a really grim hack but it's the only way I've found to get esbuild to
- // ignore unused deps like `shellout` that import node stuff but aren't used in
+ // ignore unused deps like `glint` that imports node stuff but aren't used in
// app code.
let force_tree_shaking = "{ \"sideEffects\": false }"
let assert Ok(_) =
@@ -181,7 +170,7 @@ fn configure_node_tree_shaking(root) {
filepath.join(root, "build/dev/javascript/package.json"),
force_tree_shaking,
)
- let pure_deps = ["lustre", "glint", "simplifile", "shellout"]
+ let pure_deps = ["lustre", "glint", "simplifile"]
list.try_each(pure_deps, fn(dep) {
root
diff --git a/src/lustre/cli/project.gleam b/src/lustre/cli/project.gleam
index 2071588..f52a847 100644
--- a/src/lustre/cli/project.gleam
+++ b/src/lustre/cli/project.gleam
@@ -11,8 +11,7 @@ import gleam/package_interface.{type Type, Fn, Named, Tuple, Variable}
import gleam/pair
import gleam/result
import gleam/string
-import lustre/cli/utils.{map, try}
-import shellout
+import lustre/cli/utils.{exec, map, try}
import simplifile
import tom.{type Toml}
@@ -40,12 +39,7 @@ pub type Function {
///
pub fn build() -> Result(Nil, String) {
use _ <- try(
- shellout.command(
- run: "gleam",
- in: ".",
- with: ["build", "--target=js"],
- opt: [],
- ),
+ exec(run: "gleam", in: ".", with: ["build", "--target=js"]),
on_error: map(with: pair.second),
)
@@ -57,12 +51,12 @@ pub fn interface() -> Result(Interface, String) {
let out = filepath.join(dir, "package-interface.json")
use _ <- try(
- shellout.command(
- run: "gleam",
- in: ".",
- with: ["export", "package-interface", "--out", out],
- opt: [],
- ),
+ exec(run: "gleam", in: ".", with: [
+ "export",
+ "package-interface",
+ "--out",
+ out,
+ ]),
on_error: map(with: pair.second),
)
diff --git a/src/lustre/cli/utils.gleam b/src/lustre/cli/utils.gleam
index f001a35..6dbc110 100644
--- a/src/lustre/cli/utils.gleam
+++ b/src/lustre/cli/utils.gleam
@@ -1,3 +1,10 @@
+@external(erlang, "cli_ffi", "exec")
+pub fn exec(
+ run command: String,
+ with args: List(String),
+ in in: String,
+) -> Result(String, #(Int, String))
+
// CHAINING RESULTS ------------------------------------------------------------
pub fn try(