Created
March 15, 2017 22:42
-
-
Save wilkerlucio/8065e78ddeb4a6cbd6888523ae914f64 to your computer and use it in GitHub Desktop.
Lumo with container depencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'{:dependencies [[org.clojure/core.async "0.2.395"]] | |
:node-dependencies [[request "2.81.0"]]} | |
(ns script.example | |
(:require [cljs.nodejs :as node])) | |
(def fs (node/require "fs")) | |
(def request (node/require "request")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/lumo | |
(ns lumod.core.startup | |
(:require [cljs.reader :refer [read-string]] | |
[cljs.nodejs :as node] | |
[clojure.string :as str] | |
[clojure.walk :as walk])) | |
(def fs (node/require "fs")) | |
(def exec (.-execSync (node/require "child_process"))) | |
(def spawnSync (.-spawnSync (node/require "child_process"))) | |
(defn child-env [env] | |
(js/Object.assign | |
#js {} | |
(.-env js/process) | |
(clj->js env))) | |
(defn spawn | |
([cmd-line] (spawn cmd-line {})) | |
([cmd-line options] | |
(let [[cmd & args] (str/split cmd-line #" ")] | |
(spawnSync cmd | |
(clj->js args) | |
(clj->js (merge {:stdio "inherit"} options)))))) | |
(defn read-args [] | |
(->> js/process | |
.-argv | |
js->clj | |
(drop-while (complement #(re-find #"/lumod$" %))) | |
next)) | |
(defn elide-quotes [form] | |
(walk/prewalk | |
(fn [x] | |
(if (and (list? x) | |
(= 'quote (first x))) | |
(second x) | |
x)) | |
form)) | |
(defn read-env [path] | |
(-> (.readFileSync fs path "utf8") | |
read-string | |
elide-quotes)) | |
(defn packages->line [packages] | |
(->> packages | |
(mapv (fn [[pkg version]] (str pkg "@" version))) | |
(str/join " "))) | |
(defn check-node-dependencies [packages] | |
(spawn (str "npm ls -g " (package->lines packages)))) | |
(defn install-nodejs-packages [packages] | |
(-> (str "npm install -g " (package->lines packages)) | |
spawn)) | |
(let [[path] (read-args) | |
{:keys [node-dependencies]} (read-env path)] | |
(install-nodejs-packages node-dependencies) | |
(spawn (str "lumo " path) | |
{:env (child-env {"NODE_PATH" "/usr/local/lib/node_modules/"})})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment