walker.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.Walker = void 0;
  27. const path_1 = require("path");
  28. const utils_1 = require("../utils");
  29. const joinPath = __importStar(require("./functions/join-path"));
  30. const pushDirectory = __importStar(require("./functions/push-directory"));
  31. const pushFile = __importStar(require("./functions/push-file"));
  32. const getArray = __importStar(require("./functions/get-array"));
  33. const groupFiles = __importStar(require("./functions/group-files"));
  34. const resolveSymlink = __importStar(require("./functions/resolve-symlink"));
  35. const invokeCallback = __importStar(require("./functions/invoke-callback"));
  36. const walkDirectory = __importStar(require("./functions/walk-directory"));
  37. const queue_1 = require("./queue");
  38. const counter_1 = require("./counter");
  39. class Walker {
  40. root;
  41. isSynchronous;
  42. state;
  43. joinPath;
  44. pushDirectory;
  45. pushFile;
  46. getArray;
  47. groupFiles;
  48. resolveSymlink;
  49. walkDirectory;
  50. callbackInvoker;
  51. constructor(root, options, callback) {
  52. this.isSynchronous = !callback;
  53. this.callbackInvoker = invokeCallback.build(options, this.isSynchronous);
  54. this.root = (0, utils_1.normalizePath)(root, options);
  55. this.state = {
  56. root: this.root.slice(0, -1),
  57. // Perf: we explicitly tell the compiler to optimize for String arrays
  58. paths: [""].slice(0, 0),
  59. groups: [],
  60. counts: new counter_1.Counter(),
  61. options,
  62. queue: new queue_1.Queue((error, state) => this.callbackInvoker(state, error, callback)),
  63. symlinks: new Map(),
  64. visited: [""].slice(0, 0),
  65. };
  66. /*
  67. * Perf: We conditionally change functions according to options. This gives a slight
  68. * performance boost. Since these functions are so small, they are automatically inlined
  69. * by the javascript engine so there's no function call overhead (in most cases).
  70. */
  71. this.joinPath = joinPath.build(this.root, options);
  72. this.pushDirectory = pushDirectory.build(this.root, options);
  73. this.pushFile = pushFile.build(options);
  74. this.getArray = getArray.build(options);
  75. this.groupFiles = groupFiles.build(options);
  76. this.resolveSymlink = resolveSymlink.build(options, this.isSynchronous);
  77. this.walkDirectory = walkDirectory.build(this.isSynchronous);
  78. }
  79. start() {
  80. this.walkDirectory(this.state, this.root, this.root, this.state.options.maxDepth, this.walk);
  81. return this.isSynchronous ? this.callbackInvoker(this.state, null) : null;
  82. }
  83. walk = (entries, directoryPath, depth) => {
  84. const { paths, options: { filters, resolveSymlinks, excludeSymlinks, exclude, maxFiles, signal, useRealPaths, pathSeparator, }, } = this.state;
  85. if ((signal && signal.aborted) || (maxFiles && paths.length > maxFiles))
  86. return;
  87. this.pushDirectory(directoryPath, paths, filters);
  88. const files = this.getArray(this.state.paths);
  89. for (let i = 0; i < entries.length; ++i) {
  90. const entry = entries[i];
  91. if (entry.isFile() ||
  92. (entry.isSymbolicLink() && !resolveSymlinks && !excludeSymlinks)) {
  93. const filename = this.joinPath(entry.name, directoryPath);
  94. this.pushFile(filename, files, this.state.counts, filters);
  95. }
  96. else if (entry.isDirectory()) {
  97. let path = joinPath.joinDirectoryPath(entry.name, directoryPath, this.state.options.pathSeparator);
  98. if (exclude && exclude(entry.name, path))
  99. continue;
  100. this.walkDirectory(this.state, path, path, depth - 1, this.walk);
  101. }
  102. else if (entry.isSymbolicLink() && this.resolveSymlink) {
  103. let path = joinPath.joinPathWithBasePath(entry.name, directoryPath);
  104. this.resolveSymlink(path, this.state, (stat, resolvedPath) => {
  105. if (stat.isDirectory()) {
  106. resolvedPath = (0, utils_1.normalizePath)(resolvedPath, this.state.options);
  107. if (exclude && exclude(entry.name, useRealPaths ? resolvedPath : path + pathSeparator))
  108. return;
  109. this.walkDirectory(this.state, resolvedPath, useRealPaths ? resolvedPath : path + pathSeparator, depth - 1, this.walk);
  110. }
  111. else {
  112. resolvedPath = useRealPaths ? resolvedPath : path;
  113. const filename = (0, path_1.basename)(resolvedPath);
  114. const directoryPath = (0, utils_1.normalizePath)((0, path_1.dirname)(resolvedPath), this.state.options);
  115. resolvedPath = this.joinPath(filename, directoryPath);
  116. this.pushFile(resolvedPath, files, this.state.counts, filters);
  117. }
  118. });
  119. }
  120. }
  121. this.groupFiles(this.state.groups, directoryPath, files);
  122. };
  123. }
  124. exports.Walker = Walker;