|
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 | | - [wisp.sequence :refer [first second last count reduce rest |
| 10 | + [wisp.sequence :refer [first second last count reduce rest map |
10 | 11 | conj partition assoc drop empty?]] |
11 | 12 |
|
12 | 13 | [wisp.repl :refer [start] :rename {start start-repl}] |
|
53 | 54 |
|
54 | 55 | (defn compile-file |
55 | 56 | [path options] |
56 | | - (with-stream-content (createReadStream path) |
| 57 | + (map (fn [file] (with-stream-content |
| 58 | + (createReadStream file) |
57 | 59 | compile-string |
58 | | - (conj {:source-uri path} options))) |
| 60 | + (conj {:source-uri file} options))) path)) |
59 | 61 |
|
60 | 62 | (defn compile-string |
61 | 63 | [source options] |
|
64 | 66 | content (if (= channel :code) |
65 | 67 | (:code output) |
66 | 68 | (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"))) |
67 | 75 | (.write process.stdout (or content "nil")) |
68 | 76 | (if (:error output) (throw (:error output))))) |
69 | 77 |
|
|
80 | 88 | [path] |
81 | 89 | ;; Loading module as main one, same way as nodejs does it: |
82 | 90 | ;; 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)) |
84 | 92 |
|
| 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))) |
85 | 101 |
|
86 | 102 | (defn main |
87 | 103 | [] |
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) |
90 | 119 | (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