@allurereport/directory-watcher 3.8.2 → 3.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/watcher.d.ts CHANGED
@@ -13,6 +13,7 @@ export interface Watcher {
13
13
  }
14
14
  interface WatchNewFilesOptions extends WatchOptions {
15
15
  recursive?: boolean;
16
+ maximumDepth?: number;
16
17
  indexDelay?: number;
17
18
  ignoreInitial?: boolean;
18
19
  abortController?: AbortController;
package/dist/watcher.js CHANGED
@@ -46,35 +46,43 @@ export const findMatching = async (watchDirectory, existingResults, match, maxim
46
46
  console.error("can't read directory", e);
47
47
  }
48
48
  };
49
- const findFiles = async (watchDirectory, existingResults, onNewFile, recursive) => {
50
- try {
51
- const dir = await opendir(watchDirectory, { recursive });
52
- for await (const dirent of dir) {
53
- if (dirent.isDirectory()) {
54
- continue;
55
- }
56
- const path = join(dirent.parentPath ?? dirent.path, dirent.name);
57
- if (existingResults.has(path)) {
58
- continue;
59
- }
60
- try {
61
- await onNewFile(path, dirent);
62
- existingResults.add(path);
63
- }
64
- catch (e) {
65
- if (!isFileNotFoundError(e)) {
66
- console.error("can't process file", path, e);
49
+ const findFiles = async (watchDirectory, existingResults, onNewFile, recursive, maximumDepth = 10) => {
50
+ const scanDirectory = async (directory, isRoot, remainingDepth) => {
51
+ try {
52
+ const dir = await opendir(directory);
53
+ for await (const dirent of dir) {
54
+ const path = join(dirent.parentPath ?? dirent.path, dirent.name);
55
+ if (dirent.isDirectory()) {
56
+ if (recursive && remainingDepth > 0) {
57
+ await scanDirectory(path, false, remainingDepth - 1);
58
+ }
59
+ continue;
60
+ }
61
+ if (existingResults.has(path)) {
62
+ continue;
63
+ }
64
+ try {
65
+ await onNewFile(path, dirent);
66
+ existingResults.add(path);
67
+ }
68
+ catch (e) {
69
+ if (!isFileNotFoundError(e)) {
70
+ console.error("can't process file", path, e);
71
+ }
67
72
  }
68
73
  }
69
74
  }
70
- }
71
- catch (e) {
72
- if (isFileNotFoundError(e)) {
73
- existingResults.clear();
74
- return;
75
+ catch (e) {
76
+ if (isFileNotFoundError(e)) {
77
+ if (isRoot) {
78
+ existingResults.clear();
79
+ }
80
+ return;
81
+ }
82
+ console.error("can't read directory", e);
75
83
  }
76
- console.error("can't read directory", e);
77
- }
84
+ };
85
+ await scanDirectory(watchDirectory, true, maximumDepth);
78
86
  };
79
87
  const singleIteration = async (callback, ...ac) => {
80
88
  return setImmediate(undefined, { signal: AbortSignal.any(ac.map((c) => c.signal)) })
@@ -124,13 +132,13 @@ const watch = (initialCallback, iterationCallback, doneCallback, options = {}) =
124
132
  };
125
133
  };
126
134
  export const newFilesInDirectoryWatcher = (directory, onNewFile, options = {}) => {
127
- const { recursive = true, ignoreInitial = false, ...rest } = options;
135
+ const { recursive = true, maximumDepth = 10, ignoreInitial = false, ...rest } = options;
128
136
  const indexedFiles = new Set();
129
137
  const initialCallback = async () => {
130
- await findFiles(directory, indexedFiles, ignoreInitial ? noop : onNewFile, recursive);
138
+ await findFiles(directory, indexedFiles, ignoreInitial ? noop : onNewFile, recursive, maximumDepth);
131
139
  };
132
140
  const iterationCallback = async () => {
133
- await findFiles(directory, indexedFiles, onNewFile, recursive);
141
+ await findFiles(directory, indexedFiles, onNewFile, recursive, maximumDepth);
134
142
  };
135
143
  return watch(initialCallback, iterationCallback, iterationCallback, rest);
136
144
  };
@@ -174,7 +182,12 @@ const waitUntilFileStopChanging = async (file, info, options) => {
174
182
  }
175
183
  const sinceChange = now - prev.timestamp;
176
184
  if (sinceChange < minWait) {
177
- await setTimeout(Math.min(0, maxWait, minWait - sinceChange + 1));
185
+ const maxDelay = maxWait - (now - start);
186
+ if (maxDelay <= 0) {
187
+ return false;
188
+ }
189
+ await setTimeout(Math.min(maxDelay, minWait - sinceChange + 1));
190
+ continue;
178
191
  }
179
192
  const current = await calculateInfo(file);
180
193
  if (!current) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allurereport/directory-watcher",
3
- "version": "3.8.2",
3
+ "version": "3.9.0",
4
4
  "description": "File system watcher for directories",
5
5
  "keywords": [
6
6
  "allure",
@@ -35,6 +35,7 @@
35
35
  "devDependencies": {
36
36
  "@types/node": "^20.17.9",
37
37
  "@vitest/runner": "^2.1.9",
38
+ "allure-js-commons": "^3.3.3",
38
39
  "allure-vitest": "^3.3.3",
39
40
  "rimraf": "^6.0.1",
40
41
  "ts-node": "^10.9.2",