Skip to content

Commit

Permalink
Make watcher a MFA
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jul 17, 2021
1 parent e13a022 commit 36fbd7f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
23 changes: 16 additions & 7 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,24 @@ defmodule Phoenix.Endpoint do
the "watch" mode of the webpack build tool when the server starts.
You can configure it to whatever build tool or command you want:
[node: ["node_modules/webpack/bin/webpack.js", "--mode", "development",
"--watch-stdin"]]
[
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
"--watch",
"--watch-options-stdin"
]
]
The `:cd` and `:env` options can be given at the end of the list to customize
the watcher:
[node: [..., cd: "assets"]]
The `:cd` option can be used on a watcher to override the folder from
which the watcher will run. By default this will be the project's root:
`File.cwd!()`
A watcher can also be a module-function-args tuple that will be invoked accordingly:
[node: ["node_modules/webpack/bin/webpack.js", "--mode", "development",
"--watch-stdin", cd: "my_frontend"]]
[another: {Mod, :fun, [arg1, arg2]}]
* `:live_reload` - configuration for the live reload option.
Configuration requires a `:patterns` option which should be a list of
Expand Down
4 changes: 2 additions & 2 deletions lib/phoenix/endpoint/watcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ defmodule Phoenix.Endpoint.Watcher do
Task.start_link(__MODULE__, :watch, [to_string(cmd), args])
end

def watch(_cmd, args) when is_function(args, 0) do
def watch(_cmd, {mod, fun, args}) do
try do
args.()
apply(mod, fun, args)
catch
kind, reason ->
# The function returned a non-zero exit code.
Expand Down
2 changes: 1 addition & 1 deletion test/phoenix/endpoint/watcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Phoenix.Endpoint.WatcherTest do

test "starts watching and writes to stdio with fun" do
assert capture_io(fn ->
{:ok, pid} = Watcher.start_link({"echo", fn -> IO.puts("hello") end})
{:ok, pid} = Watcher.start_link({"echo", {IO, :puts, ["hello"]}})
ref = Process.monitor(pid)
assert_receive {:DOWN, ^ref, :process, ^pid, :normal}, 1000
end) == "hello\n"
Expand Down

0 comments on commit 36fbd7f

Please sign in to comment.