-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathwatcher.ex
More file actions
49 lines (39 loc) · 1.06 KB
/
watcher.ex
File metadata and controls
49 lines (39 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule ElixirScript.Watcher do
use GenServer
require Logger
@moduledoc """
Watches the input folder for changes and calls the ElixirScript compiler
"""
def start_link(input, options) do
GenServer.start_link(__MODULE__, [input: input, options: options])
end
def init(args) do
{:ok, _} = Application.ensure_all_started(:elixir_script)
{:ok, _} = Application.ensure_all_started(:fs)
:fs.subscribe()
{:ok, args}
end
def handle_info({_pid, {:fs, :file_event}, {path, event}}, state) do
try do
if input_changed?(to_string(path), state) do
Logger.debug "Event: #{inspect event} Path: #{path}"
ElixirScript.compile_path(state[:input], state[:options])
end
rescue
x ->
Logger.error(x.message)
end
{:noreply, state}
end
defp input_changed?(path, state) do
file = Path.basename(path)
case file do
"." <> _ ->
false
_ ->
Enum.any?(List.wrap(state[:input]), fn(x) ->
path == Path.absname(Path.join([x, file]))
end)
end
end
end