diff options
| author | ST-DDT <[email protected]> | 2024-04-01 10:21:18 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-04-01 10:21:18 +0200 |
| commit | 6191a5d883048b694404dbf42527caba395828ea (patch) | |
| tree | d0f18f17789cb0bbdb5d6087f1a95772438dfe27 /scripts/apidoc/diff.ts | |
| parent | 7dae52bfcd93c41ec9d2c4dd4d96a07f31c3dfc1 (diff) | |
| download | faker-6191a5d883048b694404dbf42527caba395828ea.tar.xz faker-6191a5d883048b694404dbf42527caba395828ea.zip | |
docs: rewrite api-docs generation using ts-morph (#2628)
Diffstat (limited to 'scripts/apidoc/diff.ts')
| -rw-r--r-- | scripts/apidoc/diff.ts | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/scripts/apidoc/diff.ts b/scripts/apidoc/diff.ts deleted file mode 100644 index 101200cd..00000000 --- a/scripts/apidoc/diff.ts +++ /dev/null @@ -1,94 +0,0 @@ -import type { DocsApiDiffIndex } from './utils'; -import { nameDocsDiffIndexFile, pathDocsDiffIndexFile } from './utils'; - -/** - * 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<DocsApiDiffIndex> { - 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<DocsApiDiffIndex>; - }); -} - -/** - * 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<DocsApiDiffIndex> { - return import(path).then((imp) => imp.default as DocsApiDiffIndex); -} - -/** - * 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<DocsApiDiffIndex> { - 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/${nameDocsDiffIndexFile}`, - sourceDiffIndex = `file://${pathDocsDiffIndexFile}` -): 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; -} |
