8000 use commander.js for args handling; add --ast and --js-ast for debugg… · repos-javascript-compilers/wisp@5beba7b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5beba7b

Browse files
committed
use commander.js for args handling; add --ast and --js-ast for debugging purposes
1 parent 4a3853d commit 5beba7b

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"escodegen": "git://github.com/Constellation/escodegen.git#master",
51-
"base64-encode": "~1.0.1"
51+
"base64-encode": "~1.0.1",
52+
"commander": ">=2.2.0"
5253
}
5354
}

src/wisp.wisp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
(:require [fs :refer [createReadStream]]
55
[path :refer [basename dirname join resolve]]
66
[module :refer [Module]]
7+
[commander]
78

89
[wisp.string :refer [split join upper-case replace]]
9-
[wisp.sequence :refer [first second last count reduce rest
10+
[wisp.sequence :refer [first second last count reduce rest map
1011
conj partition assoc drop empty?]]
1112

1213
[wisp.repl :refer [start] :rename {start start-repl}]
@@ -53,9 +54,10 @@
5354

5455
(defn compile-file
5556
[path options]
56-
(with-stream-content (createReadStream path)
57+
(map (fn [file] (with-stream-content
58+
(createReadStream file)
5759
compile-string
58-
(conj {:source-uri path} options)))
60+
(conj {:source-uri file} options))) path))
5961

6062
(defn compile-string
6163
[source options]
@@ -64,6 +66,12 @@
6466
content (if (= channel :code)
6567
(:code output)
6668
(JSON.stringify (get output channel) 2 2))]
69+
(if (:ast options) (map (fn [item]
70+
(.write process.stdout
71+
(str (pr-str item.form) "\n")))
72+
output.ast))
73+
(if (:js-ast options) (.write process.stdout
74+
(str (pr-str (:body (:js-ast output))) "\n")))
6775
(.write process.stdout (or content "nil"))
6876
(if (:error output) (throw (:error output)))))
6977

@@ -80,15 +88,37 @@
8088
[path]
8189
;; Loading module as main one, same way as nodejs does it:
8290
;; https://github.com/joyent/node/blob/master/lib/module.js#L489-493
83-
(Module._load (resolve path) null true))
91+
(Module._load (resolve (get path 0)) null true))
8492

93+
(defmacro ->
94+
[& operations]
95+
(reduce
96+
(fn [form operation]
97+
(cons (first operation)
98+
(cons form (rest operation))))
99+
(first operations)
100+
(rest operations)))
85101

86102
(defn main
87103
[]
88-
(let [options (parse-params (drop 2 process.argv))]
89-
(cond (:run options) (run (:run options))
104+
(let [options commander]
105+
(-> options
106+
(.usage "[options] <file ...>")
107+
(.option "-r, --run" "Compile and execute the file")
108+
(.option "-c, --compile" "Compile to JavaScript and save as .js files")
109+
(.option "-i, --interactive" "Run an interactive wisp REPL")
110+
(.option "-p, --print" "Print compiled JavaScript")
111+
(.option "-o, --output <dir>" "Output to specified directory")
112+
(.option "--no-map" "Disable source map generation")
113+
(.option "--ast" "Print the wisp AST produced by the reader")
114+
(.option "--js-ast" "Print the JavaScript AST produced by the compiler")
115+
(.parse process.argv))
116+
(set! (aget options "no-map") (aget options "noMap")) ;; commander auto translates to camelCase
117+
(set! (aget options "js-ast") (aget options "jsAst"))
118+
(cond options.run (run options.args)
90119
(not process.stdin.isTTY) (compile-stdin options)
91-
(< (count process.argv) 3) (start-repl)
92-
(and (= (count process.argv) 3)
93-
(not (flag? (last process.argv)))) (run (last process.argv))
94-
(:compile options) (compile-file (:compile options) options))))
120+
options.interactive (start-repl)
121+
options.compile (compile-file options.args options)
122+
options.args (run options.args)
123+
:else (start-repl)
124+
)))

0 commit comments

Comments
 (0)
0