From 66ff70f3c62bd0c9f446b2e7bba836d387c9205c Mon Sep 17 00:00:00 2001 From: MoonLeeeaf <150461955+MoonLeeeaf@users.noreply.github.com> Date: Sun, 16 Mar 2025 23:48:53 +0800 Subject: [PATCH] =?UTF-8?q?chore(lib:=20io):=20=E5=85=81=E8=AE=B8=E4=BB=85?= =?UTF-8?q?=E5=88=97=E4=B8=BE=E6=96=87=E4=BB=B6=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/lib/io.js | 53 +++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/server/lib/io.js b/server/lib/io.js index 29a90d8..5ffacf6 100644 --- a/server/lib/io.js +++ b/server/lib/io.js @@ -26,7 +26,7 @@ export default class io { */ static open(path, mode) { if (!mode || mode == '') - throw new Error('当前文件对象未设置属性!') + throw new Error('当前文件对象未设置属性!') return new io(path, mode) } /** @@ -40,41 +40,56 @@ export default class io { /** * 枚举目录下所有文件 * @param { String } 扫描路径 - * @param { Function } 过滤器<文件完整路径> - * @param { Boolean } 是否搜索文件夹内的文件 - * @returns { String[] } 文件完整路径列表 + * @param { Object } extra 额外参数 + * @param { Function } [extra.filter] 过滤器<文件路径> + * @param { Boolean } [extra.recursive] 是否搜索文件夹内的文件 + * @param { Boolean } [extra.fullPath] 是否返回完整文件路径 + * @returns { String[] } 文件路径列表 */ - static listFiles(path, { filter, recursive } = {}) { + static listFiles(path, { filter, recursive = false, fullPath = true } = {}) { let a = fs.readdirSync(path, { recursive: recursive }) - a.forEach(function(v, index, arrayThis) { + a.forEach(function (v, index, arrayThis) { arrayThis[index] = `${path}//${v}` }) - return a.filter(function(v) { + + a = a.filter(function (v) { if (!fs.lstatSync(v).isFile()) return false if (filter) return filter(v) return true }) + if (!fullPath) + a.forEach(function (v, index, arrayThis) { + arrayThis[index] = v.substring(v.lastIndexOf('/') + 1) + }) + return a } /** * 枚举目录下所有文件夹 * @param { String } 扫描路径 - * @param { Object } 额外参数 - * @param { Function } [filter] 额外参数.过滤器<文件夹完整路径> - * @param { Boolean } [recursive] 额外参数.是否搜索文件夹内的文件夹 - * @returns { String[] } 文件夹完整路径列表 + * @param { Object } extra 额外参数 + * @param { Function } [extra.filter] 过滤器<文件夹路径> + * @param { Boolean } [extra.recursive] 是否搜索文件夹内的文件夹 + * @param { Boolean } [extra.fullPath] 是否返回完整文件路径 + * @returns { String[] } 文件夹路径列表 */ - static listFolders(path, { filter, recursive } = {}) { + static listFolders(path, { filter, recursive = false, fullPath = true } = {}) { let a = fs.readdirSync(path, { recursive: recursive }) - a.forEach(function(v, index, arrayThis) { + a.forEach(function (v, index, arrayThis) { arrayThis[index] = `${path}//${v}` }) - return a.filter(function(v) { + + a = a.filter(function (v) { if (!fs.lstatSync(v).isDirectory()) return false if (filter) return filter(v) return true }) + if (!fullPath) + a.forEach(function (v, index, arrayThis) { + arrayThis[index] = v.substring(v.lastIndexOf('/') + 1) + }) + return a } /** * 获取文件(夹)的全名 @@ -95,7 +110,7 @@ export default class io { * @param { String } path * @returns { String } parentPath */ - static getParent(path) { + static getParent(path) { return path.substring(0, path.lastIndexOf(this.getName(path)) - 1) } /** @@ -105,10 +120,10 @@ export default class io { */ static copyDir(from, to) { this.mkdirs(to) - this.listFiles(from).forEach(function(v) { + this.listFiles(from).forEach(function (v) { io.open(v, 'r').pipe(io.open(`${to}//${io.getName(v)}`, 'w')).close() }) - this.listFolders(from).forEach(function(v) { + this.listFolders(from).forEach(function (v) { io.copyDir(v, `${to}//${io.getName(v)}`) }) } @@ -184,7 +199,7 @@ export default class io { let r if (this.r) r = this.readAll() - else + else throw new Error('当前文件对象未设置可读!') this.close() return r @@ -232,7 +247,7 @@ export default class io { let r if (this.r) r = JSON.parse(this.readAll().toString()) - else + else throw new Error('当前文件对象未设置可读!') this.close() return r