UNPKG

3.31 kBtext/coffeescriptView Raw
1
2fs = require 'fs'
3path = require 'path'
4fs.exists ?= path.exists
5each = require 'each'
6
7###
8Conditionnal properties
9###
10module.exports =
11 ###
12
13 `all(options, failed, succeed)` Run all conditions
14 ---------------------------------------------------
15
16 `opts`
17 Command options
18
19 `failed`
20 Failed callback, called when a condition failed
21
22 `succeed`
23 Succeed callback, only called if all the condition succeed
24
25 ###
26 all: (options, failed, succeed) ->
27 each([@if, @if_exists, @not_if_exists])
28 .on 'item', (condition, next) ->
29 condition(options, failed, next)
30 .on('error', failed)
31 .on('end', succeed)
32 ###
33 `if` Run action for a user defined condition
34 --------------------------------------------
35
36 Work on the property `if` in `options`. When `if`
37 is a boolean, its value determine to the output. If it's
38 a callback, the function is called with the `options`,
39 `failed` and `succeed` arguments. If it'a an array, all its element
40 must positively resolve for the condition to pass.
41
42 Updating the content of a file if we are the owner
43
44 mecano.render
45 source:'./file'
46 if: (options, failed, succeed) ->
47 fs.stat options.source, (err, stat) ->
48 # File does not exists
49 return failed err if err
50 # Failed if we dont own the file
51 return failed() unless stat.uid is process.getuid()
52 # Succeed if we own the file
53 succeed()
54
55 ###
56 if: (options, failed, succeed) ->
57 return succeed() unless options.if?
58 ok = true
59 each(options.if)
60 .on 'item', (si, next) ->
61 return next() unless ok
62 if typeof si is 'boolean'
63 ok = false unless si
64 next()
65 else if typeof si is 'function'
66 si options, ( -> ok = false; next arguments...), next
67 .on 'both', (err) ->
68 return failed err if err or not ok
69 succeed()
70 ###
71
72 `if_exists` Run action if a file exists
73 ----------------------------------------
74
75 Work on the property `if_exists` in `options`. The value may
76 be a file path or an array of file paths.
77
78 The callback `succeed` is called if all the provided paths
79 exists otherwise the callback `failed` is called.
80
81 ###
82 if_exists: (options, failed, succeed) ->
83 return succeed() unless options.if_exists?
84 each(options.if_exists)
85 .on 'item', (if_exists, next) ->
86 # await fs.exists if_exists, defer exists
87 # if exists then next() else failed()
88 fs.exists if_exists, (exists) ->
89 if exists then next() else failed()
90 .on 'end', succeed
91 ###
92
93 `not_if_exists` Skip action if a file exists
94 ---------------------------------------------
95
96 Work on the property `not_if_exists` in `options`. The value may
97 be a file path or an array of file paths.
98
99 The callback `succeed` is called if none of the provided paths
100 exists otherwise the callback `failed` is called.
101
102 ###
103 not_if_exists: (options, failed, succeed) ->
104 return succeed() unless options.not_if_exists?
105 each(options.not_if_exists)
106 .on 'item', (not_if_exists, next) ->
107 fs.exists not_if_exists, (exists) ->
108 if exists
109 then failed()
110 else next()
111 # await fs.exists not_if_exists, defer exists
112 # if exists then failed() else next()
113 .on 'end', succeed