blob: 0ccdad6ea6c14fd3b73a29e1004f0650ba7c4d19 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
import type { ApiDiffHashes } from './output/diff-index';
import {
FILE_NAME_DOCS_DIFF_INDEX,
FILE_PATH_DOCS_DIFF_INDEX,
} from './output/diff-index';
/**
* Loads the diff index from the given source url.
*
* @param url The url to load the diff index from.
*/
async function loadRemote(url: string): Promise<ApiDiffHashes> {
return fetch(url).then((res) => {
if (!res.ok) {
throw new Error(
`Failed to load remote diff index from ${url}: ${res.statusText}`
);
}
return res.json() as Promise<ApiDiffHashes>;
});
}
/**
* Loads the diff index from the given local path.
*
* @param path The path to load the diff index from. Should start with `file://` for cross platform compatibility.
*/
async function loadLocal(path: string): Promise<ApiDiffHashes> {
return import(path).then((imp) => imp.default as ApiDiffHashes);
}
/**
* Loads the diff index from the given source.
* If the source starts with `https://` it will be loaded from the remote url.
* Otherwise it will be loaded from the local path.
*
* @param source The source to load the diff index from.
*/
async function load(source: string): Promise<ApiDiffHashes> {
return source.startsWith('https://') ? loadRemote(source) : loadLocal(source);
}
/**
* Returns a set of all keys from the given entries.
*
* @param entries The entries to get the keys from.
*/
function allKeys(
...entries: ReadonlyArray<Record<string, unknown>>
): Set<string> {
return new Set(entries.flatMap(Object.keys));
}
/**
* Compares the target (reference) and source (changed) diff index and returns the differences.
* The returned object contains the module names as keys and the method names as values.
* If the module name is `ADDED` or `REMOVED` it means that the module was added or removed in the local diff index.
*
* @param targetDiffIndex The url to the target (reference) diff index. Defaults to the next.fakerjs.dev diff index.
* @param sourceDiffIndex The path to the source (changed) index. Defaults to the local diff index.
*/
export async function diff(
targetDiffIndex = `https://next.fakerjs.dev/${FILE_NAME_DOCS_DIFF_INDEX}`,
sourceDiffIndex = `file://${FILE_PATH_DOCS_DIFF_INDEX}`
): Promise<Record<string, ['ADDED'] | ['REMOVED'] | string[]>> {
const target = await load(targetDiffIndex);
const source = await load(sourceDiffIndex);
const diff: Record<string, string[]> = {};
for (const moduleName of allKeys(target, source)) {
const remoteModule = target[moduleName];
const localModule = source[moduleName];
if (!remoteModule) {
diff[moduleName] = ['ADDED'];
continue;
}
if (!localModule) {
diff[moduleName] = ['REMOVED'];
continue;
}
for (const methodName of allKeys(remoteModule, localModule)) {
const remoteMethod = remoteModule[methodName];
const localMethod = localModule[methodName];
if (remoteMethod !== localMethod) {
(diff[moduleName] ??= []).push(methodName);
}
}
}
return diff;
}
|