Skip to content

Commit

Permalink
Use cljs-utils for arg handling, file IO, and error printing
Browse files Browse the repository at this point in the history
Use functions from cljs-utils instead of redefining them.
  • Loading branch information
jonsmock committed May 28, 2024
1 parent f61013e commit f849bb7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/dctest/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
(:require [cljs-bean.core :refer [->clj]]
[clojure.edn :as edn]
[clojure.string :as S]
[clojure.pprint :refer [pprint]]
[dctest.util :as util :refer [fatal obj->str log]]
[dctest.util :as util :refer [obj->str log]]
[promesa.core :as P]
[viasat.retry :as retry]
[viasat.util :refer [fatal parse-opts write-file]]
["stream" :as stream]
#_["dockerode$default" :as Docker]
))
Expand Down Expand Up @@ -232,7 +232,7 @@ Options:

(defn write-results-file [opts summary]
(when-let [path (:results-file opts)]
(util/write-file path (obj->str summary))))
(write-file path (obj->str summary))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Load Suite
Expand Down Expand Up @@ -274,7 +274,7 @@ Options:
;; Main

(defn -main [& argv]
(P/let [opts (util/parse-opts usage (or argv #js []))
(P/let [opts (parse-opts usage (or argv #js []))
{:keys [continue-on-error quiet project test-suite]} opts

suites (P/all
Expand Down
33 changes: 1 addition & 32 deletions src/dctest/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,20 @@
[clojure.string :as S]
[clojure.pprint :refer [pprint]]
[promesa.core :as P]
[viasat.util :refer [Eprintln fatal read-file]]
["js-yaml" :as yaml]
["fs" :as fs]
["neodoc" :as neodoc]
["util" :refer [promisify]]
#_["ajv$default" :as Ajv]
))

;; TODO: use require syntax when shadow-cljs works with "*$default"
(def Ajv (js/require "ajv"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Argument processing

(defn clean-opts [arg-map]
(reduce (fn [o [a v]]
(let [k (keyword (S/replace a #"^[-<]*([^>]*)[>]*$" "$1"))]
(assoc o k (or (get o k) v))))
{} arg-map))

(defn parse-opts [usage argv]
(-> usage
(neodoc/run (clj->js {:optionsFirst true
:smartOptions true
:argv argv}))
js->clj
clean-opts))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; General functions

(defn obj->str [obj]
(js/JSON.stringify (clj->js obj)))

(def Eprn #(binding [*print-fn* *print-err-fn*] (apply prn %&)))
(def Eprintln #(binding [*print-fn* *print-err-fn*] (apply println %&)))
(def Epprint #(binding [*print-fn* *print-err-fn*] (pprint %)))

(defn fatal [code & args]
(when (seq args)
(apply Eprintln args))
(js/process.exit code))

(defn log [opts & args]
(when-not (:quiet opts)
(apply println args)))
Expand All @@ -69,9 +41,6 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; File functions

(def read-file (promisify (.-readFile fs)))
(def write-file (promisify (.-writeFile fs)))

(defn load-yaml [path]
(P/let [buffer (read-file path)]
(try
Expand Down

0 comments on commit f849bb7

Please sign in to comment.