Skip to content

Commit fd6a579

Browse files
committed
renamed to ex_to_js. Updated cli
1 parent 43ddaf3 commit fd6a579

File tree

10 files changed

+159
-82
lines changed

10 files changed

+159
-82
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
/deps
33
erl_crash.dump
44
*.ez
5-
exjs
6-
node_modules
5+
ex2js
6+
node_modules
7+
priv/test_project/dest

lib/elixir_script.ex

Lines changed: 0 additions & 49 deletions
This file was deleted.

lib/elixir_script/cli.ex

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/ex_2_js.ex

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
defmodule ExToJS do
2+
require Logger
3+
4+
defmodule ParseError do
5+
defexception message: "Erroro while parsing SpiderMonkey JST"
6+
end
7+
8+
def parse(ast) do
9+
ExToJS.Parser.parse(ast)
10+
end
11+
12+
def parse_elixir(ex_code) do
13+
js_ast = ex_code
14+
|> Code.string_to_quoted!
15+
|> ExToJS.SpiderMonkey.parse
16+
|> Poison.encode!
17+
18+
[{ js_ast, "output.json" }]
19+
end
20+
21+
def parse_ex_files(path) do
22+
path
23+
|> Path.wildcard
24+
|> Enum.map(fn(x) -> parse_ex_file(x) end)
25+
end
26+
27+
def parse_ex_file(path) do
28+
js_ast = path
29+
|> File.read!
30+
|> Code.string_to_quoted!
31+
|> ExToJS.SpiderMonkey.parse
32+
|> Poison.encode!
33+
34+
{ js_ast, Path.basename(path, ".ex") <> ".json" }
35+
end
36+
37+
def convert_ast_to_js(js_ast) when is_list(js_ast) do
38+
Enum.map(js_ast, &convert_ast_to_js(&1))
39+
end
40+
41+
def convert_ast_to_js({ js_ast, path }) do
42+
case System.cmd(System.cwd() <> "/escodegen", [js_ast]) do
43+
{js_code, 0} ->
44+
{ js_code, Path.basename(path, ".json") <> ".js" }
45+
{error, _} ->
46+
raise ParseError, message: error
47+
end
48+
end
49+
50+
def write_to_files(list, destination) do
51+
Enum.each(list, fn(x) -> write_to_file(x, destination) end)
52+
end
53+
54+
def write_to_file({js, file_name}, destination) do
55+
file_name = Path.join([destination, file_name])
56+
57+
if !File.exists?(destination) do
58+
File.mkdir_p!(destination)
59+
end
60+
61+
File.write!(file_name, js)
62+
end
63+
end

lib/ex_to_js/cli.ex

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
defmodule ExToJS.CLI do
2+
def main(argv) do
3+
argv
4+
|> parse_args
5+
|> process
6+
end
7+
8+
defp parse_args(args) do
9+
switches = [ output: :binary, ast: :boolean, elixir: :boolean ]
10+
aliases = [ o: :output, a: :ast, ex: :elixir ]
11+
12+
parse = OptionParser.parse(args, switches: switches, aliases: aliases)
13+
14+
case parse do
15+
{ [ output: output, ast: true, elixir: true], [input], _ } -> { input, output, :ast, :elixir}
16+
{ [ output: output, ast: true], [input], _ } -> { input, output, :ast }
17+
{ [ ast: true ] , [input], _ } -> { input, :ast }
18+
19+
{ [ output: output, elixir: true], [input], _ } -> { input, output, :elixir}
20+
{ [ output: output], [input], _ } -> { input, output }
21+
22+
{ [elixir: true] , [input], _ } -> { input, :elixir }
23+
{ [] , [input], _ } -> { input }
24+
_ -> :help
25+
end
26+
27+
end
28+
29+
defp process({ input, output, :ast, :elixir }) do
30+
input
31+
|> ExToJS.parse_elixir
32+
|> ExToJS.write_to_file(output)
33+
end
34+
35+
defp process({ input, output, :ast }) do
36+
input
37+
|> ExToJS.parse_ex_files
38+
|> ExToJS.write_to_files(output)
39+
end
40+
41+
defp process({ input, :ast }) do
42+
input
43+
|> ExToJS.parse_ex_files
44+
|> Enum.map(fn({ast, _path})-> IO.puts(ast) end)
45+
end
46+
47+
defp process({ input, output, :elixir }) do
48+
input
49+
|> ExToJS.parse_elixir
50+
|> ExToJS.write_to_files(output)
51+
end
52+
53+
defp process({ input, :elixir }) do
54+
input
55+
|> ExToJS.parse_elixir
56+
|> Enum.map(fn({js, _path})-> IO.puts(js) end)
57+
end
58+
59+
defp process({ input, output }) do
60+
input
61+
|> ExToJS.parse_ex_files
62+
|> ExToJS.convert_ast_to_js
63+
|> ExToJS.write_to_files(output)
64+
end
65+
66+
defp process({ input }) do
67+
input
68+
|> ExToJS.parse_ex_files
69+
|> ExToJS.convert_ast_to_js
70+
|> Enum.map(fn({js, _path})-> IO.puts(js) end)
71+
end
72+
73+
defp process(:help) do
74+
IO.puts """
75+
usage: ex2js <input> [options]
76+
77+
options:
78+
79+
-o --output places output in the given directory or file
80+
-a --ast shows only produced spider monkey ast
81+
-ex --elixir read input as elixir code string
82+
"""
83+
end
84+
end
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule ElixirScript.SpiderMonkey do
2-
alias ElixirScript.SpiderMonkey.Nodes
1+
defmodule ExToJS.SpiderMonkey do
2+
alias ExToJS.SpiderMonkey.Nodes
33

44
def parse(nil) do
55
Nodes.literal(nil)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
defmodule ElixirScript.SpiderMonkey.Nodes do
2-
alias ElixirScript.SpiderMonkey
1+
defmodule ExToJS.SpiderMonkey.Nodes do
2+
alias ExToJS.SpiderMonkey
33

44
def literal(value) do
55
%{ type: "Literal", value: value }

mix.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ElixirScript.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :elixir_script,
5+
[app: :ex_to_js,
66
version: "0.0.1",
77
elixir: "~> 1.0",
88
escript: escript_config,
@@ -21,7 +21,7 @@ defmodule ElixirScript.Mixfile do
2121
end
2222

2323
defp escript_config do
24-
[main_module: ElixirScript.CLI, name: "exjs"]
24+
[main_module: ExToJS.CLI, name: "ex2js"]
2525
end
2626

2727
end

priv/test_project/src/hello.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/spider_monkey_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
defmodule ElixirScript.SpiderMonkey.Test do
1+
defmodule ExToJS.SpiderMonkey.Test do
22
use ExUnit.Case
3-
alias ElixirScript.SpiderMonkey
4-
alias ElixirScript.SpiderMonkey.Nodes
3+
alias ExToJS.SpiderMonkey
4+
alias ExToJS.SpiderMonkey.Nodes
55

66
test "parse nil" do
77
assert SpiderMonkey.parse(nil) == Nodes.literal(nil)

0 commit comments

Comments
 (0)