-
-
Notifications
You must be signed in to change notification settings - Fork 975
infra(docs): add docs diff script #1755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ef2996e
docs: generate module hashes for api docs diff
ST-DDT 7cec095
chore: apply suggestions
ST-DDT 160bc81
Merge branch 'next' into docs/diff-notification
ST-DDT 9a16588
chore: apply suggestions + add shortcut command
ST-DDT ea0685e
chore: improve console output
ST-DDT 55294a6
chore: use correct test url
ST-DDT 2b87847
Merge branch 'next' into docs/diff-notification
ST-DDT cca71c8
chore: merge next
ST-DDT b60bc57
chore: cleanup
ST-DDT 57ff4e6
chore: cleanup
ST-DDT 89bdd53
chore: reduce diff
ST-DDT 9d66dca
Merge branch 'next' into docs/diff-notification
ST-DDT 18489f6
Merge branch 'next' into docs/diff-notification
ST-DDT 9755841
Merge branch 'next' into docs/diff-notification
ST-DDT 258528a
chore: remove debug code
ST-DDT 003a443
chore: cleanup
ST-DDT b777cae
Merge branch 'next' into docs/diff-notification
ST-DDT cdd7850
chore: apply review suggestions
ST-DDT 19e670b
chore: merge next
ST-DDT cacd5e7
chore: apply suggestions
ST-DDT af9600b
Merge branch 'next' into docs/diff-notification
ST-DDT a6bd43b
Merge branch 'next' into docs/diff-notification
ST-DDT 55f77b8
Merge branch 'next' into docs/diff-notification
ST-DDT f52a6dd
Merge branch 'next' into docs/diff-notification
ST-DDT 95e676e
chore: fix lint errors
ST-DDT 3d3ca57
Merge branch 'docs/diff-notification' of github.com:faker-js/faker in…
ST-DDT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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}` | ||
); | ||
} else { | ||
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) { | ||
if (source.startsWith('https://')) { | ||
return loadRemote(source); | ||
} else { | ||
return loadLocal(source); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a set of all keys from the given entries. | ||
* | ||
* @param entries The entries to get the keys from. | ||
*/ | ||
function allKeys(...entries: Record<string, unknown>[]): Set<string> { | ||
return new Set(entries.map(Object.keys).flat()); | ||
} | ||
|
||
/** | ||
* Compares the remote and local 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 remoteDiffIndex The url to the remote diff index. Defaults to the next.fakerjs.dev diff index. | ||
* @param localDiffIndex The path to the local diff index. Defaults to the local diff index. | ||
*/ | ||
export async function diff( | ||
remoteDiffIndex = `https://next.fakerjs.dev/${nameDocsDiffIndexFile}`, | ||
localDiffIndex = `file://${pathDocsDiffIndexFile}` | ||
): Promise<Record<string, string[]>> { | ||
const remote = await load(remoteDiffIndex); | ||
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const local = await load(localDiffIndex); | ||
xDivisionByZerox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const diff: Record<string, string[]> = {}; | ||
|
||
for (const moduleName of allKeys(remote, local)) { | ||
const remoteModule = remote[moduleName]; | ||
const localModule = local[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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { existsSync } from 'fs'; | ||
import { diff } from './apidoc/diff'; | ||
import { nameDocsDiffIndexFile, pathDocsDiffIndexFile } from './apidoc/utils'; | ||
|
||
if (!existsSync(pathDocsDiffIndexFile)) { | ||
throw new Error( | ||
`Unable to find local diff index file at: ${pathDocsDiffIndexFile}\n | ||
You can run \`pnpm run generate:api-docs\` to generate it.` | ||
); | ||
} | ||
|
||
// TODO @ST-DDT 2023-01-20: Remove this url when the diff index is available on next.fakerjs.dev | ||
diff( | ||
`https://docs-diff-notification--serene-sprite-f3ef50.netlify.app/${nameDocsDiffIndexFile}` | ||
) | ||
ST-DDT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.then((delta) => { | ||
if (Object.keys(delta).length === 0) { | ||
console.log('No documentation changes detected'); | ||
return; | ||
} | ||
|
||
console.log('Documentation changes detected:'); | ||
for (const [module, methods] of Object.entries(delta)) { | ||
console.log(`- ${module}`); | ||
for (const method of methods) { | ||
console.log(` - ${method}`); | ||
} | ||
} | ||
}) | ||
.catch(console.error); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.