Skip to content

Commit 8787fd0

Browse files
committed
feat: file copy support overwrite
1 parent b528a21 commit 8787fd0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

electron/mapi/file/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,14 @@ const rename = async (
474474
}
475475
};
476476

477-
const copy = async (pathOld: string, pathNew: string, option?: { isDataPath?: boolean }) => {
477+
const copy = async (pathOld: string, pathNew: string, option?: {
478+
isDataPath?: boolean,
479+
overwrite?: boolean,
480+
}) => {
478481
option = Object.assign(
479482
{
480483
isDataPath: false,
484+
overwrite: false,
481485
},
482486
option
483487
);
@@ -491,7 +495,11 @@ const copy = async (pathOld: string, pathNew: string, option?: { isDataPath?: bo
491495
throw `Copy.FileNotFound - ${fullPathOld}`;
492496
}
493497
if (fs.existsSync(fullPathNew)) {
494-
throw `FileAlreadyExists:${fullPathNew}`;
498+
if (option.overwrite) {
499+
await deletes(fullPathNew, {isDataPath: false});
500+
} else {
501+
throw `Copy.FileAlreadyExists - ${fullPathNew}`;
502+
}
495503
}
496504
// console.log('copy', fullPathOld, fullPathNew)
497505
const dir = nodePath.dirname(fullPathNew);

src/declarations/type.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ type DefsMapi = {
188188
overwrite?: boolean;
189189
}
190190
) => Promise<void>;
191-
copy: (pathOld: string, pathNew: string, option?: { isDataPath?: boolean }) => Promise<void>;
191+
copy: (pathOld: string, pathNew: string, option?: {
192+
isDataPath?: boolean,
193+
overwrite?: boolean;
194+
}) => Promise<void>;
192195
temp: (ext: string = "tmp", prefix: string = "file", suffix: string = "") => Promise<string>;
193196
tempDir: (prefix: string = "dir") => Promise<string>;
194197
watchText: (

0 commit comments

Comments
 (0)