main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1021 bytes
|
Rev | Line | |
---|
[d24f17c] | 1 | 'use strict'
|
---|
| 2 | const fs = require('graceful-fs')
|
---|
| 3 | const path = require('path')
|
---|
| 4 |
|
---|
| 5 | function klawSync (dir, opts, ls) {
|
---|
| 6 | if (!ls) {
|
---|
| 7 | ls = []
|
---|
| 8 | dir = path.resolve(dir)
|
---|
| 9 | opts = opts || {}
|
---|
| 10 | opts.fs = opts.fs || fs
|
---|
| 11 | if (opts.depthLimit > -1) opts.rootDepth = dir.split(path.sep).length + 1
|
---|
| 12 | }
|
---|
| 13 | const paths = opts.fs.readdirSync(dir).map(p => dir + path.sep + p)
|
---|
| 14 | for (var i = 0; i < paths.length; i += 1) {
|
---|
| 15 | const pi = paths[i]
|
---|
| 16 | const st = opts.fs.statSync(pi)
|
---|
| 17 | const item = {path: pi, stats: st}
|
---|
| 18 | const isUnderDepthLimit = (!opts.rootDepth || pi.split(path.sep).length - opts.rootDepth < opts.depthLimit)
|
---|
| 19 | const filterResult = opts.filter ? opts.filter(item) : true
|
---|
| 20 | const isDir = st.isDirectory()
|
---|
| 21 | const shouldAdd = filterResult && (isDir ? !opts.nodir : !opts.nofile)
|
---|
| 22 | const shouldTraverse = isDir && isUnderDepthLimit && (opts.traverseAll || filterResult)
|
---|
| 23 | if (shouldAdd) ls.push(item)
|
---|
| 24 | if (shouldTraverse) ls = klawSync(pi, opts, ls)
|
---|
| 25 | }
|
---|
| 26 | return ls
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | module.exports = klawSync
|
---|
Note:
See
TracBrowser
for help on using the repository browser.