walker.js 6.6 KB

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