-
-
Notifications
You must be signed in to change notification settings - Fork 120
Cache Twoslash results across documentation builds #949
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
+589
−358
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| name: Cache documentation build | ||
| description: Restore the VitePress and Twoslash build caches | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - id: cache-key | ||
| shell: bash | ||
| run: | | ||
| namespace=$(node --input-type=module --eval \ | ||
| "import { getFedifyTwoslashCacheNamespace } from './docs/.vitepress/twoslash-cache.mts'; console.log(getFedifyTwoslashCacheNamespace());") | ||
| echo "namespace=$namespace" >> "$GITHUB_OUTPUT" | ||
| echo "docs-tree=$(git rev-parse HEAD:docs)" >> "$GITHUB_OUTPUT" | ||
| - uses: actions/cache@v6 | ||
| with: | ||
| path: docs/.vitepress/cache | ||
| key: ${{ runner.os }}-${{ runner.arch }}-vitepress-${{ steps.cache-key.outputs.namespace }}-${{ steps.cache-key.outputs.docs-tree }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-${{ runner.arch }}-vitepress-${{ steps.cache-key.outputs.namespace }}- | ||
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,171 @@ | ||
| import { createFileSystemTypesCache } from "@shikijs/vitepress-twoslash/cache-fs"; | ||
| import { createHash } from "node:crypto"; | ||
| import { | ||
| existsSync, | ||
| type Dirent, | ||
| readdirSync, | ||
| readFileSync, | ||
| rmSync, | ||
| } from "node:fs"; | ||
| import { join, relative, resolve } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| const cacheFormatVersion = 1; | ||
| const docsDir = fileURLToPath(new URL("../", import.meta.url)); | ||
| const repositoryDir = fileURLToPath(new URL("../../", import.meta.url)); | ||
|
|
||
| function isDeclarationFile(path: string): boolean { | ||
| return path.endsWith(".d.ts") || | ||
| path.endsWith(".d.mts") || | ||
| path.endsWith(".d.cts"); | ||
| } | ||
|
|
||
| function collectDeclarationFiles(directory: string): string[] { | ||
| if (!existsSync(directory)) return []; | ||
|
|
||
| const files: string[] = []; | ||
| const entries = readdirSync(directory, { withFileTypes: true }); | ||
| for (const entry of entries) { | ||
| if (entry.name === "node_modules") continue; | ||
|
|
||
| const path = join(directory, entry.name); | ||
| if (entry.isDirectory()) { | ||
| files.push(...collectDeclarationFiles(path)); | ||
| } else if (entry.isFile() && isDeclarationFile(path)) { | ||
| files.push(path); | ||
| } | ||
| } | ||
| return files; | ||
| } | ||
|
|
||
| function getPackageDirectories(): string[] { | ||
| const packagesDir = join(repositoryDir, "packages"); | ||
| return readdirSync(packagesDir, { withFileTypes: true }) | ||
| .filter((entry: Dirent) => entry.isDirectory()) | ||
| .map((entry: Dirent) => join(packagesDir, entry.name)); | ||
| } | ||
|
|
||
| function getTypeEnvironmentFiles(): string[] { | ||
| const files = [ | ||
| fileURLToPath(import.meta.url), | ||
| join(repositoryDir, "deno.json"), | ||
| join(repositoryDir, "package.json"), | ||
| join(repositoryDir, "pnpm-lock.yaml"), | ||
| join(repositoryDir, "pnpm-workspace.yaml"), | ||
| join(docsDir, "package.json"), | ||
| join(docsDir, ".vitepress", "config.mts"), | ||
| ]; | ||
|
dahlia marked this conversation as resolved.
|
||
|
|
||
| for (const packageDir of getPackageDirectories()) { | ||
| const packageJson = join(packageDir, "package.json"); | ||
| if (existsSync(packageJson)) files.push(packageJson); | ||
| files.push(...collectDeclarationFiles(join(packageDir, "dist"))); | ||
| } | ||
|
|
||
| return files.sort(); | ||
| } | ||
|
|
||
| export function getFedifyTwoslashCacheNamespace(): string { | ||
| const hash = createHash("sha256"); | ||
| hash.update(`fedify-twoslash-cache:${cacheFormatVersion}\0`); | ||
| hash.update(`${process.platform}:${process.arch}:${process.versions.node}\0`); | ||
|
|
||
| for (const path of getTypeEnvironmentFiles()) { | ||
| hash.update(relative(repositoryDir, path)); | ||
| hash.update("\0"); | ||
| hash.update(readFileSync(path)); | ||
| hash.update("\0"); | ||
| } | ||
|
|
||
| return hash.digest("hex"); | ||
| } | ||
|
|
||
| function normalizeForCacheKey( | ||
| value: unknown, | ||
| seen: WeakSet<object>, | ||
| ): unknown { | ||
| if (typeof value === "bigint") return `${value}n`; | ||
| if (typeof value === "function") return value.toString(); | ||
| if (typeof value === "symbol") return value.toString(); | ||
| if (value == null || typeof value !== "object") return value; | ||
| if (seen.has(value)) return "[Circular]"; | ||
| seen.add(value); | ||
|
|
||
| if (Array.isArray(value)) { | ||
| return value.map((item) => normalizeForCacheKey(item, seen)); | ||
| } | ||
| if (value instanceof Map) { | ||
| return Array.from(value.entries()) | ||
| .map(([key, item]) => [ | ||
| normalizeForCacheKey(key, seen), | ||
| normalizeForCacheKey(item, seen), | ||
| ]) | ||
| .sort(([left], [right]) => | ||
| JSON.stringify(left).localeCompare(JSON.stringify(right)) | ||
| ); | ||
| } | ||
| if (value instanceof Set) { | ||
| return Array.from(value.values()) | ||
| .map((item) => normalizeForCacheKey(item, seen)) | ||
| .sort((left, right) => | ||
| JSON.stringify(left).localeCompare(JSON.stringify(right)) | ||
| ); | ||
| } | ||
|
|
||
| return Object.fromEntries( | ||
| Object.entries(value) | ||
| .sort(([left], [right]) => left.localeCompare(right)) | ||
| .map(([key, item]) => [key, normalizeForCacheKey(item, seen)]), | ||
| ); | ||
| } | ||
|
dahlia marked this conversation as resolved.
|
||
|
|
||
| function getEntryCacheKey( | ||
| code: string, | ||
| lang: string | undefined, | ||
| options: unknown, | ||
| ): string { | ||
| return JSON.stringify( | ||
| normalizeForCacheKey( | ||
| { cacheFormatVersion, code, lang, options }, | ||
| new WeakSet(), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| export function createFedifyTwoslashCache(): ReturnType< | ||
| typeof createFileSystemTypesCache | ||
| > { | ||
| const namespace = getFedifyTwoslashCacheNamespace(); | ||
| const cacheRoot = resolve(docsDir, ".vitepress", "cache", "twoslash"); | ||
| const cacheDir = join(cacheRoot, namespace); | ||
|
|
||
| // Restored CI caches can contain obsolete type environments. Keeping only | ||
| // the current one prevents the cache artifact from growing without bound. | ||
| if (existsSync(cacheRoot)) { | ||
| for (const entry of readdirSync(cacheRoot, { withFileTypes: true })) { | ||
| if (entry.name !== namespace) { | ||
| rmSync(join(cacheRoot, entry.name), { recursive: true, force: true }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const cache = createFileSystemTypesCache({ | ||
| dir: cacheDir, | ||
| }); | ||
|
|
||
| return { | ||
| init: cache.init, | ||
| read(code, lang, options, meta) { | ||
| return cache.read(getEntryCacheKey(code, lang, options), lang, options, meta); | ||
| }, | ||
| write(code, data, lang, options, meta) { | ||
| cache.write( | ||
| getEntryCacheKey(code, lang, options), | ||
| data, | ||
| lang, | ||
| options, | ||
| meta, | ||
| ); | ||
| }, | ||
| }; | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.