diff --git a/History.md b/History.md index 1b2e094..3c9dc26 100644 --- a/History.md +++ b/History.md @@ -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 ================== diff --git a/Readme.md b/Readme.md index 179f8b3..ea22394 100644 --- a/Readme.md +++ b/Readme.md @@ -19,10 +19,12 @@ -### ls(path) +### ls(path, child) - path `` +- child `` > 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]) diff --git a/index.js b/index.js index 0a2aae1..7f3a3c3 100644 --- a/index.js +++ b/index.js @@ -32,12 +32,27 @@ class Iofs { /** * [ls 读取整个目录(不遍历子目录)] - * @param {string} file [目标路径] + * @param {string} dir [目标路径] + * @param {boolean} child [是否遍历子目录] * @return {array} [返回目标目录所有文件名和子目录名, 不包括'.'和'..'] */ - ls(file){ + ls(dir, child){ 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){ console.error(err) return null diff --git a/package.json b/package.json index 5507606..d19d104 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iofs", - "version": "1.0.0", + "version": "1.0.1", "description": "Base on native fs module, for easy using.", "main": "index.js", "repository": {