|
4 | 4 | (:require [fs :refer [createReadStream]] |
5 | 5 | [path :refer [basename dirname join resolve]] |
6 | 6 | [module :refer [Module]] |
| 7 | + [commander] |
7 | 8 |
|
8 | 9 | [wisp.string :refer [split join upper-case replace]] |
9 | 10 | [wisp.sequence :refer [first second last count reduce rest |
|
15 | 16 | [wisp.ast :refer [pr-str name]] |
16 | 17 | [wisp.compiler :refer [compile]])) |
17 | 18 |
|
18 | | - |
19 | | -(defn flag? |
20 | | - [param] |
21 | | - ;; HACK: Workaround for segfault #6691 |
22 | | - (identical? (subs param 0 2) (name :--))) |
23 | | - |
24 | | -(defn flag->key |
25 | | - [flag] |
26 | | - (subs flag 2)) |
27 | | - |
28 | | -;; Just mungle all the `--param value` pairs into global *env* hash. |
29 | | -(defn parse-params |
30 | | - [params] |
31 | | - (loop [input params |
32 | | - output {}] |
33 | | - (if (empty? input) |
34 | | - output |
35 | | - (let [name (first input) |
36 | | - value (second input)] |
37 | | - (if (flag? name) |
38 | | - (if (or (nil? value) (flag? value)) |
39 | | - (recur (rest input) |
40 | | - (assoc output (flag->key name) true)) |
41 | | - (recur (drop 2 input) |
42 | | - (assoc output (flag->key name) value))) |
43 | | - (recur (rest input) |
44 | | - output)))))) |
45 | | - |
46 | | - |
47 | | - |
48 | 19 | (defn compile-stdin |
49 | 20 | [options] |
50 | 21 | (with-stream-content process.stdin |
51 | 22 | compile-string |
52 | | - options)) |
| 23 | + (conj {} options))) |
| 24 | +;; (conj {:source-uri options}) causes segfault for some reason |
53 | 25 |
|
54 | 26 | (defn compile-file |
55 | 27 | [path options] |
|
61 | 33 | [source options] |
62 | 34 | (let [channel (or (:print options) :code) |
63 | 35 | output (compile source options) |
64 | | - content (if (= channel :code) |
65 | | - (:code output) |
66 | | - (JSON.stringify (get output channel) 2 2))] |
67 | | - (.write process.stdout (or content "nil")) |
68 | | - (if (:error output) (throw (:error output))))) |
| 36 | + content (cond |
| 37 | + (= channel :code) (:code output) |
| 38 | + (= channel :expansion) (:expansion output) |
| 39 | + :else (JSON.stringify (get output channel) 2 2))] |
| 40 | + (.write process.stdout (or content "nil")) |
| 41 | + (if (:error output) (throw (.-error output))))) |
69 | 42 |
|
70 | 43 | (defn with-stream-content |
71 | 44 | [input resume options] |
|
82 | 55 | ;; https://github.com/joyent/node/blob/master/lib/module.js#L489-493 |
83 | 56 | (Module._load (resolve path) null true)) |
84 | 57 |
|
| 58 | +(defmacro -> |
| 59 | + [& operations] |
| 60 | + (reduce |
| 61 | + (fn [form operation] |
| 62 | + (cons (first operation) |
| 63 | + (cons form (rest operation)))) |
| 64 | + (first operations) |
| 65 | + (rest operations))) |
85 | 66 |
|
86 | 67 | (defn main |
87 | 68 | [] |
88 | | - (let [options (parse-params (drop 2 process.argv))] |
89 | | - (cond (:run options) (run (:run options)) |
| 69 | + (let [options commander] |
| 70 | + (-> options |
| 71 | + (.usage "[options] <file ...>") |
| 72 | + (.option "-r, --run" "Compile and execute the file") |
| 73 | + (.option "-c, --compile" "Compile to JavaScript and save as .js files") |
| 74 | + (.option "-i, --interactive" "Run an interactive wisp REPL") |
| 75 | + (.option "--debug, --print <type>" "Print debug information. Possible values are `expansion`,`forms`, `ast` and `js-ast`") |
| 76 | + (.option "--no-map" "Disable source map generation") |
| 77 | + (.parse process.argv)) |
| 78 | + (set! (aget options "no-map") (not (aget options "map"))) ;; commander auto translates to camelCase |
| 79 | + (cond options.run (run (get options.args 0)) |
90 | 80 | (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)))) |
| 81 | + options.interactive (start-repl) |
| 82 | + options.compile (compile-file (get options.args 0) options) |
| 83 | + options.args (run options.args) |
| 84 | + :else (start-repl) |
| 85 | + ))) |
0 commit comments