8000 working --output and --print · repos-javascript-compilers/wisp@607877b · GitHub
[go: up one dir, main page]

Skip to content

Commit 607877b

Browse files
committed
working --output and --print
1 parent 5beba7b commit 607877b

File tree

1 file changed

+8
-39
lines changed

1 file changed

+8
-39
lines changed

src/wisp.wisp

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns wisp.wisp
22
"Wisp program that reads wisp code from stdin and prints
33
compiled javascript code into stdout"
4-
(:require [fs :refer [createReadStream]]
4+
(:require [fs :refer [createReadStream writeFileSync]]
55
[path :refer [basename dirname join resolve]]
66
[module :refer [Module]]
77
[commander]
@@ -16,36 +16,6 @@
1616
[wisp.ast :refer [pr-str name]]
1717
[wisp.compiler :refer [compile]]))
1818

19-
20-
(defn flag?
21-
[param]
22-
;; HACK: Workaround for segfault #6691
23-
(identical? (subs param 0 2) (name :--)))
24-
25-
(defn flag->key
26-
[flag]
27-
(subs flag 2))
28-
29-
;; Just mungle all the `--param value` pairs into global *env* hash.
30-
(defn parse-params
31-
[params]
32-
(loop [input params
33-
output {}]
34-
(if (empty? input)
35-
output
36-
(let [name (first input)
37-
value (second input)]
38-
(if (flag? name)
39-
(if (or (nil? value) (flag? value))
40-
(recur (rest input)
41-
(assoc output (flag->key name) true))
42-
(recur (drop 2 input)
43-
(assoc output (flag->key name) value)))
44-
(recur (rest input)
45-
output))))))
46-
47-
48-
4919
(defn compile-stdin
5020
[options]
5121
(with-stream-content process.stdin
@@ -70,9 +40,11 @@
7040
(.write process.stdout
7141
(str (pr-str item.form) "\n")))
7242
output.ast))
73-
(if (:js-ast options) (.write process.stdout
74-
(str (pr-str (:body (:js-ast output))) "\n")))
75-
(.write process.stdout (or content "nil"))
43+
(if (and (:output options) (:source-uri options) content)
44+
(writeFileSync (path.join (:output options) ;; `join` relies on `path`
45+
(str (basename (:source-uri options) ".wisp") ".js"))
46+
content)
47+
(.write process.stdout (or content "nil")))
7648
(if (:error output) (throw (:error output)))))
7749

7850
(defn with-stream-content
@@ -107,14 +79,11 @@
10779
(.option "-r, --run" "Compile and execute the file")
10880
(.option "-c, --compile" "Compile to JavaScript and save as .js files")
10981
(.option "-i, --interactive" "Run an interactive wisp REPL")
110-
(.option "-p, --print" "Print compiled JavaScript")
82+
(.option "--debug, --print <type>" "Print debug information. Possible values are `form`, `ast` and `js-ast`")
11183
(.option "-o, --output <dir>" "Output to specified directory")
11284
(.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")
11585
(.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"))
86+
(set! (aget options "no-map") (not (aget options "map"))) ;; commander auto translates to camelCase
11887
(cond options.run (run options.args)
11988
(not process.stdin.isTTY) (compile-stdin options)
12089
options.interactive (start-repl)

0 commit comments

Comments
 (0)
0