rxwalker
v7.0.1
Published
A simple directory tree walker by rxjs
Downloads
171
Maintainers
Readme
rxwalker
响应式递归读取目录及文件,通过过滤回调函数可实现更灵活的搜索控制
安装
npm install --save rxwalker
使用
// js
const walk = require('rxwalker').walk
walk('<path>').subscribe(
data => console.info(data),
)
// es6 or typescript
import { walk } from 'rxwalker'
walk('<path>').subscribe(
data => console.info(data),
)
import { walk, EntryType } from 'rxwalker'
const maxDepth = 7 // <--- 递归最大深度
let dirCount = 0
let fileCount = 0
let linkCount = 0
let entryCount = 0
walk('<path>', { maxDepth }).subscribe(
data => {
switch (data.type) {
case EntryType.dir:
console.info('got a dir', data)
dirCount += 1
entryCount += 1
break
case EntryType.file:
console.info('got a file', data)
fileCount += 1
entryCount += 1
break
case EntryType.link:
console.info('got a symbolicLink', data)
linkCount += 1
// entryCount += 1
break
case EntryType.noAcessPermission:
console.info('got a entry without access permission', data)
entryCount += 1
break
case EntryType.unknown:
console.info('got a unknow entry', data)
break
case EntryType.notExist:
console.error('entry not exists:', data.path)
break
}
},
err => console.error(err),
() => {
console.info(`count result dirs: ${dirCount}, files: ${fileCount}, links: ${linkCount}, entries: ${entryCount} `)
},
)
// typescript
import { walk, DirFilterCbParams, DirFilterCbRet, EntryType } from 'rxwalker'
let dirCount = 0
let fileCount = 0
let linkCount = 0
let entryCount = 0
const dirFilterCb = ({ files }: DirFilterCbParams): DirFilterCbRet => {
return files.filter(file => {
return file.includes('a') ? true : false // <--- 过滤文件名、目录名
})
}
walk('<path>', { dirFilterCb }).subscribe(
data => {
switch (data.type) {
case EntryType.dir:
console.info('got a dir', data)
dirCount += 1
entryCount += 1
break
case EntryType.file:
console.info('got a file', data)
fileCount += 1
entryCount += 1
break
case EntryType.link:
console.info('got a symbolicLink', data)
linkCount += 1
// entryCount += 1
break
case EntryType.noAcessPermission:
console.info('got a entry without access permission', data)
entryCount += 1
break
case EntryType.unknown:
console.info('got a unknow entry', data)
break
case EntryType.notExist:
console.error('entry not exists:', data.path)
break
}
},
err => console.error(err),
() => {
console.info(`count result dirs: ${dirCount}, files: ${fileCount}, links: ${linkCount}, entries: ${entryCount} `)
},
)
// 根据条件终止搜索
import { walk } from 'rxwalker'
const sub = walk('<path>').subscribe(
data => {
if (data.path.includes('a')) {
sub.unsubscribe()
console.log('stopped with:', data)
}
else {
console.log(data)
}
}
)
// Do not walk symbolicLink
import { walk } from 'rxwalker'
walk('<path>', { followLink: false }).subscribe(
data => console.log(data),
)