Skip to content

Commit

Permalink
More performant remove
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Mar 15, 2017
1 parent 9011a82 commit f85f5bc
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions lib/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@
var pathUtil = require('path');
var fs = require('./utils/fs');
var validate = require('./utils/validate');
var inspect = require('./inspect');
var list = require('./list');

var validateInput = function (methodName, path) {
var methodSignature = methodName + '([path])';
validate.argument(methodSignature, 'path', path, ['string', 'undefined']);
};

var generateUnknownEtityUnderPathError = function (path) {
return new Error("Can't remove " + path + ' The path is not file nor directory');
};

// ---------------------------------------------------------
// Sync
// ---------------------------------------------------------

var removeSync = function (path) {
// First check what we have on given path.
var inspectedFile = inspect.sync(path, { symlinks: 'report' });
if (inspectedFile === undefined) {
// The path already doesn't exits. Nothing to do here.
} else if (inspectedFile.type === 'dir') {
// Must delete everything inside the directory first.
list.sync(path).forEach(function (filename) {
removeSync(pathUtil.join(path, filename));
});
// Everything inside directory has been removed,
// it's safe now do go for the directory itself.
fs.rmdirSync(path);
} else if (inspectedFile.type === 'file' || inspectedFile.type === 'symlink') {
try {
// Assume the path is a file and just try to remove it.
fs.unlinkSync(path);
} else {
throw generateUnknownEtityUnderPathError(path);
} catch (err) {
if (err.code === 'EPERM') {
// Must delete everything inside the directory first.
list.sync(path).forEach(function (filename) {
removeSync(pathUtil.join(path, filename));
});
// Everything inside directory has been removed,
// it's safe now do go for the directory itself.
fs.rmdirSync(path);
} else if (err.code === 'ENOENT') {
// File already doesn't exist. We're done.
} else {
// Something unexpected happened. Rethrow original error.
throw err;
}
}
};

Expand All @@ -45,13 +42,13 @@ var removeSync = function (path) {

var removeAsync = function (path) {
return new Promise(function (resolve, reject) {
// First check what we have on given path.
inspect.async(path).then(function (inspectedFile) {
if (inspectedFile === undefined) {
// The path already doesn't exits. Nothing to do here.
resolve();
} else if (inspectedFile.type === 'dir') {
// Must delete everything inside the directory first.
// Assume the path is a file and just try to remove it.
fs.unlink(path)
.then(resolve)
.catch(function (err) {
if (err.code === 'EPERM') {
// It's not a file, it's a directory.
// Must delete everything inside first.
list.async(path).then(function (filenamesInsideDir) {
var promises = filenamesInsideDir.map(function (filename) {
return removeAsync(pathUtil.join(path, filename));
Expand All @@ -60,14 +57,16 @@ var removeAsync = function (path) {
})
.then(function () {
// Everything inside directory has been removed,
// it's safe now do go for the directory itself.
// it's safe now to go for the directory itself.
return fs.rmdir(path);
})
.then(resolve, reject);
} else if (inspectedFile.type === 'file' || inspectedFile.type === 'symlink') {
fs.unlink(path).then(resolve, reject);
} else if (err.code === 'ENOENT') {
// File already doesn't exist. We're done.
resolve();
} else {
reject(generateUnknownEtityUnderPathError(path));
// Something unexpected happened. Rethrow original error.
reject(err);
}
});
});
Expand Down

0 comments on commit f85f5bc

Please sign in to comment.