aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/program/aedifex/command/clean.lux
blob: c37c46367b80350ac25f552dd71096fb6960f666 (plain)
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
(.module:
  [lux #*
   [abstract
    ["." monad (#+ do)]]
   [control
    [try (#+ Try)]
    [concurrency
     ["." promise (#+ Promise)]]]
   [data
    [text
     ["%" format (#+ format)]]]
   [world
    ["." file (#+ Path)]
    ["." console (#+ Console)]]]
  ["." /// #_
   [command (#+ Command)]
   ["#" profile]
   ["#." action (#+ Action)]])

(def: (clean_files! fs root)
  (-> (file.System Promise) Path (Promise (Try Any)))
  (let [! ///action.monad]
    (|> root
        (\ fs directory_files)
        (\ ! map (monad.map ! (\ fs delete)))
        (\ ! join))))

(def: #export success
  (-> ///.Target Text)
  (|>> (format "Successfully cleaned target directory: ")))

(def: #export (do! console fs profile)
  (-> (Console Promise) (file.System Promise) (Command Any))
  (do {! promise.monad}
    [#let [target (get@ #///.target profile)]
     ? (\ fs directory? target)
     _ (let [! ///action.monad]
         (if ?
           (loop [root target]
             (do !
               [_ (..clean_files! fs root)
                _ (|> root
                      (\ fs sub_directories)
                      (\ ! map (monad.map ! recur))
                      (\ ! join))]
               (\ fs delete root)))
           (\ ! wrap [])))]
    (console.write_line (..success target) console)))