You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
1.4 KiB

'use strict';
const path = require('path');
const fs = require('fs');
const pathExists = require('path-exists');
const fsP = fs.promises;
module.exports = async (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
}
options = {
overwrite: true,
...options
};
if (!options.overwrite && await pathExists(destination)) {
throw new Error(`The destination file exists: ${destination}`);
}
await fsP.mkdir(path.dirname(destination), {
recursive: true,
mode: options.directoryMode
});
try {
await fsP.rename(source, destination);
} catch (error) {
if (error.code === 'EXDEV') {
await fsP.copyFile(source, destination);
await fsP.unlink(source);
} else {
throw error;
}
}
};
module.exports.sync = (source, destination, options) => {
if (!source || !destination) {
throw new TypeError('`source` and `destination` file required');
}
options = {
overwrite: true,
...options
};
if (!options.overwrite && fs.existsSync(destination)) {
throw new Error(`The destination file exists: ${destination}`);
}
fs.mkdirSync(path.dirname(destination), {
recursive: true,
mode: options.directoryMode
});
try {
fs.renameSync(source, destination);
} catch (error) {
if (error.code === 'EXDEV') {
fs.copyFileSync(source, destination);
fs.unlinkSync(source);
} else {
throw error;
}
}
};