How to use treeverse - 4 common examples

To help you get started, we’ve selected a few treeverse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github npm / arborist / lib / arborist.js View on Github external
removeRetiredAndDeletedNodes () {
    const moves = this.reifyRetiredNodes
    const promises = []
    const failures = []
    const rm = path => rimraf(path)
      .catch(
        /* istanbul ignore next - hard to make rimraf fail */
        er => failures.push([path, er])
      )

    // XXX get list of removal nodes in an earlier walk instead of here
    dfwalk({
      tree: this.diff,
      visit: diff => {
        if (diff.action === 'REMOVE')
          promises.push(rm(diff.actual.path))
      },
      getChildren: diff => diff.action === 'REMOVE' ? [] : diff.children,
    })

    for (const [realPath, retirePath] of Object.entries(moves)) {
      promises.push(rm(retirePath))
    }

    return promiseAllRejectLate(promises).then(() => {
      /* istanbul ignore if - hard to make rimraf fail */
      if (failures.length)
        this.emit('warn', 'Failed to clean up some directories', failures)
github npm / arborist / lib / arborist.js View on Github external
unpackNewModules () {
    const unpacks = []
    dfwalk({
      tree: this.diff,
      filter: diff => diff.ideal,
      visit: diff => {
        const node = diff.ideal
        const bd = node.package.bundleDependencies
        const sw = node.hasShrinkwrap
        // should inBundle differentiate if it's in the root's bundle?
        // because in that case, it should still be installed.
        if (node && !node.isRoot && !(bd && bd.length) && !sw && !node.inBundle)
          unpacks.push(this.reifyNode(node))
      },
      getChildren: diff => diff.children,
    })
    return promiseAllRejectLate(unpacks)
      .catch(er => this.rollbackCreateSparseTree(er))
  }
github npm / arborist / lib / arborist.js View on Github external
getBundlesByDepth () {
    const bundlesByDepth = new Map()
    let maxBundleDepth = -1
    dfwalk({
      tree: this.diff,
      visit: diff => {
        const node = diff.ideal
        if (node && !node.isRoot && node.package.bundleDependencies &&
            node.package.bundleDependencies.length) {
          maxBundleDepth = Math.max(maxBundleDepth, node.depth)
          if (!bundlesByDepth.has(node.depth))
            bundlesByDepth.set(node.depth, [node])
          else
            bundlesByDepth.get(node.depth).push(node)
        }
      },
      getChildren: diff => diff.children,
    })

    bundlesByDepth.set('maxBundleDepth', maxBundleDepth)
github npm / arborist / lib / diff.js View on Github external
static calculate ({actual, ideal}) {
    return depth({
      tree: new Diff({actual, ideal}),
      getChildren,
      leave,
    })
  }
}

treeverse

Walk any kind of tree structure depth- or breadth-first. Supports promises and advanced map-reduce operations with a very small API.

ISC
Latest version published 1 month ago

Package Health Score

86 / 100
Full package analysis

Popular treeverse functions