107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
import * as pathUtil from 'path';
|
|
import { rename, renameSync } from 'fs';
|
|
import { async as existsAsync, sync as existsSync } from './exists.js';
|
|
import { validateArgument } from './utils/validate.js';
|
|
import { ErrDoesntExists } from './errors.js';
|
|
import { EError } from './interfaces.js';
|
|
import { sync as copySync } from './copy.js';
|
|
import { sync as removeSync } from './remove.js';
|
|
import { promisify } from 'util';
|
|
import { async as mkdirAsync, sync as dirSync } from './dir.js';
|
|
export const validateInput = (methodName, from, to) => {
|
|
const methodSignature = methodName + '(from, to)';
|
|
validateArgument(methodSignature, 'from', from, ['string']);
|
|
validateArgument(methodSignature, 'to', to, ['string']);
|
|
};
|
|
// ---------------------------------------------------------
|
|
// Sync
|
|
// ---------------------------------------------------------
|
|
export const sync = (from, to) => {
|
|
try {
|
|
renameSync(from, to);
|
|
}
|
|
catch (err) {
|
|
// not the same device, rename doesnt work here
|
|
if (err.code === EError.CROSS_DEVICE) {
|
|
try {
|
|
copySync(from, to);
|
|
}
|
|
catch (e) {
|
|
throw e;
|
|
}
|
|
try {
|
|
removeSync(from);
|
|
}
|
|
catch (e) {
|
|
throw e;
|
|
}
|
|
return;
|
|
}
|
|
if (err.code !== EError.NOEXISTS) {
|
|
// We can't make sense of this error. Rethrow it.
|
|
throw err;
|
|
}
|
|
else {
|
|
// Ok, source or destination path doesn't exist.
|
|
// Must do more investigation.
|
|
if (!existsSync(from)) {
|
|
throw ErrDoesntExists(from);
|
|
}
|
|
if (!existsSync(to)) {
|
|
// Some parent directory doesn't exist. Create it.
|
|
dirSync(pathUtil.dirname(to));
|
|
// Retry the attempt
|
|
renameSync(from, to);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
// ---------------------------------------------------------
|
|
// Async
|
|
// ---------------------------------------------------------
|
|
const ensureDestinationPathExistsAsync = (to) => {
|
|
return new Promise((resolve, reject) => {
|
|
const destDir = pathUtil.dirname(to);
|
|
existsAsync(destDir)
|
|
.then(dstExists => {
|
|
if (!dstExists) {
|
|
mkdirAsync(destDir).then(resolve, reject);
|
|
}
|
|
else {
|
|
// Hah, no idea.
|
|
reject();
|
|
}
|
|
})
|
|
.catch(reject);
|
|
});
|
|
};
|
|
export const async = (from, to) => {
|
|
return new Promise((resolve, reject) => {
|
|
promisify(rename)(from, to)
|
|
.then(resolve)
|
|
.catch(err => {
|
|
if (err.code !== EError.NOEXISTS) {
|
|
// Something unknown. Rethrow original error.
|
|
reject(err);
|
|
}
|
|
else {
|
|
// Ok, source or destination path doesn't exist.
|
|
// Must do more investigation.
|
|
existsAsync(from)
|
|
.then(srcExists => {
|
|
if (!srcExists) {
|
|
reject(ErrDoesntExists(from));
|
|
}
|
|
else {
|
|
ensureDestinationPathExistsAsync(to)
|
|
// Retry the attempt
|
|
.then(() => promisify(rename)(from, to))
|
|
.then(resolve, reject);
|
|
}
|
|
})
|
|
.catch(reject);
|
|
}
|
|
});
|
|
});
|
|
};
|