1
0

file.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import path from "path"
  2. import fs from "fs"
  3. export default {
  4. getAllFilePaths
  5. }
  6. export function getAllFilePaths(dir) {
  7. return _getAllFilePaths(dir, './', 5)
  8. }
  9. function _getAllFilePaths(filePath, localpath, deep) {
  10. if (deep === 0) {
  11. return [], []
  12. }
  13. let filePathResult = []
  14. let localPathResult = []
  15. // 读取目录中的所有文件和子目录
  16. fs.readdirSync(filePath).forEach(function (file) {
  17. const newFilePath = path.join(filePath, file)
  18. const newLocalPath = path.join(localpath, file)
  19. // 如果是目录,则递归读取该目录下的文件
  20. if (fs.statSync(newFilePath).isDirectory()) {
  21. const { filePathResult: _filrPath, localPathResult: _localPath } = _getAllFilePaths(
  22. newFilePath,
  23. newLocalPath,
  24. deep - 1
  25. )
  26. filePathResult = filePathResult.concat(_filrPath)
  27. localPathResult = localPathResult.concat(_localPath)
  28. } else {
  29. // 如果是文件,则直接加入结果数组
  30. filePathResult.push(newFilePath)
  31. localPathResult.push(newLocalPath)
  32. }
  33. })
  34. return {
  35. filePathResult,
  36. localPathResult
  37. }
  38. }