| 1 |
"use strict"; |
| 2 |
|
| 3 |
var isCallable = require("es5-ext/object/is-callable") |
| 4 |
, isValue = require("es5-ext/object/is-value") |
| 5 |
, deferred = require("deferred") |
| 6 |
, resolve = require("path").resolve |
| 7 |
, original = require("fs").readlink; |
| 8 |
|
| 9 |
var readlink = function (path, options) { |
| 10 |
var def = deferred(); |
| 11 |
original(path, options, function (err, stats) { |
| 12 |
if (err) { |
| 13 |
if ( |
| 14 |
options.loose && |
| 15 |
(err.code === "ENOENT" || |
| 16 |
err.code === "EINVAL" || |
| 17 |
err.code === "ENOTDIR" || |
| 18 |
err.code === "UNKNOWN") |
| 19 |
) { |
| 20 |
def.resolve(null); |
| 21 |
} else { |
| 22 |
def.reject(err); |
| 23 |
} |
| 24 |
} else { |
| 25 |
def.resolve(stats); |
| 26 |
} |
| 27 |
}); |
| 28 |
return def.promise; |
| 29 |
}; |
| 30 |
readlink.returnsPromise = true; |
| 31 |
|
| 32 |
module.exports = function (path/*[, options[, callback]]*/) { |
| 33 |
var options, cb; |
| 34 |
|
| 35 |
path = resolve(String(path)); |
| 36 |
options = arguments[1]; |
| 37 |
cb = arguments[2]; |
| 38 |
if (!isValue(cb) && isCallable(options)) { |
| 39 |
cb = Object(options); |
| 40 |
options = {}; |
| 41 |
} else { |
| 42 |
options = Object(options); |
| 43 |
} |
| 44 |
|
| 45 |
return readlink(path, options).cb(cb); |
| 46 |
}; |
| 47 |
module.exports.returnsPromise = true; |
| 48 |
module.exports.readlink = readlink; |