331 lines
11 KiB
JavaScript
331 lines
11 KiB
JavaScript
import { validateArgument } from './utils/validate.js';
|
|
import { sync as inspectSync } from './inspect.js';
|
|
import { async as listAsync, sync as listSync } from './list.js';
|
|
import * as pathUtil from 'path';
|
|
import { rmdir, unlink, rmdirSync, unlinkSync } from 'fs';
|
|
import { ErrNoFileOrDir } from './errors.js';
|
|
import { ErrnoException } from './interfaces.js';
|
|
import { create as matcher } from './utils/matcher.js';
|
|
import { ENodeOperationStatus, EDeleteFlags, EResolve, EResolveMode } from './interfaces.js';
|
|
import { createItem } from './inspect.js';
|
|
import { async as iteratorAsync } from './iterator.js';
|
|
import { ErrCantDelete } from './errors.js';
|
|
export function validateInput(methodName, path) {
|
|
const methodSignature = methodName + '([path])';
|
|
validateArgument(methodSignature, 'path', path, ['string', 'undefined']);
|
|
}
|
|
const parseOptions = (options, path) => {
|
|
const opts = options || {};
|
|
const parsedOptions = {};
|
|
parsedOptions.progress = opts.progress;
|
|
parsedOptions.conflictCallback = opts.conflictCallback;
|
|
parsedOptions.conflictSettings = opts.conflictSettings;
|
|
parsedOptions.debug = opts.debug;
|
|
parsedOptions.matching = opts.matching;
|
|
if (!opts.filter) {
|
|
if (opts.matching) {
|
|
parsedOptions.filter = matcher(path, opts.matching);
|
|
}
|
|
else {
|
|
parsedOptions.filter = () => {
|
|
return true;
|
|
};
|
|
}
|
|
}
|
|
return parsedOptions;
|
|
};
|
|
// ---------------------------------------------------------
|
|
// Sync
|
|
// ---------------------------------------------------------
|
|
export function sync(path, options) {
|
|
const inspectedFile = inspectSync(path, { symlinks: true });
|
|
if (inspectedFile === undefined) {
|
|
// The path already doesn't exits. Nothing to do here.
|
|
}
|
|
else if (inspectedFile.type === 'dir') {
|
|
listSync(path).forEach((filename) => {
|
|
sync(pathUtil.join(path, filename));
|
|
});
|
|
rmdirSync(path);
|
|
}
|
|
else if (inspectedFile.type === 'file' || inspectedFile.type === 'symlink') {
|
|
unlinkSync(path);
|
|
}
|
|
else {
|
|
throw ErrNoFileOrDir(path);
|
|
}
|
|
}
|
|
// ---------------------------------------------------------
|
|
// Async
|
|
// ---------------------------------------------------------
|
|
const rmASync = (path, options) => {
|
|
return new Promise((resolve, reject) => {
|
|
unlink(path, (err) => {
|
|
if (!err) {
|
|
resolve();
|
|
}
|
|
else {
|
|
reject(err);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
const isDone = (nodes) => {
|
|
let done = true;
|
|
nodes.forEach((element) => {
|
|
if (element.status !== ENodeOperationStatus.DONE) {
|
|
done = false;
|
|
}
|
|
});
|
|
return done;
|
|
};
|
|
const next = (nodes) => {
|
|
for (let i = 0; i < nodes.length; i++) {
|
|
if (nodes[i].status === ENodeOperationStatus.COLLECTED) {
|
|
return nodes[i];
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
// handle user side setting "THROW" and non enum values (null)
|
|
const onConflict = (from, options, settings) => {
|
|
switch (settings.overwrite) {
|
|
case EResolveMode.THROW: {
|
|
throw ErrCantDelete(from);
|
|
}
|
|
case EResolveMode.OVERWRITE:
|
|
case EResolveMode.APPEND:
|
|
case EResolveMode.IF_NEWER:
|
|
case EResolveMode.ABORT:
|
|
case EResolveMode.IF_SIZE_DIFFERS:
|
|
case EResolveMode.SKIP: {
|
|
return settings.overwrite;
|
|
}
|
|
default: {
|
|
return undefined;
|
|
}
|
|
}
|
|
};
|
|
export function resolveConflict(path, resolveMode) {
|
|
if (resolveMode === undefined) {
|
|
return true;
|
|
}
|
|
if (resolveMode === EResolveMode.SKIP) {
|
|
return false;
|
|
}
|
|
else if (resolveMode === EResolveMode.ABORT) {
|
|
return false;
|
|
}
|
|
else if (resolveMode === EResolveMode.RETRY) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
const visitor = (path, vars, item) => {
|
|
const options = vars.options;
|
|
if (!item) {
|
|
return;
|
|
}
|
|
item.status = ENodeOperationStatus.PROCESSING;
|
|
const done = () => {
|
|
item.status = ENodeOperationStatus.DONE;
|
|
if (isDone(vars.nodes)) {
|
|
return vars.resolve(vars.result);
|
|
}
|
|
else {
|
|
if (vars.nodes.length) {
|
|
const itemInner = next(vars.nodes);
|
|
if (itemInner) {
|
|
visitor(itemInner.path, vars, itemInner);
|
|
}
|
|
else {
|
|
vars.resolve(vars.result);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
if (isDone(vars.nodes)) {
|
|
return vars.resolve(vars.result);
|
|
}
|
|
vars.filesInProgress += 1;
|
|
rmASync(path, options)
|
|
.then((res) => {
|
|
done();
|
|
})
|
|
.catch((err) => {
|
|
if (err.code === 'EACCES' || err.code === 'EPERM' || err.code === 'EISDIR' || err.code === 'ENOTEMPTY') {
|
|
const resolved = (settings) => {
|
|
settings.error = err.code;
|
|
// feature : report
|
|
if (settings && options && options.flags && options.flags & EDeleteFlags.REPORT) {
|
|
vars.result.push({
|
|
error: settings.error,
|
|
node: item,
|
|
resolved: settings
|
|
});
|
|
}
|
|
if (settings) {
|
|
// if the first resolve callback returned an individual resolve settings "THIS",
|
|
// ask the user again with the same item
|
|
const always = settings.mode === EResolve.ALWAYS;
|
|
if (always) {
|
|
options.conflictSettings = settings;
|
|
}
|
|
let how = settings.overwrite;
|
|
how = onConflict(item.path, options, settings);
|
|
if (how === EResolveMode.ABORT) {
|
|
vars.abort = true;
|
|
}
|
|
if (vars.abort) {
|
|
done();
|
|
return;
|
|
}
|
|
if (!resolveConflict(item.path, how)) {
|
|
done();
|
|
return;
|
|
}
|
|
item.status = ENodeOperationStatus.PROCESS;
|
|
if (settings.overwrite === EResolveMode.RETRY) {
|
|
item.status = ENodeOperationStatus.COLLECTED;
|
|
visitor(path, vars, item);
|
|
}
|
|
}
|
|
};
|
|
if (!options.conflictSettings) {
|
|
const promise = options.conflictCallback(path, createItem(path), err.code);
|
|
promise.then(resolved);
|
|
}
|
|
else {
|
|
resolved(options.conflictSettings);
|
|
}
|
|
}
|
|
});
|
|
};
|
|
async function collect(path, options) {
|
|
return new Promise((resolve, reject) => {
|
|
const all = [];
|
|
iteratorAsync(path, {
|
|
filter: options.filter
|
|
}).then((it) => {
|
|
let node;
|
|
while (node = it.next()) {
|
|
all.push({
|
|
path: node.path,
|
|
item: node.item,
|
|
status: ENodeOperationStatus.COLLECTED
|
|
});
|
|
}
|
|
resolve(all);
|
|
}).catch((err) => {
|
|
console.error('read error', err);
|
|
});
|
|
});
|
|
}
|
|
export async function async(path, options) {
|
|
options = parseOptions(options, path);
|
|
const onError = (err, resolve, reject, nodes) => {
|
|
if (err.code === 'EPERM' || err.code === 'EISDIR' || err.code === 'ENOTEMPTY') {
|
|
const proceed = () => {
|
|
// It's not a file, it's a directory.
|
|
// Must delete everything inside first.
|
|
listAsync(path).then((filenamesInsideDir) => {
|
|
const promises = filenamesInsideDir.map((filename) => {
|
|
return async(pathUtil.join(path, filename), options);
|
|
});
|
|
return Promise.all(promises);
|
|
}).then(() => {
|
|
// Everything inside directory has been removed,
|
|
// it's safe now to go for the directory itself.
|
|
return rmdir(path, (err2) => {
|
|
if (err2) {
|
|
reject(err2);
|
|
}
|
|
});
|
|
})
|
|
.then(resolve, reject);
|
|
};
|
|
// we have a user conflict callback,
|
|
// collect nodes and start asking
|
|
if (options.conflictCallback) {
|
|
const result = void 0;
|
|
// walker variables
|
|
const visitorArgs = {
|
|
resolve: resolve,
|
|
reject: reject,
|
|
abort: false,
|
|
filesInProgress: 0,
|
|
resolveSettings: null,
|
|
options: options,
|
|
result: result,
|
|
nodes: nodes || []
|
|
};
|
|
const process = () => {
|
|
visitorArgs.nodes = nodes;
|
|
if (isDone(nodes)) {
|
|
return resolve(result);
|
|
}
|
|
if (nodes.length) {
|
|
const item = next(nodes);
|
|
if (item) {
|
|
visitor(item.path, visitorArgs, item);
|
|
}
|
|
}
|
|
};
|
|
if (!nodes) {
|
|
const _nodes = visitorArgs.nodes;
|
|
iteratorAsync(path, {
|
|
filter: options.filter
|
|
}).then((it) => {
|
|
let node;
|
|
while (node = it.next()) {
|
|
_nodes.push({
|
|
path: node.path,
|
|
item: node.item,
|
|
status: ENodeOperationStatus.COLLECTED
|
|
});
|
|
}
|
|
process();
|
|
}).catch((err2) => {
|
|
console.error('read error', err2);
|
|
});
|
|
}
|
|
else {
|
|
process();
|
|
}
|
|
}
|
|
else {
|
|
proceed();
|
|
}
|
|
}
|
|
else if (err.code === 'ENOENT') {
|
|
// File already doesn't exist. We're done.
|
|
resolve();
|
|
}
|
|
else {
|
|
// Something unexpected happened. Rethrow original error.
|
|
reject(err);
|
|
}
|
|
};
|
|
// if matching is set, its like rm somePath/*.ext
|
|
// in this case, we collect the inner matching nodes and proceed as it
|
|
// would be an error
|
|
if (options.matching) {
|
|
const nodes = await collect(path, options);
|
|
const err = new ErrnoException('dummy');
|
|
err.code = 'ENOTEMPTY';
|
|
return new Promise((resolve, reject) => {
|
|
onError(err, resolve, reject, nodes);
|
|
});
|
|
}
|
|
else {
|
|
return new Promise((resolve, reject) => {
|
|
// Assume the path is a file or directory and just try to remove it.
|
|
rmASync(path, options)
|
|
.then((res) => resolve())
|
|
.catch((err) => {
|
|
onError(err, resolve, reject);
|
|
});
|
|
});
|
|
}
|
|
}
|