Skip to content

Commit 493da2f

Browse files
committed
improve code quality & add --print expansion
1 parent 1f5c7e7 commit 493da2f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/wisp.wisp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@
2525

2626
(defn compile-file
2727
[path options]
28-
(map (fn [file] (with-stream-content
29-
(createReadStream file)
28+
(with-stream-content (createReadStream path)
3029
compile-string
31-
(conj {:source-uri file} options))) path))
30+
(conj {:source-uri path} options)))
31+
32+
(defn compile-files
33+
[paths options]
34+
(map #(compile-file % options) paths))
3235

3336
(defn compile-string
3437
[source options]
3538
(let [channel (or (:print options) :code)
3639
output (compile source options)
37-
content (if (= channel :code)
38-
(:code output)
39-
(JSON.stringify (get output channel) 2 2))]
40-
(if (:ast options) (map (fn [item]
41-
(.write process.stdout
42-
(str (pr-str item.form) "\n")))
43-
output.ast))
40+
content (cond
41+
(= channel :code) (:code output)
42+
(= channel :expansion) (reduce (fn [result item]
43+
(str result (pr-str (.-form item)) "\n"))
44+
"" (.-ast output))
45+
:else (JSON.stringify (get output channel) 2 2))]
4446
(if (and (:output options) (:source-uri options) content)
45-
(writeFileSync (path.join (:output options) ;; `join` relies on `path`
47+
(writeFileSync (path.join (.-output options) ;; `join` relies on `path`
4648
(str (basename (:source-uri options) ".wisp") ".js"))
4749
content)
4850
(.write process.stdout (or content "nil")))
49-
(if (:error output) (throw (:error output)))))
51+
(if (:error output) (throw (.-error output)))))
5052

5153
(defn with-stream-content
5254
[input resume options]
@@ -61,7 +63,7 @@
6163
[path]
6264
;; Loading module as main one, same way as nodejs does it:
6365
;; https://github.com/joyent/node/blob/master/lib/module.js#L489-493
64-
(Module._load (resolve (get path 0)) null true))
66+
(Module._load (resolve path) null true))
6567

6668
(defmacro ->
6769
[& operations]
@@ -80,15 +82,15 @@
8082
(.option "-r, --run" "Compile and execute the file")
8183
(.option "-c, --compile" "Compile to JavaScript and save as .js files")
8284
(.option "-i, --interactive" "Run an interactive wisp REPL")
83-
(.option "--debug, --print <type>" "Print debug information. Possible values are `form`, `ast` and `js-ast`")
85+
(.option "--debug, --print <type>" "Print debug information. Possible values are `expansion`,`forms`, `ast` and `js-ast`")
8486
(.option "-o, --output <dir>" "Output to specified directory")
8587
(.option "--no-map" "Disable source map generation")
8688
(.parse process.argv))
8789
(set! (aget options "no-map") (not (aget options "map"))) ;; commander auto translates to camelCase
88-
(cond options.run (run options.args)
90+
(cond options.run (run (get options.args 0))
8991
(not process.stdin.isTTY) (compile-stdin options)
9092
options.interactive (start-repl)
91-
options.compile (compile-file options.args options)
93+
options.compile (compile-files options.args options)
9294
options.args (run options.args)
9395
:else (start-repl)
9496
)))

0 commit comments

Comments
 (0)