| 1 |
// Function that provides map of rules found in ignorefiles for given ignorefile |
| 2 |
// type. Additinally it invokes event on changes in map which are results of |
| 3 |
// changes in ignorefiles. |
| 4 |
|
| 5 |
"use strict"; |
| 6 |
|
| 7 |
var invoke = require("es5-ext/function/invoke") |
| 8 |
, assign = require("es5-ext/object/assign") |
| 9 |
, isValue = require("es5-ext/object/is-value") |
| 10 |
, forEach = require("es5-ext/object/for-each") |
| 11 |
, isCopy = require("es5-ext/object/is-copy-deep") |
| 12 |
, endsWith = require("es5-ext/string/#/ends-with") |
| 13 |
, deferred = require("deferred") |
| 14 |
, pathUtils = require("path") |
| 15 |
, findRoot = require("./find-root") |
| 16 |
, readFile = require("../read-file").readFile |
| 17 |
, dirname = pathUtils.dirname |
| 18 |
, sep = pathUtils.sep |
| 19 |
, ConfMap |
| 20 |
, readRules |
| 21 |
, readRulesWatcher |
| 22 |
, paths |
| 23 |
, getMap |
| 24 |
, lockId = 0; |
| 25 |
|
| 26 |
paths = function (rootPath, path2) { |
| 27 |
var starter, data; |
| 28 |
starter = [rootPath]; |
| 29 |
if (rootPath === path2) { |
| 30 |
return starter; |
| 31 |
} |
| 32 |
if (endsWith.call(rootPath, sep)) { |
| 33 |
data = path2.slice(rootPath.length).split(sep); |
| 34 |
starter.push((rootPath += data.shift())); |
| 35 |
} else { |
| 36 |
data = path2.slice(rootPath.length + 1).split(sep); |
| 37 |
} |
| 38 |
return starter.concat(data.map(function (path) { return (rootPath += sep + path); })); |
| 39 |
}; |
| 40 |
|
| 41 |
readRules = function (path) { |
| 42 |
return this.readFile(path + sep + this.filename)( |
| 43 |
function (src) { return isValue(src) ? this.parse(src, path) : src; }.bind(this) |
| 44 |
); |
| 45 |
}; |
| 46 |
readRulesWatcher = function (path) { |
| 47 |
var watcher, promise, current; |
| 48 |
watcher = this.readFile(path + sep + this.filename); |
| 49 |
promise = watcher( |
| 50 |
function (src) { return (current = isValue(src) ? this.parse(src, path) : src); }.bind(this) |
| 51 |
); |
| 52 |
watcher.on( |
| 53 |
"change", |
| 54 |
function (data) { |
| 55 |
data = isValue(data) ? this.parse(data, path) : data; |
| 56 |
if (data === current) return; |
| 57 |
if ( |
| 58 |
isValue(current) && |
| 59 |
isValue(data) && |
| 60 |
typeof current === "object" && |
| 61 |
isCopy(current, data) |
| 62 |
) { |
| 63 |
return; |
| 64 |
} |
| 65 |
current = promise.value = data; |
| 66 |
promise.emit("change", current, path); |
| 67 |
}.bind(this) |
| 68 |
); |
| 69 |
promise.close = watcher.close; |
| 70 |
return promise; |
| 71 |
}; |
| 72 |
|
| 73 |
ConfMap = function (path, watch) { |
| 74 |
this.path = path; |
| 75 |
this.watch = watch; |
| 76 |
this.data = { root: null, map: {} }; |
| 77 |
assign(this, deferred()); |
| 78 |
if (this.watch) { |
| 79 |
this.onRulesChange = this.onRulesChange.bind(this); |
| 80 |
this.rulePromises = {}; |
| 81 |
this.promise.close = this.close.bind(this); |
| 82 |
} |
| 83 |
}; |
| 84 |
ConfMap.prototype = { |
| 85 |
init: function (lFindRoot) { |
| 86 |
this.findRoot = lFindRoot; |
| 87 |
if (this.watch) { |
| 88 |
lFindRoot.on("change", this.updateRoot.bind(this)); |
| 89 |
} |
| 90 |
lFindRoot.done( |
| 91 |
function (rootPath) { |
| 92 |
if (rootPath) { |
| 93 |
this.data.root = rootPath; |
| 94 |
this.addPaths(rootPath, this.path).done( |
| 95 |
function () { this.resolve(this.data); }.bind(this), this.resolve |
| 96 |
); |
| 97 |
} else { |
| 98 |
this.resolve(this.data); |
| 99 |
} |
| 100 |
}.bind(this), |
| 101 |
this.resolve |
| 102 |
); |
| 103 |
return this.promise; |
| 104 |
}, |
| 105 |
close: function () { |
| 106 |
if (this.rulePromises) { |
| 107 |
this.findRoot.close(); |
| 108 |
forEach(this.rulePromises, invoke("close")); |
| 109 |
delete this.rulePromises; |
| 110 |
} |
| 111 |
}, |
| 112 |
parse: String, |
| 113 |
readFile: function (path) { return readFile(path, { loose: true, watch: this.watch }); }, |
| 114 |
onRulesChange: function (rules, path) { |
| 115 |
if (isValue(rules)) { |
| 116 |
this.data.map[path] = rules; |
| 117 |
} else { |
| 118 |
delete this.data.map[path]; |
| 119 |
} |
| 120 |
this.promise.emit("change", this.data); |
| 121 |
}, |
| 122 |
addPaths: function (rootPath, path) { |
| 123 |
return deferred.map( |
| 124 |
paths(rootPath, path), |
| 125 |
function (lPath) { |
| 126 |
var rules = this.readRules(lPath); |
| 127 |
if (this.watch) { |
| 128 |
this.rulePromises[lPath] = rules; |
| 129 |
rules.on("change", this.onRulesChange); |
| 130 |
} |
| 131 |
return rules.aside( |
| 132 |
function (lRules) { |
| 133 |
if (isValue(lRules)) { |
| 134 |
this[lPath] = lRules; |
| 135 |
} |
| 136 |
}.bind(this.data.map) |
| 137 |
)( |
| 138 |
null, |
| 139 |
function (err) { |
| 140 |
// Watcher might have been closed in a meantime, if it was we ignore |
| 141 |
// this error as we're not interested in that value anymore |
| 142 |
return this.rulePromises[lPath] ? err : null; |
| 143 |
}.bind(this) |
| 144 |
); |
| 145 |
}, |
| 146 |
this |
| 147 |
); |
| 148 |
}, |
| 149 |
removePaths: function (rootPath, path) { |
| 150 |
paths(rootPath, path).forEach(function (lPath) { |
| 151 |
var promise = this.rulePromises[lPath]; |
| 152 |
delete this.rulePromises[lPath]; |
| 153 |
delete this.data.map[lPath]; |
| 154 |
promise.close(); |
| 155 |
}, this); |
| 156 |
}, |
| 157 |
updateRoot: function (rootPath) { |
| 158 |
var lock = ++lockId; |
| 159 |
if (!rootPath) { |
| 160 |
this.removePaths(this.data.root, this.path); |
| 161 |
this.data.root = null; |
| 162 |
this.promise.emit("change", this.data); |
| 163 |
} else if (!this.data.root) { |
| 164 |
this.data.root = rootPath; |
| 165 |
this.addPaths(rootPath, this.path).done( |
| 166 |
function () { |
| 167 |
if (lock === lockId) { |
| 168 |
this.promise.emit("change", this.data); |
| 169 |
} |
| 170 |
}.bind(this) |
| 171 |
); |
| 172 |
} else if (this.data.root < rootPath) { |
| 173 |
this.removePaths(this.data.root, dirname(rootPath)); |
| 174 |
this.data.root = rootPath; |
| 175 |
this.promise.emit("change", this.data); |
| 176 |
} else { |
| 177 |
this.addPaths(rootPath, dirname(this.data.root)).done( |
| 178 |
function () { |
| 179 |
if (lock === lockId) { |
| 180 |
this.promise.emit("change", this.data); |
| 181 |
} |
| 182 |
}.bind(this) |
| 183 |
); |
| 184 |
this.data.root = rootPath; |
| 185 |
} |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
getMap = module.exports = function (path, mode, watch, parse) { |
| 190 |
var map = new ConfMap(path, watch); |
| 191 |
map.filename = mode.filename; |
| 192 |
map.readRules = watch ? readRulesWatcher : readRules; |
| 193 |
if (parse) { |
| 194 |
map.parse = parse; |
| 195 |
} |
| 196 |
return map.init(findRoot(watch ? mode.isRootWatcher : mode.isRoot, path, { watch: watch })); |
| 197 |
}; |
| 198 |
getMap.ConfMap = ConfMap; |
| 199 |
getMap.readRules = readRules; |
| 200 |
getMap.readRulesWatcher = readRulesWatcher; |