mirror of
https://github.com/YuzuZensai/Termix.git
synced 2026-11-12 19:41:49 +00:00
23 lines
605 B
JavaScript
23 lines
605 B
JavaScript
const fs = require("node:fs");
|
|||
|
|
|
||
|
|
function renameLocalPath(source, target) {
|
||
|
|
if (source === target) return;
|
||
|
|
try {
|
||
|
|
fs.lstatSync(target);
|
||
|
|
const error = new Error("A file or folder with that name already exists");
|
||
|
|
error.code = "EEXIST";
|
||
|
|
throw error;
|
||
|
|
} catch (error) {
|
||
|
|
if (error.code !== "ENOENT") throw error;
|
||
|
|
}
|
||
|
|
if (fs.lstatSync(source).isDirectory()) {
|
||
|
|
fs.renameSync(source, target);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
// link is exclusive: a destination created concurrently is never replaced.
|
||
|
|
fs.linkSync(source, target);
|
||
|
|
fs.unlinkSync(source);
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { renameLocalPath };
|