[add] ls function can recur list child folder with second para set to be true.

master 1.0.1
宇天 2017-04-24 22:38:36 +08:00
parent b31001bd93
commit 891a6cca41
4 changed files with 26 additions and 5 deletions

View File

@ -1,3 +1,7 @@
1.0.1 / 2017-04-24
* [add] ls function can recur list child folder with second para set to be true.
1.0.0 / 2017-02-26 1.0.0 / 2017-02-26
================== ==================

View File

@ -19,10 +19,12 @@
### ls(path) ### ls(path, child)
- path `<String>` - path `<String>`
- child `<Boolean>`
> List all files and folders of the path given exclude '.' and '..'. I t return an array. > List all files and folders of the path given exclude '.' and '..'. I t return an array.
> If para `child` is set to be ture, it will recur list all files of child dir.
### echo(data, file[, append][, encode]) ### echo(data, file[, append][, encode])

View File

@ -32,12 +32,27 @@ class Iofs {
/** /**
* [ls 读取整个目录(不遍历子目录)] * [ls 读取整个目录(不遍历子目录)]
* @param {string} file [目标路径] * @param {string} dir [目标路径]
* @param {boolean} child [是否遍历子目录]
* @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..'] * @return {array} [返回目标目录所有文件名和子目录名, 不包括'.''..']
*/ */
ls(file){ ls(dir, child){
try{ try{
return fs.readdirSync(file) let list = fs.readdirSync(dir)
if(!child)
return list
let tmp = Array.from(list)
tmp.forEach(it => {
let childdir = path.join(dir, it)
if(this.isdir(childdir)){
list = Array.prototype.concat.apply(list, this.ls(childdir, true).map(sub => {
return path.join(it, sub)
}))
}
})
return list
}catch(err){ }catch(err){
console.error(err) console.error(err)
return null return null

View File

@ -1,6 +1,6 @@
{ {
"name": "iofs", "name": "iofs",
"version": "1.0.0", "version": "1.0.1",
"description": "Base on native fs module, for easy using.", "description": "Base on native fs module, for easy using.",
"main": "index.js", "main": "index.js",
"repository": { "repository": {