A reactive nodejs directory walker. More flexible control via filename and recursive depth.
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 // <--- walk with maxDepth
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} `)
},
)
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 // <--- filter filename/dirname
})
}
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} `)
},
)
// stop running walker conditionally
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)
}
}
)
// no follow symbol link
import { walk } from 'rxwalker'
walk('<path>', { followLink: false }).subscribe(
data => console.log(data),
)