@bevry/fs-remove
Version:
Remove a file or directory.
122 lines (121 loc) • 5.72 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// builtin
const fs_1 = require("fs");
const os_1 = require("os");
const isWindows = (0, os_1.platform)() === 'win32';
const child_process_1 = require("child_process");
const process_1 = require("process");
const nodeVersion = String(process_1.versions.node || '0');
// external
const fs_accessible_1 = __importStar(require("@bevry/fs-accessible"));
const errlop_1 = __importDefault(require("errlop"));
const version_compare_1 = __importDefault(require("version-compare"));
/** Remove a file or directory. */
async function remove(path) {
if (Array.isArray(path)) {
return Promise.all(path.map((i) => remove(i))).then(() => { });
}
// sanity check
if (path === '' || path === '/') {
throw new Error('will not remove root directory');
}
// check exists
try {
await (0, fs_accessible_1.default)(path);
}
catch (err) {
// if it doesn't exist, then we don't care
return;
}
// check writable
try {
await (0, fs_accessible_1.default)(path, fs_accessible_1.W_OK);
}
catch (err) {
if (err.code === 'ENOENT') {
// if it doesn't exist, then we don't care (this may not seem necessary due to the earlier accessible check, however it is necessary, testen would fail on @bevry/json otherwise)
return;
}
throw new errlop_1.default(`unable to remove the non-writable path: ${path}`, err);
}
// attempt removal
return new Promise(function (resolve, reject) {
function next(err) {
if (err && err.code === 'ENOENT')
return resolve();
if (err) {
return reject(new errlop_1.default(`failed to remove the accessible and writable path: ${path}`, err));
}
return resolve();
}
// modern versions have a command that works on files and directories
if ((0, version_compare_1.default)(nodeVersion, '14.14.0') >= 0) {
// use rm builtin via dynamic import (necessary as older versions will fail as you can't import something that doesn't exist
Promise.resolve().then(() => __importStar(require('fs'))).then(function ({ rm: _rm }) {
_rm(path, { recursive: true, force: true, maxRetries: 10 }, next);
});
return;
}
// older versions require a different command for files and directories, so determine which it is
(0, fs_1.stat)(path, function (err, stat) {
if (err)
return next(err);
const isDirectory = stat.isDirectory();
// is file
if (!isDirectory) {
(0, fs_1.unlink)(path, next);
return;
}
// is directory
// NOTE: if you are wondering why this now differs from @bevry/fs-rmdir, it is because the 14.14.0 option is earlier in this file
if ((0, version_compare_1.default)(nodeVersion, '12.16.0') >= 0 &&
(0, version_compare_1.default)(nodeVersion, '16') < 0) {
// use rmdir builtin via dynamic import (necessary as older versions will fail as you can't import something that doesn't exist
Promise.resolve().then(() => __importStar(require('fs'))).then(function ({ rmdir: _rmdir }) {
_rmdir(path, { recursive: true, maxRetries: 10 }, next);
});
}
else if ((0, version_compare_1.default)(nodeVersion, '12.10.0') >= 0 &&
(0, version_compare_1.default)(nodeVersion, '12.16.0') < 0) {
// use rmdir builtin via dynamic import (necessary as older versions will fail as you can't import something that doesn't exist
Promise.resolve().then(() => __importStar(require('fs'))).then(function ({ rmdir: _rmdir }) {
// as any is to workaround that type definition is only for the latest version
_rmdir(path, { recursive: true, maxBusyTries: 10 }, next);
});
}
else {
// no builtin option exists, so use platform-specific workarounds
// rmdir: https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490990(v=technet.10)?redirectedfrom=MSDN
(0, child_process_1.exec)(`${isWindows ? `rmdir /s /q` : `rm -rf`} ${JSON.stringify(path)}`, next);
}
});
});
}
exports.default = remove;