|
12 | 12 | For example: a build script may need to how to invalidate compiled
|
13 | 13 | files so that they will be recompiled."
|
14 | 14 | (:refer-clojure :exclude [compile])
|
15 |
| - (:require [cljs.util :as util] |
| 15 | + (:require [clojure.java.io :as io] |
| 16 | + [clojure.string :as string] |
| 17 | + [clojure.data.json :as json] |
| 18 | + [cljs.util :as util] |
16 | 19 | [cljs.env :as env]
|
17 | 20 | [cljs.analyzer :as ana]
|
18 | 21 | [cljs.compiler :as comp]
|
19 | 22 | [cljs.closure :as closure]
|
20 |
| - [cljs.js-deps :as js-deps] |
21 |
| - [clojure.java.io :as io]) |
22 |
| - (:import java.io.File)) |
| 23 | + [cljs.js-deps :as js-deps]) |
| 24 | + (:import [java.io File] |
| 25 | + [java.lang ProcessBuilder Process])) |
23 | 26 |
|
24 | 27 | ;; =============================================================================
|
25 | 28 | ;; Useful Utilities
|
|
213 | 216 | (binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
|
214 | 217 | (closure/watch source opts compiler-env stop))))
|
215 | 218 |
|
| 219 | +(defn add-package-jsons |
| 220 | + "EXPERIMENTAL: see node-module-deps" |
| 221 | + [deps] |
| 222 | + (let [checked (atom #{})] |
| 223 | + (reduce |
| 224 | + (fn [ret {:keys [file] :as dep}] |
| 225 | + (let [f (.getParentFile (io/file file)) |
| 226 | + path (.getAbsolutePath f)] |
| 227 | + (if-not (contains? @checked path) |
| 228 | + (let [f' (io/file f "package.json")] |
| 229 | + (swap! checked conj path) |
| 230 | + (if (.exists f') |
| 231 | + (conj ret dep |
| 232 | + {:file (.getAbsolutePath f') |
| 233 | + :module-type :commonjs}) |
| 234 | + (conj ret dep))) |
| 235 | + ret))) |
| 236 | + [] deps))) |
| 237 | + |
| 238 | +(defn node-module-deps |
| 239 | + "EXPERIMENTAL: return the foreign libs entries as computed by running |
| 240 | + the module-deps package on the supplied JavaScript entry point. Assumes |
| 241 | + that the module-deps & JSONStream NPM packages are either locally or |
| 242 | + globally installed." |
| 243 | + [entry] |
| 244 | + (let [code (string/replace |
| 245 | + (slurp (io/resource "cljs/module_deps.js")) |
| 246 | + "JS_FILE" entry) |
| 247 | + proc (-> (ProcessBuilder. |
| 248 | + (into-array |
| 249 | + ["node" "--eval" (str code)])) |
| 250 | + .start) |
| 251 | + err (.waitFor proc)] |
| 252 | + (if (zero? err) |
| 253 | + (let [is (.getInputStream proc)] |
| 254 | + (into [] |
| 255 | + (map (fn [{:strs [file]}] file |
| 256 | + {:file file :module-type :commonjs})) |
| 257 | + (butlast (json/read-str (slurp is))))) |
| 258 | + []))) |
| 259 | + |
216 | 260 | (comment
|
| 261 | + (add-package-jsons (node-module-deps "src/test/node/test.js")) |
| 262 | + ) |
217 | 263 |
|
| 264 | +(defn node-inputs |
| 265 | + "EXPERIMENTAL: return the foreign libs entries as computed by running |
| 266 | + the module-deps package on the supplied JavaScript entry points. Assumes |
| 267 | + that the module-deps & JSONStream NPM packages are either locally or |
| 268 | + globally installed." |
| 269 | + [entries] |
| 270 | + ) |
| 271 | + |
| 272 | +(comment |
218 | 273 | (def test-cenv (atom {}))
|
219 | 274 | (def test-env (assoc-in (ana/empty-env) [:ns :name] 'cljs.user))
|
220 | 275 |
|
|
0 commit comments