|
5 | 5 | [cljs.core.async :refer [<! >! timeout chan]]
|
6 | 6 | [shallow-diff.patch :refer [patch]]
|
7 | 7 | [cirru-sepal.analyze :refer [write-file ns->path]]
|
| 8 | + [cirru-edn.core :as cirru-edn] |
8 | 9 | [fipp.edn :refer [pprint]]
|
9 |
| - [app.walk :refer [walk]]) |
| 10 | + [app.walk :refer [walk]] |
| 11 | + ["fs" :as fs] |
| 12 | + ["http" :as http] |
| 13 | + ["path" :as path] |
| 14 | + ["mkdirp" :as mkdirp]) |
10 | 15 | (:require-macros [cljs.core.async.macros :refer [go]]))
|
11 | 16 |
|
12 |
| -(def fs (js/require "fs")) |
13 |
| -(def http (js/require "http")) |
14 |
| -(def path (js/require "path")) |
15 |
| -(def mkdirp (js/require "mkdirp")) |
16 |
| - |
17 | 17 | (def ir-path
|
18 | 18 | (or
|
19 | 19 | (get (js->clj js/process.argv) 2)
|
20 |
| - (if (fs.existsSync "ir.edn") "ir.edn") |
21 |
| - (if (fs.existsSync "stack-sepal.ir") "stack-sepal.ir") |
| 20 | + (if (fs/existsSync "stack.cirru") "stack.cirru") |
22 | 21 | (do
|
23 |
| - (println "Missing file: ir.edn or stack-sepal.ir not found!") |
| 22 | + (println "Missing file: stack.cirru not found!") |
24 | 23 | (.exit js/process 1))))
|
25 | 24 |
|
26 | 25 | (def ref-sepal
|
27 | 26 | (atom
|
28 |
| - (if (fs.existsSync ir-path) |
29 |
| - (read-string (fs.readFileSync ir-path "utf8")) |
| 27 | + (if (fs/existsSync ir-path) |
| 28 | + (cirru-edn/parse (fs/readFileSync ir-path "utf8")) |
30 | 29 | (do (.log js/console (str "Error: " ir-path " does not exist!"))
|
31 | 30 | (.exit js/process 1)))))
|
32 | 31 |
|
33 | 32 | (def extension (or js/process.env.extension (:extension @ref-sepal) ".cljs"))
|
34 | 33 | (def out-folder (or js/process.env.out (get-in @ref-sepal [:options :src]) "src/"))
|
35 | 34 | (def port (js/parseInt (or js/process.env.port (get-in @ref-sepal [:options :port]) "7010")))
|
36 | 35 |
|
37 |
| -(defn read-body [req] |
| 36 | +(defn read-body [^js req] |
38 | 37 | (let [body-ref (atom "")
|
39 | 38 | body-chan (chan)]
|
40 | 39 | (.on req "data" (fn [chunk] (swap! body-ref str chunk)))
|
41 | 40 | (.on req "end" (fn [] (go (>! body-chan @body-ref))))
|
42 | 41 | body-chan))
|
43 | 42 |
|
44 | 43 | (defn rewrite-file! [content]
|
45 |
| - (fs.writeFileSync ir-path (with-out-str (pprint content {:width 120})))) |
| 44 | + (fs/writeFileSync ir-path (cirru-edn/write content))) |
46 | 45 |
|
47 | 46 | (defn write-by-file [pkg ns-part file-info]
|
48 | 47 | (let [file-name (str (ns->path pkg ns-part) extension)
|
49 | 48 | content (write-file file-info)
|
50 |
| - file-target (path.join out-folder file-name) |
51 |
| - container (path.dirname file-target)] |
| 49 | + file-target (path/join out-folder file-name) |
| 50 | + container (path/dirname file-target)] |
52 | 51 | (println "File compiled:" file-name)
|
53 |
| - (if (not (fs.existsSync container)) |
| 52 | + (if (not (fs/existsSync container)) |
54 | 53 | (do
|
55 | 54 | (println "Creating folder:" container)
|
56 |
| - (mkdirp.sync container))) |
57 |
| - (fs.writeFileSync file-target content))) |
| 55 | + (mkdirp/sync container))) |
| 56 | + (fs/writeFileSync file-target content))) |
58 | 57 |
|
59 | 58 | (defn compare-write-source! [sepal-data]
|
60 | 59 | (doseq [entry (:files sepal-data)]
|
|
68 | 67 | (let [[ns-part file-info] entry]
|
69 | 68 | (write-by-file (:package sepal-data) ns-part file-info))))
|
70 | 69 |
|
71 |
| -(defn req-handler [req res] |
| 70 | +(defn req-handler [^js req ^js res] |
72 | 71 | (if (some? req.headers.origin)
|
73 | 72 | (.setHeader res "Access-Control-Allow-Origin" req.headers.origin))
|
74 | 73 | (.setHeader res "Content-Type" "text/edn; charset=UTF-8")
|
|
92 | 91 | (.end res (str "Unknown:" req.method))))
|
93 | 92 |
|
94 | 93 | (defn create-app! []
|
95 |
| - (let [app (http.createServer req-handler)] |
| 94 | + (let [app (http/createServer req-handler)] |
96 | 95 | (.listen app port)
|
97 | 96 | (println "File:" ir-path)
|
98 | 97 | (println "Port:" port)
|
|
110 | 109 | (map
|
111 | 110 | (fn [ns-part]
|
112 | 111 | (string/replace
|
113 |
| - (path.join out-folder |
| 112 | + (path/join out-folder |
114 | 113 | (string/replace (:package @ref-sepal) "." "/")
|
115 | 114 | (str (string/replace ns-part "." "/") extension))
|
116 | 115 | "-" "_"))
|
|
128 | 127 | ; (println "alive-files" alive-files)
|
129 | 128 | ; (println "existing-files" existing-files)
|
130 | 129 | (doseq [file-path removed-files]
|
131 |
| - (fs.unlinkSync file-path) |
| 130 | + (fs/unlinkSync file-path) |
132 | 131 | (println "Redundant file:" file-path)))))
|
133 | 132 |
|
134 | 133 | (.on js/process "SIGINT"
|
135 | 134 | (fn []
|
136 |
| - (if (fs.existsSync out-folder) (check-removed!)) |
| 135 | + (if (fs/existsSync out-folder) (check-removed!)) |
137 | 136 | (.exit js/process)))
|
138 | 137 |
|
139 | 138 | (defn reload! []
|
|
0 commit comments