-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feature(lib): Copy or move function, including files or folders
- Loading branch information
Showing
8 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fs from 'fs/promises'; | ||
import path from 'path'; | ||
import isDir from '@/isDir'; | ||
import normalize from '@/normalize'; | ||
import makeDir from '@/makeDir'; | ||
import file from '@/copy/file'; | ||
|
||
/** | ||
* 复制目录到新位置 (copy the directory to a new location) | ||
* @param {string} source 源目录路径 (source directory path) | ||
* @param {string} target 目标目录路径 (target directory path) | ||
* @param {boolean} [overwrite=false] 是否覆盖已存在的文件 (whether to overwrite existing files) | ||
* @returns {Promise<boolean>} 是否复制成功 (whether the copy was successful) | ||
*/ | ||
const dir = async (source: string, target: string, overwrite: boolean = false): Promise<boolean> => { | ||
try { | ||
if (!isDir(source)) { | ||
throw new Error('The source path is not a directory.'); | ||
} else if (!isDir(target)) { | ||
throw new Error('The target path is not a directory.'); | ||
} | ||
|
||
const normalizedSource = normalize(source); | ||
const normalizedTarget = normalize(target); | ||
|
||
await makeDir(normalizedTarget); | ||
|
||
const queue = [{ source: normalizedSource, target: normalizedTarget }]; | ||
while (queue.length > 0) { | ||
const task = queue.shift(); | ||
|
||
if (task) { | ||
const { source: currentSource, target: currentTarget } = task; | ||
|
||
const items = await fs.readdir(currentSource, { withFileTypes: true }); | ||
for (const item of items) { | ||
const sourcePath = path.join(currentSource, item.name); | ||
const targetPath = path.join(currentTarget, item.name); | ||
|
||
if (item.isDirectory()) { | ||
(await makeDir(targetPath)) && queue.push({ source: sourcePath, target: targetPath }); | ||
} else { | ||
await file(sourcePath, targetPath, overwrite); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
export default dir; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import fs from 'fs/promises'; | ||
import normalize from '@/normalize'; | ||
import makeDir from '@/makeDir'; | ||
|
||
/** | ||
* 复制文件到新位置 (copy the file to a new location) | ||
* @param {string} source 源文件路径 (source file path) | ||
* @param {string} target 目标文件路径 (target file path) | ||
* @param {boolean} [overwrite=false] 是否覆盖已存在的文件 (whether to overwrite existing files) | ||
* @returns {Promise<boolean>} 是否复制成功 (whether the copy was successful) | ||
*/ | ||
const file = async (source: string, target: string, overwrite: boolean = false): Promise<boolean> => { | ||
try { | ||
const normalizedSource = normalize(source); | ||
const normalizedTarget = normalize(target); | ||
|
||
// 检查源文件和目标文件是否相同 | ||
if (normalizedSource && normalizedTarget && normalizedSource !== normalizedTarget) { | ||
await makeDir(normalizedTarget); | ||
await fs.copyFile(normalizedSource, normalizedTarget, overwrite ? 0 : fs.constants.COPYFILE_EXCL); | ||
} | ||
|
||
return true; | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
export default file; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'fs/promises'; | ||
import normalize from '@/normalize'; | ||
|
||
/** | ||
* 检查路径是否为目录。 | ||
* @param {string} path 要检查的路径。 | ||
* @returns {Promise<boolean>} 如果路径是目录,则返回 true,否则返回 false。 | ||
*/ | ||
const isDir = async (path: string): Promise<boolean> => { | ||
try { | ||
const stats = await fs.stat(normalize(path)); | ||
return stats.isDirectory(); | ||
} catch (err) { | ||
// 错误处理:如果路径不存在或无法访问,将被视为非目录 | ||
return false; | ||
} | ||
}; | ||
|
||
export default isDir; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'fs/promises'; | ||
import normalize from '@/normalize'; | ||
|
||
/** | ||
* 检查路径是否为文件。 | ||
* @param {string} path 要检查的路径。 | ||
* @returns {Promise<boolean>} 如果路径是文件,则返回 true,否则返回 false。 | ||
*/ | ||
const isFile = async (path: string): Promise<boolean> => { | ||
try { | ||
const stats = await fs.stat(normalize(path)); | ||
return stats.isFile(); | ||
} catch (err) { | ||
// 错误处理:如果路径不存在或无法访问,将被视为非文件 | ||
return false; | ||
} | ||
}; | ||
|
||
export default isFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import remove from '@/remove'; | ||
import copy from '@/copy/dir'; | ||
|
||
/** | ||
* 移动文件到新位置 | ||
* @param {string} source 源文件路径 | ||
* @param {string} target 目标文件路径 | ||
* @param {boolean} [overwrite=false] 是否覆盖已存在的文件 (whether to overwrite existing files) | ||
* @returns {Promise<boolean>} 是否移动成功 | ||
*/ | ||
const dir = async (source: string, target: string, overwrite: boolean = false): Promise<boolean> => { | ||
try { | ||
if (await copy(source, target, overwrite)) { | ||
return await remove(source); | ||
} | ||
|
||
return false; | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
export default dir; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import remove from '@/remove'; | ||
import copy from '@/copy/file'; | ||
|
||
/** | ||
* 移动文件到新位置 | ||
* @param {string} source 源文件路径 | ||
* @param {string} target 目标文件路径 | ||
* @param {boolean} [overwrite=false] 是否覆盖已存在的文件 (whether to overwrite existing files) | ||
* @returns {Promise<boolean>} 是否移动成功 | ||
*/ | ||
const file = async (source: string, target: string, overwrite: boolean = false): Promise<boolean> => { | ||
try { | ||
if (await copy(source, target, overwrite)) { | ||
return await remove(source); | ||
} | ||
|
||
return false; | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
throw err; | ||
} | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
export default file; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters