utils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const fs = require('fs');
  3. const path = require('path');
  4. /**
  5. * Recursively read the contents of a directory.
  6. *
  7. * @param targetDir Absolute or relative path of the directory to scan. All returned paths will be relative to this
  8. * directory.
  9. * @returns Array holding all relative paths
  10. * @deprecated This function will be removed in the next major version.
  11. */
  12. function deepReadDirSync(targetDir) {
  13. const targetDirAbsPath = path.resolve(targetDir);
  14. if (!fs.existsSync(targetDirAbsPath)) {
  15. throw new Error(`Cannot read contents of ${targetDirAbsPath}. Directory does not exist.`);
  16. }
  17. if (!fs.statSync(targetDirAbsPath).isDirectory()) {
  18. throw new Error(`Cannot read contents of ${targetDirAbsPath}, because it is not a directory.`);
  19. }
  20. // This does the same thing as its containing function, `deepReadDirSync` (except that - purely for convenience - it
  21. // deals in absolute paths rather than relative ones). We need this to be separate from the outer function to preserve
  22. // the difference between `targetDirAbsPath` and `currentDirAbsPath`.
  23. const deepReadCurrentDir = (currentDirAbsPath) => {
  24. return fs.readdirSync(currentDirAbsPath).reduce((absPaths, itemName) => {
  25. const itemAbsPath = path.join(currentDirAbsPath, itemName);
  26. if (fs.statSync(itemAbsPath).isDirectory()) {
  27. return absPaths.concat(deepReadCurrentDir(itemAbsPath));
  28. }
  29. absPaths.push(itemAbsPath);
  30. return absPaths;
  31. }, []);
  32. };
  33. return deepReadCurrentDir(targetDirAbsPath).map(absPath => path.relative(targetDirAbsPath, absPath));
  34. }
  35. exports.deepReadDirSync = deepReadDirSync;
  36. //# sourceMappingURL=utils.js.map