/
platform.cljs
119 lines (88 loc) · 2.35 KB
/
platform.cljs
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
(ns cli-matic.platform
"
## Platform-specific functions for ClojureScript.
At the moment, we only support Planck.
BTW, in this NS, we avoid using Spec / Orchestra.
"
(:require [planck.core :as plk]
[planck.environ :as plkenv]
[cljs.reader :as csrdr]
[clojure.string :as str]))
(defn read-env
"Reads an environment variable.
If undefined, returns nil."
[var]
(let [kw (keyword (str/lower-case var))]
(get plkenv/env kw nil)))
(defn exit-script
"Terminates execution with a return value.
Please note that in Planck, return codes seem to be 8-bit unsigned ints.
"
[retval]
(plk/exit retval))
(defn add-shutdown-hook
"Add a shutdown hook.
Does not work (?) on CLJS and we will throw an exception.
It might be conceivable that in JS-land, we save this locally in this namespace
and then call it on `exit-script`.
"
[fnToCallOnShutdown]
(when (ifn? fnToCallOnShutdown)
(throw (ex-info "Shutdown hooks not supported outside the JVM" {}))))
(defn slurp-file
"
Luckily, Planck implements slurp for us.
No slurping in Node-land.
See https://github.com/pkpkpk/cljs-node-io
"
[f]
(plk/slurp f))
(defonce ^:dynamic *stdin*
plk/*in*)
;
; Conversions
;
(defn parseInt
"Converts a string to an integer. "
[s]
(js/parseInt s))
(defn parseFloat
"Converts a string to a float."
[s]
(js/parseFloat s))
(defn asDate
"Converts a string in format yyyy-mm-dd to a
Date object; if conversion
fails, returns nil."
[s]
(throw (ex-info "Dates not supported in CLJS." {:date s})))
(defn parseEdn
"
This is actually a piece of ClojureScript, though it lives in a different NS.
See https://cljs.github.io/api/cljs.reader/read-string
"
[edn-in]
(csrdr/read-string edn-in))
(defn isDeferredValue?
"Is this a deferred value for this platform?"
[_]
(cond
:else false))
(defn waitForDeferredValue
"Given that value is a deferred value for this platform,
block termination until it's realized.
On JS, we support:
- nothing yet...
"
[x]
(cond
:else (throw (ex-info
(str "Value is not deferred " x)
{}))))
(defn printError
"On ClojureScript, STDERR is not *err* but it's platform dependent.
Thanks @mfikes for the tip! (issue #111)
"
[o]
(binding [*print-fn* *print-err-fn*]
(println o)))