|
1 | 1 | (ns wisp.wisp |
2 | 2 | "Wisp program that reads wisp code from stdin and prints |
3 | 3 | compiled javascript code into stdout" |
4 | | - (:require [fs :refer [createReadStream]] |
| 4 | + (:require [fs :refer [createReadStream writeFileSync]] |
5 | 5 | [path :refer [basename dirname join resolve]] |
6 | 6 | [module :refer [Module]] |
7 | 7 | [commander] |
|
16 | 16 | [wisp.ast :refer [pr-str name]] |
17 | 17 | [wisp.compiler :refer [compile]])) |
18 | 18 |
|
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 | | - |
49 | 19 | (defn compile-stdin |
50 | 20 | [options] |
51 | 21 | (with-stream-content process.stdin |
|
70 | 40 | (.write process.stdout |
71 | 41 | (str (pr-str item.form) "\n"))) |
72 | 42 | 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"))) |
76 | 48 | (if (:error output) (throw (:error output))))) |
77 | 49 |
|
78 | 50 | (defn with-stream-content |
|
107 | 79 | (.option "-r, --run" "Compile and execute the file") |
108 | 80 | (.option "-c, --compile" "Compile to JavaScript and save as .js files") |
109 | 81 | (.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`") |
111 | 83 | (.option "-o, --output <dir>" "Output to specified directory") |
112 | 84 | (.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 | 85 | (.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 |
118 | 87 | (cond options.run (run options.args) |
119 | 88 | (not process.stdin.isTTY) (compile-stdin options) |
120 | 89 | options.interactive (start-repl) |
|
0 commit comments