Skip to content

Commit 830d246

Browse files
committed
Trying to add watcher
1 parent 4969005 commit 830d246

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

lib/elixir_script/cli.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule ElixirScript.CLI do
66
@switches [
77
output: :binary, elixir: :boolean, root: :binary,
88
help: :boolean, core_path: :binary, std_lib: :binary,
9-
full_build: :boolean, version: :boolean
9+
full_build: :boolean, version: :boolean, watch: :boolean
1010
]
1111

1212
@aliases [
@@ -63,6 +63,7 @@ defmodule ElixirScript.CLI do
6363
end
6464

6565
def process({ input, options }) do
66+
IO.inspect(options)
6667
if options_contains_unknown_values(options) do
6768
process(:help)
6869
else
@@ -71,6 +72,8 @@ defmodule ElixirScript.CLI do
7172
end
7273

7374
def do_process(input, options) do
75+
{watch, options} = Keyword.pop(options, :watch, false)
76+
7477
compile_opts = %{
7578
root: options[:root],
7679
include_path: true,
@@ -84,6 +87,11 @@ defmodule ElixirScript.CLI do
8487
ElixirScript.compile(input, compile_opts)
8588
_ ->
8689
ElixirScript.compile_path(input, compile_opts)
90+
91+
if watch do
92+
ElixirScript.Watcher.start_link(input, compile_opts)
93+
:timer.sleep :infinity
94+
end
8795
end
8896
end
8997

lib/elixir_script/watcher.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule ElixirScript.Watcher do
2+
use GenServer
3+
require Logger
4+
5+
def start_link(input, options) do
6+
GenServer.start_link(__MODULE__, [input: input, options: options])
7+
end
8+
9+
def init(args) do
10+
{:ok, _} = Application.ensure_all_started(:elixir_script)
11+
:fs.subscribe()
12+
{:ok, args}
13+
end
14+
15+
def handle_info({_pid, {:fs, :file_event}, {path, event}}, state) do
16+
Logger.debug "File changed: #{path}"
17+
ElixirScript.compile_path(state[:input], state[:options])
18+
{:noreply, state}
19+
end
20+
end

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ defmodule ElixirScript.Mixfile do
2222

2323
def application do
2424
[
25-
applications: [:logger, :estree]
25+
applications: [:logger, :estree, :fs]
2626
]
2727
end
2828

2929
defp deps do
3030
[
3131
{:estree, "~> 2.3" },
32+
{:fs, "~> 0.9.1"},
3233
{:earmark, "~> 0.2", only: :dev },
3334
{:ex_doc, "~> 0.11", only: :dev },
3435
{:excoveralls, "~> 0.4", only: :test},

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"ex_doc": {:hex, :ex_doc, "0.11.4"},
99
"excoveralls": {:hex, :excoveralls, "0.5.1"},
1010
"exjsx": {:hex, :exjsx, "3.2.0"},
11+
"fs": {:hex, :fs, "0.9.2"},
1112
"hackney": {:hex, :hackney, "1.4.8"},
1213
"idna": {:hex, :idna, "1.0.3"},
1314
"inflex": {:hex, :inflex, "1.5.0"},

0 commit comments

Comments
 (0)