-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
cli_opts.cljc
142 lines (120 loc) · 3.55 KB
/
cli_opts.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
(ns shadow.cljs.devtools.cli-opts
#?(:clj
(:require
[clojure.tools.cli :as cli]
[clojure.string :as str])
:cljs
(:require
[goog.string.format]
[goog.string :refer (format)]
[cljs.tools.cli :as cli]
[clojure.string :as str])))
(defn parse-dep [dep-str]
(let [[sym ver] (str/split dep-str #":")]
[(symbol sym) ver]
))
(defn conj-vec [x y]
(if (nil? x)
[y]
(conj x y)))
(def cli-spec
;; FIXME: how do I make this not show up in summary?
[[nil "--npm" "internal, used by the shadow-cljs npm package"]
["-d" "--dependency DEP" "adds an additional dependency (eg. -d foo/bar:1.2.3 -d another/thing:4.0.0)"
:parse-fn parse-dep
:assoc-fn
(fn [opts k v]
(update opts :dependencies conj-vec v))]
;; generic
["-A" "--aliases ALIASES" "adds aliases for use with clj, only effective when using deps.edn"]
[nil "--source-maps" "temporarily enable source-maps for release debugging"]
[nil "--pseudo-names" "temporarily enable pseudo-names for release debugging. DO NOT SHIP THIS CODE!"]
[nil "--debug" "enable source-maps + pseudo-names. DO NOT SHIP THIS CODE!"]
[nil "--stdin" "clj-eval from stdin"]
["-v" "--verbose" "verbose build log"]
[nil "--force-spawn" "spawn new process, do not connect to running server"]
[nil "--cli-info" "prints a bunch of information"]
[nil "--via VIA" "internal option, used by node script" :parse-fn keyword]
["-h" "--help"]])
(def action-help
;; per action help for: shadow-cljs compile -h
{:compile "TBD"})
(def action-list
[:compile
:watch
:check
:release
:node-repl
:browser-repl
:cljs-repl
:clj-repl
:clj-eval
:clj-run
:run
:info
:pom
:npm-deps
:test
:aot
:init
:help
:server
:start
:stop
:restart
])
(defn help [{:keys [errors summary] :as opts}]
(do (doseq [err errors]
(println)
(println err)
(println))
(println "Usage:")
(println " shadow-cljs <action> <zero or more build ids>")
(println)
(println "Supported actions are:")
(println)
(doseq [action action-list]
;; FIXME: add help
(println (format "%12s - ..." (name action))))
(println)
(println "Options:")
(println "-----")
(println summary)
(println "-----")))
(def action-set
(into #{} action-list))
(def actions-that-require-build-arg
#{:compile
:watch
:release
:check
:cljs-repl})
(defn parse-build-arg [{:keys [action arguments] :as result}]
(if (empty? arguments)
(assoc result :errors [(str "Action \"" (name action) "\" requires one or more build ids")])
;; FIXME: validate build-ids
(assoc result :builds (into [] (map (comp keyword #(str/replace %1 ":" ""))) arguments))))
(defn parse-arguments [{:keys [arguments] :as result}]
(if (empty? arguments)
(assoc result :errors ["Please specify which action to run!"])
(let [action-str
(first arguments)
action
(keyword action-str)]
(if-not (contains? action-set action)
(assoc result :errors [(str "Invalid action \"" action-str "\"")])
(-> result
(assoc :action action)
(update :arguments subvec 1)
(cond->
(contains? actions-that-require-build-arg action)
(parse-build-arg)
))))))
(defn parse [args]
(let [parsed
(cli/parse-opts args cli-spec)]
(if (or (:errors parsed)
(get-in parsed [:options :help]))
parsed
(parse-arguments parsed)
)))