chore(lib: io): 允许仅列举文件名称
This commit is contained in:
@@ -26,7 +26,7 @@ export default class io {
|
|||||||
*/
|
*/
|
||||||
static open(path, mode) {
|
static open(path, mode) {
|
||||||
if (!mode || mode == '')
|
if (!mode || mode == '')
|
||||||
throw new Error('当前文件对象未设置属性!')
|
throw new Error('当前文件对象未设置属性!')
|
||||||
return new io(path, mode)
|
return new io(path, mode)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -40,41 +40,56 @@ export default class io {
|
|||||||
/**
|
/**
|
||||||
* 枚举目录下所有文件
|
* 枚举目录下所有文件
|
||||||
* @param { String } 扫描路径
|
* @param { String } 扫描路径
|
||||||
* @param { Function<String> } 过滤器<文件完整路径>
|
* @param { Object } extra 额外参数
|
||||||
* @param { Boolean } 是否搜索文件夹内的文件
|
* @param { Function<String> } [extra.filter] 过滤器<文件路径>
|
||||||
* @returns { String[] } 文件完整路径列表
|
* @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 })
|
let a = fs.readdirSync(path, { recursive: recursive })
|
||||||
a.forEach(function(v, index, arrayThis) {
|
a.forEach(function (v, index, arrayThis) {
|
||||||
arrayThis[index] = `${path}//${v}`
|
arrayThis[index] = `${path}//${v}`
|
||||||
})
|
})
|
||||||
return a.filter(function(v) {
|
|
||||||
|
a = a.filter(function (v) {
|
||||||
if (!fs.lstatSync(v).isFile()) return false
|
if (!fs.lstatSync(v).isFile()) return false
|
||||||
|
|
||||||
if (filter) return filter(v)
|
if (filter) return filter(v)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
if (!fullPath)
|
||||||
|
a.forEach(function (v, index, arrayThis) {
|
||||||
|
arrayThis[index] = v.substring(v.lastIndexOf('/') + 1)
|
||||||
|
})
|
||||||
|
return a
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 枚举目录下所有文件夹
|
* 枚举目录下所有文件夹
|
||||||
* @param { String } 扫描路径
|
* @param { String } 扫描路径
|
||||||
* @param { Object } 额外参数
|
* @param { Object } extra 额外参数
|
||||||
* @param { Function<String> } [filter] 额外参数.过滤器<文件夹完整路径>
|
* @param { Function<String> } [extra.filter] 过滤器<文件夹路径>
|
||||||
* @param { Boolean } [recursive] 额外参数.是否搜索文件夹内的文件夹
|
* @param { Boolean } [extra.recursive] 是否搜索文件夹内的文件夹
|
||||||
* @returns { String[] } 文件夹完整路径列表
|
* @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 })
|
let a = fs.readdirSync(path, { recursive: recursive })
|
||||||
a.forEach(function(v, index, arrayThis) {
|
a.forEach(function (v, index, arrayThis) {
|
||||||
arrayThis[index] = `${path}//${v}`
|
arrayThis[index] = `${path}//${v}`
|
||||||
})
|
})
|
||||||
return a.filter(function(v) {
|
|
||||||
|
a = a.filter(function (v) {
|
||||||
if (!fs.lstatSync(v).isDirectory()) return false
|
if (!fs.lstatSync(v).isDirectory()) return false
|
||||||
|
|
||||||
if (filter) return filter(v)
|
if (filter) return filter(v)
|
||||||
return true
|
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
|
* @param { String } path
|
||||||
* @returns { String } parentPath
|
* @returns { String } parentPath
|
||||||
*/
|
*/
|
||||||
static getParent(path) {
|
static getParent(path) {
|
||||||
return path.substring(0, path.lastIndexOf(this.getName(path)) - 1)
|
return path.substring(0, path.lastIndexOf(this.getName(path)) - 1)
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -105,10 +120,10 @@ export default class io {
|
|||||||
*/
|
*/
|
||||||
static copyDir(from, to) {
|
static copyDir(from, to) {
|
||||||
this.mkdirs(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()
|
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)}`)
|
io.copyDir(v, `${to}//${io.getName(v)}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -184,7 +199,7 @@ export default class io {
|
|||||||
let r
|
let r
|
||||||
if (this.r)
|
if (this.r)
|
||||||
r = this.readAll()
|
r = this.readAll()
|
||||||
else
|
else
|
||||||
throw new Error('当前文件对象未设置可读!')
|
throw new Error('当前文件对象未设置可读!')
|
||||||
this.close()
|
this.close()
|
||||||
return r
|
return r
|
||||||
@@ -232,7 +247,7 @@ export default class io {
|
|||||||
let r
|
let r
|
||||||
if (this.r)
|
if (this.r)
|
||||||
r = JSON.parse(this.readAll().toString())
|
r = JSON.parse(this.readAll().toString())
|
||||||
else
|
else
|
||||||
throw new Error('当前文件对象未设置可读!')
|
throw new Error('当前文件对象未设置可读!')
|
||||||
this.close()
|
this.close()
|
||||||
return r
|
return r
|
||||||
|
|||||||
Reference in New Issue
Block a user