forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ex
More file actions
99 lines (83 loc) · 2.26 KB
/
Copy pathcli.ex
File metadata and controls
99 lines (83 loc) · 2.26 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
defmodule ElixirScript.CLI do
@moduledoc false
@switches [
output: :binary, elixir: :boolean, root: :binary,
help: :boolean
]
@aliases [
o: :output, ex: :elixir, h: :help, r: :root
]
def main(argv) do
argv
|> parse_args
|> process
end
def parse_args(args) do
parse = OptionParser.parse(args, switches: @switches, aliases: @aliases)
case parse do
{ [help: true] , _ , _ } -> :help
{ options , [input], _ } -> { input, options }
{ [], [], [] } -> :help
end
end
def process(:help) do
IO.write """
usage: ex2js <input> [options]
<input> path to elixir files or
the elixir code string if the -ex flag is used
options:
-o --output [path] places output at the given path
-ex --elixir read input as elixir code string
-r --root [path] root path for standard libs
-h --help this message
"""
end
def process({ input, options }) do
if options_contains_unknown_values(options) do
process(:help)
else
do_process(input, options)
end
end
def do_process(input, options) do
compile_opts = [
root: options[:root],
include_path: options[:output] != nil
]
compile_output = case options[:elixir] do
true ->
ElixirScript.compile(input, compile_opts)
_ ->
ElixirScript.compile_path(input, compile_opts)
end
case options[:output] do
nil ->
Enum.each(compile_output,
fn
({_path, code})-> IO.write(code)
(code)-> IO.write(code)
end)
output_path ->
Enum.each(compile_output, fn(x) ->
write_to_file(x, output_path)
end)
ElixirScript.copy_standard_libs_to_destination(output_path)
end
end
defp options_contains_unknown_values(options) do
Enum.any?(options, fn({key, _value}) ->
if key in Keyword.keys(@switches) do
false
else
true
end
end)
end
def write_to_file({ file_path, js_code }, destination) do
file_name = Path.join([destination, file_path])
if !File.exists?(Path.dirname(file_name)) do
File.mkdir_p!(Path.dirname(file_name))
end
File.write!(file_name, js_code)
end
end