src/commands/walkBeta1.js
/**
 * A powerful recursive tree-walking utility.
 *
 * @link https://isomorphic-git.org/docs/en/walkBeta1
 */
A powerful recursive tree-walking utility.
A powerful recursive tree - walking utility .
async function walkBeta1 ({
  core = 'default',
  trees,
  filter = async () => true,
  map = async entry => entry,
  reduce = async (parent, children) => {
    let flatten = children.reduce((acc, x) => acc.concat(x), [])
    if (parent !== undefined) flatten.unshift(parent)
    return flatten
  },
  iterate = (walk, children) => Promise.all([...children].map(walk))
}) {
  try {
    let walkers = trees.map(proxy => proxy[GitWalkerSymbol]())
    let root = new Array(walkers.length).fill({
      fullpath: '.',
      basename: '.',
      exists: true
    })
    const range = arrayRange(0, walkers.length)
    const unionWalkerFromReaddir = async entry => {
      const subdirs = await Promise.all(
        range.map(i => walkers[i].readdir(entry[i]))
      )
      range.map(i => {
        entry[i] = new walkers[i].ConstructEntry(entry[i])
      })
      let iterators = subdirs
        .map(array => (array === null ? [] : array))
        .map(array => array[Symbol.iterator]())
      return {
        entry,
        children: unionOfIterators(iterators)
      }
    }
    const walk = async root => {
      let { children, entry } = await unionWalkerFromReaddir(root)
      if (await filter(entry)) {
        let parent = await map(entry)
        children = await iterate(walk, children)
        children = children.filter(x => x !== undefined)
        return reduce(parent, children)
      }
    }
    return walk(root)
  } catch (err) {
    err.caller = 'git.walk'
    throw err
  }
}
async function walkBeta1 ( { core = 'default' , trees , filter = async ( ) => true , map = async entry => entry , reduce = async ( parent , children ) => { let flatten = children . reduce ( ( acc , x ) => acc . concat ( x ) , [ ] ) if ( parent !== undefined ) flatten . unshift ( parent ) return flatten } , iterate = ( walk , children ) => Promise . all ( [ ... children ] . map ( walk ) ) } ) { try { let walkers = trees . map ( proxy => proxy [ GitWalkerSymbol ] ( ) ) let root = new Array ( walkers . length ) . fill ( { fullpath : '.' , basename : '.' , exists : true } ) const range = arrayRange ( 0 , walkers . length ) const unionWalkerFromReaddir = async entry => { const subdirs = await Promise . all ( range . map ( i => walkers [ i ] . readdir ( entry [ i ] ) ) ) range . map ( i => { entry [ i ] = new walkers [ i ] . ConstructEntry ( entry [ i ] ) } ) let iterators = subdirs . map ( array => ( array === null ? [ ] : array ) ) . map ( array => array [ Symbol . iterator ] ( ) ) return { entry , children : unionOfIterators ( iterators ) } } const walk = async root => { let { children , entry } = await unionWalkerFromReaddir ( root ) if ( await filter ( entry ) ) { let parent = await map ( entry ) children = await iterate ( walk , children ) children = children . filter ( x => x !== undefined ) return reduce ( parent , children ) } } return walk ( root ) } catch ( err ) { err . caller = 'git.walk' throw err } }
A powerful recursive tree-walking utility.
A powerful recursive tree - walking utility .
// The default reducer is a flatmap that filters out undefineds. // TODO: replace with `[parent, children].flat()` once that gets standardized // The default iterate function walks all children concurrently // Now process child directories