| 1 |
"use strict"; |
| 2 |
|
| 3 |
var isCallable = require("es5-ext/object/is-callable") |
| 4 |
, isValue = require("es5-ext/object/is-value") |
| 5 |
, isString = require("es5-ext/string/is-string") |
| 6 |
, deferred = require("deferred") |
| 7 |
, original = require("fs").readFile |
| 8 |
, resolve = require("path").resolve |
| 9 |
, watch = require("./watch").watch |
| 10 |
, WatchPath = require("./watch-path").WatchPath |
| 11 |
, readFile; |
| 12 |
|
| 13 |
readFile = function (filename, options) { |
| 14 |
var def, current, promise, watcher, resolveCb, onchange, loose; |
| 15 |
|
| 16 |
def = deferred(); |
| 17 |
loose = options.loose; |
| 18 |
original( |
| 19 |
filename, |
| 20 |
options.encoding, |
| 21 |
(resolveCb = function (err, data) { |
| 22 |
if (def.resolved) return; |
| 23 |
if (err) { |
| 24 |
if (watcher && !loose) watcher.close(); |
| 25 |
if (loose) def.resolve(null); |
| 26 |
else def.reject(err); |
| 27 |
return; |
| 28 |
} |
| 29 |
if (options.watch) current = String(data); |
| 30 |
def.resolve(data); |
| 31 |
}) |
| 32 |
); |
| 33 |
promise = def.promise; |
| 34 |
|
| 35 |
if (options.watch) { |
| 36 |
onchange = function () { |
| 37 |
original(filename, options.encoding, function (err, data) { |
| 38 |
var dataStr; |
| 39 |
if (!def.resolved) { |
| 40 |
resolveCb(err, data); |
| 41 |
return; |
| 42 |
} |
| 43 |
if (!watcher) return; |
| 44 |
if (err) { |
| 45 |
watcher.close(); |
| 46 |
promise.emit("end"); |
| 47 |
return; |
| 48 |
} |
| 49 |
dataStr = String(data); |
| 50 |
if (dataStr !== current) { |
| 51 |
current = dataStr; |
| 52 |
promise.emit("change", data); |
| 53 |
} |
| 54 |
}); |
| 55 |
}; |
| 56 |
if (loose) { |
| 57 |
current = null; |
| 58 |
watcher = new WatchPath(filename); |
| 59 |
watcher.on("change", function (event) { |
| 60 |
if (event.type === "remove") { |
| 61 |
if (isValue(current)) promise.emit("change", (current = null)); |
| 62 |
} else { |
| 63 |
onchange(); |
| 64 |
} |
| 65 |
}); |
| 66 |
} else { |
| 67 |
try { watcher = watch(filename); } |
| 68 |
catch (e) { return def.reject(e); } |
| 69 |
watcher.on("change", onchange); |
| 70 |
watcher.on("end", function () { |
| 71 |
watcher = null; |
| 72 |
promise.emit("end"); |
| 73 |
}); |
| 74 |
} |
| 75 |
promise.close = function () { |
| 76 |
watcher.close(); |
| 77 |
if (!def.resolved) { |
| 78 |
def.reject(new Error("Operation aborted: " + filename)); |
| 79 |
} |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
return promise; |
| 84 |
}; |
| 85 |
readFile.returnsPromise = true; |
| 86 |
|
| 87 |
module.exports = exports = function (filename) { |
| 88 |
var options, cb; |
| 89 |
|
| 90 |
filename = resolve(String(filename)); |
| 91 |
options = arguments[1]; |
| 92 |
cb = arguments[2]; |
| 93 |
if (!isValue(cb) && isCallable(options)) { |
| 94 |
cb = options; |
| 95 |
options = {}; |
| 96 |
} else { |
| 97 |
options = isString(options) ? { encoding: options } : Object(options); |
| 98 |
} |
| 99 |
|
| 100 |
return readFile(filename, options).cb(cb); |
| 101 |
}; |
| 102 |
exports.returnsPromise = true; |
| 103 |
exports.readFile = readFile; |