-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.cirru
105 lines (104 loc) · 4.53 KB
/
compact.cirru
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
{} (:package |cumulo-util)
:configs $ {} (:init-fn |cumulo-util.client/main!) (:reload-fn |cumulo-util.client/reload!) (:version |0.0.7)
:modules $ []
:entries $ {}
:server $ {} (:init-fn |cumulo-util.app/main!) (:reload-fn |cumulo-util.app/reload!)
:modules $ []
:files $ {}
|cumulo-util.app $ %{} :FileEntry
:defs $ {}
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! () (println "\"Started") (task!) (write-mildly! "\"a/a/a" "\"a")
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! () (println "\"Reload") (task!)
|task! $ %{} :CodeEntry (:doc |)
:code $ quote
defn task! () $ echo "\"Task..."
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns cumulo-util.app $ :require
cumulo-util.file :refer $ write-mildly!
|cumulo-util.client $ %{} :FileEntry
:defs $ {}
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! () $ on-page-touch
fn () $ println "\"called"
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! $
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns cumulo-util.client $ :require
cumulo-util.core :refer $ on-page-touch
|cumulo-util.core $ %{} :FileEntry
:defs $ {}
|*cooling $ %{} :CodeEntry (:doc |)
:code $ quote (defatom *cooling false)
|on-page-touch $ %{} :CodeEntry (:doc |)
:code $ quote
defn on-page-touch (listener) (reset! *cooling false)
let
call-listener $ fn ()
when (not @*cooling) (listener) (reset! *cooling true)
flipped js/setTimeout 800 $ fn () (reset! *cooling false)
js/window.addEventListener "\"focus" $ fn (event) (call-listener)
js/window.addEventListener "\"visibilitychange" $ fn (event)
when
= "\"visible" $ .-visibilityState js/document
call-listener
|visibility-heartbeat $ %{} :CodeEntry (:doc "|chrome 15s, firefox 7s, maybe, so pick a smaller time for checking")
:code $ quote
defn visibility-heartbeat (cb ? duration)
flipped js/setInterval 3000 $ fn ()
if
= "\"visible" $ .-visibilityState js/document
cb
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns cumulo-util.core)
|cumulo-util.file $ %{} :FileEntry
:defs $ {}
|get-backup-path! $ %{} :CodeEntry (:doc |)
:code $ quote
defn get-backup-path! () $ let
now $ new js/Date
path/join js/__dirname "\"backups"
str $ inc (.!getMonth now)
str (.!getDate now) "\"-snapshot.edn"
|merge-local-edn! $ %{} :CodeEntry (:doc |)
:code $ quote
defn merge-local-edn! (x0 filepath handler)
merge x0 $ let
found? $ fs/existsSync filepath
if (fn? handler) (handler found?)
if found?
parse-cirru-edn $ fs/readFileSync filepath |utf8
, nil
|sh! $ %{} :CodeEntry (:doc |)
:code $ quote
defn sh! (command) (println command)
println $ .toString (cp/execSync command)
|write-mildly! $ %{} :CodeEntry (:doc |)
:code $ quote
defn write-mildly! (file-path content)
let
dir $ path/dirname file-path
filename $ path/basename file-path
temp-name $ str "\"/tmp/" (js/Date.now) "\"-" (js/Math.random) "\"-" filename
do-write! $ fn () (fs/writeFileSync temp-name content) (fs/renameSync temp-name file-path) (println "\"Write to file:" file-path)
if (fs/existsSync file-path)
let
old-content $ fs/readFileSync file-path "\"utf8"
if (not= content old-content) (do-write!) (; println "\"same file, skipping:" file-path)
do
when
and (not= "\"." dir)
not $ fs/existsSync dir
fs/mkdirSync dir $ to-js-data
{} $ :recursive true
do-write!
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns cumulo-util.file $ :require ("\"path" :as path) ("\"fs" :as fs) ("\"child_process" :as cp) ("\"net" :as net)