| 1 |
"use strict"; |
| 2 |
|
| 3 |
const isCallable = require("es5-ext/object/is-callable") |
| 4 |
, isValue = require("es5-ext/object/is-value") |
| 5 |
, deferred = require("deferred") |
| 6 |
, { resolve } = require("path") |
| 7 |
, original = require("fs").unlink; |
| 8 |
|
| 9 |
const unlink = function (path, options) { |
| 10 |
const def = deferred(); |
| 11 |
original(path, err => { |
| 12 |
if (err) { |
| 13 |
if (err.code === "ENOENT" && options.loose) def.resolve(); |
| 14 |
else def.reject(err); |
| 15 |
return; |
| 16 |
} |
| 17 |
def.resolve(); |
| 18 |
}); |
| 19 |
return def.promise; |
| 20 |
}; |
| 21 |
unlink.returnsPromise = true; |
| 22 |
|
| 23 |
module.exports = exports = function (path, options = {}, cb = null) { |
| 24 |
path = resolve(String(path)); |
| 25 |
if (!isValue(cb) && isCallable(options)) { |
| 26 |
cb = options; |
| 27 |
options = {}; |
| 28 |
} else { |
| 29 |
options = Object(options); |
| 30 |
} |
| 31 |
|
| 32 |
return unlink(path, options).cb(cb); |
| 33 |
}; |
| 34 |
exports.returnsPromise = true; |
| 35 |
exports.unlink = unlink; |