fs.js
function walk (directoryPath, filter, collector) {
  var defer = Q.defer()
  fs.stat(directoryPath, function (err, stat) {
    if (err) {
      if (err.code === 'ENOENT') {
        // Like q-io/fs, return an empty array, if the directory does not exist
        return defer.resolve([])
      }
      return defer.reject(err)
    }
    // Call filter to get result, "true" if no filter is set
    var filterResult = !filter || filter(directoryPath, stat)
    if (filterResult) {
      collector.push(directoryPath)
    }
    // false/true => iterate directory
    if (stat.isDirectory() && filterResult !== null) {
      fs.readdir(directoryPath, function (err, filenames) {
        if (err) {
          return defer.reject(err)
        }
        var paths = filenames.map(function (name) {
          return path.join(directoryPath, name)
        })
        // Walk all files/subdirs
        Q.all(paths.map(function (filepath) {
          return walk(filepath, filter, collector)
        }))
          .then(function () {
            defer.fulfill(collector)
          })
      })
    } else {
      // No recursive call with a file
      defer.fulfill(collector)
    }
  })
  return defer.promise
}
function walk (directoryPath, filter, collector) {
  var defer = Q.defer()
  fs.stat(directoryPath, function (err, stat) {
    if (err) {
      if (err.code === 'ENOENT') {
        // Like q-io/fs, return an empty array, if the directory does not exist
        return defer.resolve([])
      }
      return defer.reject(err)
    }
    // Call filter to get result, "true" if no filter is set
    var filterResult = !filter || filter(directoryPath, stat)
    if (filterResult) {
      collector.push(directoryPath)
    }
    // false/true => iterate directory
    if (stat.isDirectory() && filterResult !== null) {
      fs.readdir(directoryPath, function (err, filenames) {
        if (err) {
          return defer.reject(err)
        }
        var paths = filenames.map(function (name) {
          return path.join(directoryPath, name)
        })
        // Walk all files/subdirs
        Q.all(paths.map(function (filepath) {
          return walk(filepath, filter, collector)
        }))
          .then(function () {
            defer.fulfill(collector)
          })
      })
    } else {
      // No recursive call with a file
      defer.fulfill(collector)
    }
  })
  return defer.promise
}
function walk ( directoryPath , filter , collector ) { var defer = Q . defer ( ) fs . stat ( directoryPath , function ( err , stat ) { if ( err ) { if ( err . code === 'ENOENT' ) { // Like q-io/fs, return an empty array, if the directory does not exist return defer . resolve ( [ ] ) } return defer . reject ( err ) } // Call filter to get result, "true" if no filter is set var filterResult = ! filter || filter ( directoryPath , stat ) if ( filterResult ) { collector . push ( directoryPath ) } // false/true => iterate directory if ( stat . isDirectory ( ) && filterResult !== null ) { fs . readdir ( directoryPath , function ( err , filenames ) { if ( err ) { return defer . reject ( err ) } var paths = filenames . map ( function ( name ) { return path . join ( directoryPath , name ) } ) // Walk all files/subdirs Q . all ( paths . map ( function ( filepath ) { return walk ( filepath , filter , collector ) } ) ) . then ( function ( ) { defer . fulfill ( collector ) } ) } ) } else { // No recursive call with a file defer . fulfill ( collector ) } } ) return defer . promise }
Walk a directory tree, collect paths of files in an Array and return a Promise for the fulfilled action
@param {string} directoryPath the base path
@param {function(string,fs.Stats):boolean} filter a function that returns true, false or null to show that a file
should be included or ignored and that a directory should be ignored completely (null)
@param {string[]} collector array to collect the filenames into
@private
@returns {Promise<string[]>} a promise for the collector, that is fulfilled after traversal
Walk a directory tree collect paths of files in an Array and return a Promise for the fulfilled action