@mintlify/previewing 4.0.1664 → 4.0.1666
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/__tests__/export-archive.test.d.ts +1 -0
- package/dist/__tests__/export-archive.test.js +53 -0
- package/dist/__tests__/listener-batching.test.js +11 -13
- package/dist/__tests__/listener-graphql-packages.test.js +9 -11
- package/dist/__tests__/listener-package-manifest.test.js +9 -11
- package/dist/__tests__/page-metadata-cache.test.js +17 -1
- package/dist/__tests__/watcher-native.test.d.ts +1 -0
- package/dist/__tests__/watcher-native.test.js +61 -0
- package/dist/__tests__/watcher.test.d.ts +1 -0
- package/dist/__tests__/watcher.test.js +237 -0
- package/dist/local-preview/export-archive.d.ts +9 -0
- package/dist/local-preview/export-archive.js +51 -0
- package/dist/local-preview/export.js +12 -5
- package/dist/local-preview/listener/index.js +7 -10
- package/dist/local-preview/listener/page-metadata-cache.js +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/watcher.d.ts +14 -0
- package/dist/watcher.js +267 -0
- package/package.json +10 -7
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type WatchEventType = 'add' | 'change' | 'unlink';
|
|
2
|
+
export type WatchTreeOptions = {
|
|
3
|
+
ignored: (absolutePath: string, stats?: {
|
|
4
|
+
isDirectory: () => boolean;
|
|
5
|
+
}) => boolean;
|
|
6
|
+
followSymlinks?: boolean;
|
|
7
|
+
onEvent: (type: WatchEventType, absolutePath: string) => unknown;
|
|
8
|
+
onError?: (error: unknown) => void;
|
|
9
|
+
onNativeUnavailable?: (error: unknown) => void;
|
|
10
|
+
};
|
|
11
|
+
export type TreeWatcher = {
|
|
12
|
+
close: () => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
export declare const watchTree: (root: string, options: WatchTreeOptions) => Promise<TreeWatcher>;
|
package/dist/watcher.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import chokidar from 'chokidar';
|
|
2
|
+
import { lstat, readdir, realpath, stat } from 'fs/promises';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
class SymlinkError extends Error {
|
|
5
|
+
}
|
|
6
|
+
const ATOMIC_WINDOW_MS = 100;
|
|
7
|
+
const MTIME_MARGIN_MS = 1000;
|
|
8
|
+
const BACKENDS = {
|
|
9
|
+
darwin: 'fs-events',
|
|
10
|
+
linux: 'inotify',
|
|
11
|
+
win32: 'windows',
|
|
12
|
+
};
|
|
13
|
+
const isNativeEnabled = () => process.platform in BACKENDS && process.env.MINTLIFY_DISABLE_NATIVE_WATCHER !== '1';
|
|
14
|
+
const watchWithChokidar = (root, options, snapshot) => new Promise((resolve, reject) => {
|
|
15
|
+
let ready = false;
|
|
16
|
+
const known = new Set(snapshot?.files);
|
|
17
|
+
const add = (file, stats) => {
|
|
18
|
+
if (ready || !known.delete(file))
|
|
19
|
+
return options.onEvent('add', file);
|
|
20
|
+
if (snapshot && stats && stats.mtimeMs >= snapshot.since)
|
|
21
|
+
options.onEvent('change', file);
|
|
22
|
+
};
|
|
23
|
+
const watcher = chokidar
|
|
24
|
+
.watch(root, {
|
|
25
|
+
ignoreInitial: !snapshot,
|
|
26
|
+
alwaysStat: !!snapshot,
|
|
27
|
+
followSymlinks: options.followSymlinks ?? true,
|
|
28
|
+
ignored: options.ignored,
|
|
29
|
+
})
|
|
30
|
+
.on('add', add)
|
|
31
|
+
.on('change', (file) => options.onEvent('change', file))
|
|
32
|
+
.on('unlink', (file) => options.onEvent('unlink', file))
|
|
33
|
+
.on('error', (error) => {
|
|
34
|
+
if (ready)
|
|
35
|
+
return options.onError?.(error);
|
|
36
|
+
void watcher.close();
|
|
37
|
+
reject(error);
|
|
38
|
+
})
|
|
39
|
+
.once('ready', () => {
|
|
40
|
+
ready = true;
|
|
41
|
+
for (const file of known)
|
|
42
|
+
options.onEvent('unlink', file);
|
|
43
|
+
known.clear();
|
|
44
|
+
resolve({ close: () => watcher.close() });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
const watchNatively = async (root, options, onFailure) => {
|
|
48
|
+
const { subscribe } = (await import('@parcel/watcher')).default;
|
|
49
|
+
const realRoot = await realpath(root);
|
|
50
|
+
const follow = options.followSymlinks ?? true;
|
|
51
|
+
const directories = new Map();
|
|
52
|
+
const ignoredDirectories = new Set();
|
|
53
|
+
const toWatched = realRoot === root
|
|
54
|
+
? (realPath) => realPath
|
|
55
|
+
: (realPath) => path.join(root, path.relative(realRoot, realPath));
|
|
56
|
+
const emit = (type, realPath) => options.onEvent(type, toWatched(realPath));
|
|
57
|
+
const ignored = (realPath, isDirectory) => options.ignored(toWatched(realPath), { isDirectory: () => isDirectory });
|
|
58
|
+
const rejectSymlink = async (realPath) => {
|
|
59
|
+
if (!follow)
|
|
60
|
+
return;
|
|
61
|
+
const isDirectory = await stat(realPath).then((info) => info.isDirectory(), () => false);
|
|
62
|
+
if (!ignored(realPath, isDirectory))
|
|
63
|
+
throw new SymlinkError();
|
|
64
|
+
};
|
|
65
|
+
const scan = async (directory, files) => {
|
|
66
|
+
const names = new Set();
|
|
67
|
+
directories.set(directory, names);
|
|
68
|
+
const entries = await readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
69
|
+
const pending = [];
|
|
70
|
+
for (const entry of entries) {
|
|
71
|
+
const child = path.join(directory, entry.name);
|
|
72
|
+
if (entry.isSymbolicLink()) {
|
|
73
|
+
pending.push(rejectSymlink(child));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const isDirectory = entry.isDirectory();
|
|
77
|
+
if (!isDirectory && !entry.isFile())
|
|
78
|
+
continue;
|
|
79
|
+
if (ignored(child, isDirectory)) {
|
|
80
|
+
if (isDirectory)
|
|
81
|
+
ignoredDirectories.add(child);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
names.add(entry.name);
|
|
85
|
+
if (isDirectory)
|
|
86
|
+
pending.push(scan(child, files));
|
|
87
|
+
else
|
|
88
|
+
files?.push(child);
|
|
89
|
+
}
|
|
90
|
+
await Promise.all(pending);
|
|
91
|
+
};
|
|
92
|
+
const remove = (target) => {
|
|
93
|
+
const names = directories.get(target);
|
|
94
|
+
if (!names)
|
|
95
|
+
return [target];
|
|
96
|
+
directories.delete(target);
|
|
97
|
+
return [...names].flatMap((name) => remove(path.join(target, name)));
|
|
98
|
+
};
|
|
99
|
+
const forget = (target) => directories.get(path.dirname(target))?.delete(path.basename(target)) ? remove(target) : [];
|
|
100
|
+
let closed = false;
|
|
101
|
+
let failed = false;
|
|
102
|
+
let synced = Date.now();
|
|
103
|
+
const removed = new Set();
|
|
104
|
+
const snapshot = () => {
|
|
105
|
+
const files = new Set([...removed].map(toWatched));
|
|
106
|
+
for (const [directory, names] of directories)
|
|
107
|
+
for (const name of names) {
|
|
108
|
+
const child = path.join(directory, name);
|
|
109
|
+
if (!directories.has(child))
|
|
110
|
+
files.add(toWatched(child));
|
|
111
|
+
}
|
|
112
|
+
return { files, since: synced - MTIME_MARGIN_MS };
|
|
113
|
+
};
|
|
114
|
+
const unlink = (files) => {
|
|
115
|
+
if (!files.length)
|
|
116
|
+
return;
|
|
117
|
+
for (const file of files)
|
|
118
|
+
removed.add(file);
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
if (closed || failed)
|
|
121
|
+
return;
|
|
122
|
+
for (const file of files)
|
|
123
|
+
if (removed.delete(file))
|
|
124
|
+
emit('unlink', file);
|
|
125
|
+
}, ATOMIC_WINDOW_MS);
|
|
126
|
+
};
|
|
127
|
+
const handle = async ({ type, path: target }, fresh) => {
|
|
128
|
+
const siblings = directories.get(path.dirname(target));
|
|
129
|
+
if (!siblings || (type !== 'delete' && fresh.has(target)))
|
|
130
|
+
return;
|
|
131
|
+
const info = type === 'delete' ? undefined : await lstat(target).catch(() => undefined);
|
|
132
|
+
if (info?.isSymbolicLink())
|
|
133
|
+
await rejectSymlink(target);
|
|
134
|
+
const isDirectory = info?.isDirectory() ?? false;
|
|
135
|
+
if (!info || (!isDirectory && !info.isFile()) || ignored(target, isDirectory)) {
|
|
136
|
+
if (isDirectory)
|
|
137
|
+
ignoredDirectories.add(target);
|
|
138
|
+
unlink(forget(target));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (isDirectory && type === 'update' && directories.has(target))
|
|
142
|
+
return;
|
|
143
|
+
const previous = new Set(forget(target));
|
|
144
|
+
siblings.add(path.basename(target));
|
|
145
|
+
const found = isDirectory ? [] : [target];
|
|
146
|
+
if (isDirectory)
|
|
147
|
+
await scan(target, found).catch((error) => {
|
|
148
|
+
forget(target);
|
|
149
|
+
for (const file of previous)
|
|
150
|
+
removed.add(file);
|
|
151
|
+
throw error;
|
|
152
|
+
});
|
|
153
|
+
const current = new Set(found);
|
|
154
|
+
unlink([...previous].filter((file) => !current.has(file)));
|
|
155
|
+
for (const file of current) {
|
|
156
|
+
fresh.add(file);
|
|
157
|
+
emit(previous.has(file) || removed.delete(file) ? 'change' : 'add', file);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
let subscribed = 0;
|
|
161
|
+
let subscription;
|
|
162
|
+
let release = () => { };
|
|
163
|
+
let queue = new Promise((resolve) => {
|
|
164
|
+
release = resolve;
|
|
165
|
+
});
|
|
166
|
+
const start = () => {
|
|
167
|
+
subscribed = ignoredDirectories.size;
|
|
168
|
+
return subscribe(realRoot, (error, events) => {
|
|
169
|
+
const received = Date.now();
|
|
170
|
+
queue = queue
|
|
171
|
+
.then(async () => {
|
|
172
|
+
if (closed || failed)
|
|
173
|
+
return;
|
|
174
|
+
if (error)
|
|
175
|
+
throw error;
|
|
176
|
+
const fresh = new Set();
|
|
177
|
+
for (const event of events)
|
|
178
|
+
await handle(event, fresh);
|
|
179
|
+
synced = received;
|
|
180
|
+
if (ignoredDirectories.size === subscribed)
|
|
181
|
+
return;
|
|
182
|
+
const previous = subscription;
|
|
183
|
+
subscription = await start();
|
|
184
|
+
await previous?.unsubscribe();
|
|
185
|
+
})
|
|
186
|
+
.catch((failure) => {
|
|
187
|
+
if (closed || failed)
|
|
188
|
+
return;
|
|
189
|
+
failed = true;
|
|
190
|
+
onFailure(failure, snapshot());
|
|
191
|
+
});
|
|
192
|
+
}, {
|
|
193
|
+
backend: BACKENDS[process.platform],
|
|
194
|
+
ignore: [...ignoredDirectories],
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
const close = async () => {
|
|
198
|
+
if (closed)
|
|
199
|
+
return;
|
|
200
|
+
closed = true;
|
|
201
|
+
release();
|
|
202
|
+
await queue;
|
|
203
|
+
await subscription?.unsubscribe();
|
|
204
|
+
};
|
|
205
|
+
try {
|
|
206
|
+
if (process.platform === 'linux') {
|
|
207
|
+
const before = [];
|
|
208
|
+
await scan(realRoot, before);
|
|
209
|
+
subscription = await start();
|
|
210
|
+
directories.clear();
|
|
211
|
+
const after = [];
|
|
212
|
+
await scan(realRoot, after);
|
|
213
|
+
const known = new Set(before);
|
|
214
|
+
const current = new Set(after);
|
|
215
|
+
for (const file of known)
|
|
216
|
+
if (!current.has(file))
|
|
217
|
+
emit('unlink', file);
|
|
218
|
+
for (const file of current)
|
|
219
|
+
if (!known.has(file))
|
|
220
|
+
emit('add', file);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
subscription = await start();
|
|
224
|
+
await scan(realRoot);
|
|
225
|
+
subscribed = ignoredDirectories.size;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
await close();
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
release();
|
|
233
|
+
return { close };
|
|
234
|
+
};
|
|
235
|
+
export const watchTree = async (root, options) => {
|
|
236
|
+
let closed = false;
|
|
237
|
+
let active;
|
|
238
|
+
const useChokidar = async (snapshot) => {
|
|
239
|
+
await active?.close();
|
|
240
|
+
active = await watchWithChokidar(root, options, snapshot);
|
|
241
|
+
if (closed)
|
|
242
|
+
await active.close();
|
|
243
|
+
};
|
|
244
|
+
const reportUnavailable = (error) => {
|
|
245
|
+
if (!(error instanceof SymlinkError))
|
|
246
|
+
options.onNativeUnavailable?.(error);
|
|
247
|
+
};
|
|
248
|
+
if (isNativeEnabled()) {
|
|
249
|
+
try {
|
|
250
|
+
active = await watchNatively(root, options, (error, snapshot) => {
|
|
251
|
+
reportUnavailable(error);
|
|
252
|
+
void useChokidar(snapshot).catch(options.onError);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
reportUnavailable(error);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (!active)
|
|
260
|
+
await useChokidar();
|
|
261
|
+
return {
|
|
262
|
+
close: async () => {
|
|
263
|
+
closed = true;
|
|
264
|
+
await active?.close();
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mintlify/previewing",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1666",
|
|
4
4
|
"description": "Preview Mintlify docs locally",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.0.0"
|
|
@@ -44,10 +44,9 @@
|
|
|
44
44
|
"type": "tsrs --noEmit"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@mintlify/common": "1.0.
|
|
48
|
-
"@mintlify/prebuild": "1.0.
|
|
47
|
+
"@mintlify/common": "1.0.1395",
|
|
48
|
+
"@mintlify/prebuild": "1.0.1575",
|
|
49
49
|
"@mintlify/validation": "0.1.987",
|
|
50
|
-
"adm-zip": "0.6.0",
|
|
51
50
|
"better-opn": "3.0.2",
|
|
52
51
|
"chalk": "5.2.0",
|
|
53
52
|
"chokidar": "3.5.3",
|
|
@@ -62,10 +61,12 @@
|
|
|
62
61
|
"react": "19.2.3",
|
|
63
62
|
"socket.io": "4.8.0",
|
|
64
63
|
"tar": "7.5.21",
|
|
65
|
-
"yargs": "17.7.1"
|
|
64
|
+
"yargs": "17.7.1",
|
|
65
|
+
"yazl": "3.3.1"
|
|
66
66
|
},
|
|
67
67
|
"optionalDependencies": {
|
|
68
|
-
"@mintlify/package-builder": "0.0.
|
|
68
|
+
"@mintlify/package-builder": "0.0.184",
|
|
69
|
+
"@parcel/watcher": "2.5.1"
|
|
69
70
|
},
|
|
70
71
|
"devDependencies": {
|
|
71
72
|
"@mintlify/models": "0.0.402",
|
|
@@ -80,6 +81,8 @@
|
|
|
80
81
|
"@types/node": "18.19.21",
|
|
81
82
|
"@types/tar": "6.1.5",
|
|
82
83
|
"@types/yargs": "17.0.22",
|
|
84
|
+
"@types/yazl": "3.3.1",
|
|
85
|
+
"adm-zip": "0.6.0",
|
|
83
86
|
"oxfmt": "^0.51.0",
|
|
84
87
|
"oxlint": "1.93.0",
|
|
85
88
|
"rimraf": "5.0.1",
|
|
@@ -87,5 +90,5 @@
|
|
|
87
90
|
"vitest": "2.1.9",
|
|
88
91
|
"vitest-mock-process": "1.0.4"
|
|
89
92
|
},
|
|
90
|
-
"gitHead": "
|
|
93
|
+
"gitHead": "96db0c7a5a85396042bca6c9ad66cb3f4b3add21"
|
|
91
94
|
}
|