crawlfs.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict'
  2. const { promisify } = require('util')
  3. const fs = require('./wrapped-fs')
  4. const glob = promisify(require('glob'))
  5. async function determineFileType (filename) {
  6. const stat = await fs.lstat(filename)
  7. if (stat.isFile()) {
  8. return { type: 'file', stat }
  9. } else if (stat.isDirectory()) {
  10. return { type: 'directory', stat }
  11. } else if (stat.isSymbolicLink()) {
  12. return { type: 'link', stat }
  13. }
  14. }
  15. module.exports = async function (dir, options) {
  16. const metadata = {}
  17. const crawled = await glob(dir, options)
  18. const results = await Promise.all(crawled.map(async filename => [filename, await determineFileType(filename)]))
  19. const links = []
  20. const filenames = results.map(([filename, type]) => {
  21. if (type) {
  22. metadata[filename] = type
  23. if (type.type === 'link') links.push(filename)
  24. }
  25. return filename
  26. }).filter((filename) => {
  27. // Newer glob can return files inside symlinked directories, to avoid
  28. // those appearing in archives we need to manually exclude theme here
  29. const exactLinkIndex = links.findIndex(link => filename === link)
  30. return links.every((link, index) => {
  31. if (index === exactLinkIndex) return true
  32. return !filename.startsWith(link)
  33. })
  34. })
  35. return [filenames, metadata]
  36. }
  37. module.exports.determineFileType = determineFileType