~subsetpark/bagatto

ref: afc04f637ad02216e9b9b8336411e6916c8f221c bagatto/src/writers.janet -rw-r--r-- 762 bytes
afc04f63 — Zach Smith Monitor output jobs 4 months ago
                                                                                
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
(import spork/path)

(import /src/util)

(defn writer-init
  "Return an empty table to be used as path cache"
  []
  @{})

(defn ensure-path
  [path cache]
  (let [s (string path)]
    (unless (in cache s)
      (util/mkpath s cache)
      (put cache s true))))

(defn write-handler
  [output-dir]

  (fn handler
    [msg path-cache]

    (match msg
      [:write path contents]
      (let [path (if output-dir (path/join output-dir path) path)
            ppath (path/dirname path)]

        (ensure-path ppath path-cache)
        (spit path contents))

      [:copy from to]
      (let [to (if output-dir (path/join output-dir to) to)
            ppath (path/dirname to)]

        (ensure-path ppath path-cache)
        (util/copy-file from to path-cache)))))