rxwalker
TypeScript icon, indicating that this package has built-in type declarations

7.0.1 • Public • Published

A reactive nodejs directory walker. More flexible control via filename and recursive depth.

Version License ci Build status codecov Conventional Commits

Installing

npm install --save rxwalker

Usage

// 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),
)

Demo

License

MIT

Languages

Package Sidebar

Install

npm i rxwalker

Weekly Downloads

36

Version

7.0.1

License

MIT

Unpacked Size

40 kB

Total Files

31

Last publish

Collaborators

  • waiting