-
Notifications
You must be signed in to change notification settings - Fork 17
feat: spec preview support + local spec reading #1043
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
358ee2a
feat: add spec preview support and local spec reading
dslovinsky f1b7a45
fix: fail fast on missing specs dir, fix generate race condition
dslovinsky c532d64
fix: remove metadata.json dependency, add generate to CI workflow
dslovinsky 03ba8f6
fix: split CI generate steps, use spawn for parallel generation
dslovinsky c4709d7
feat: standalone upload-specs command with change detection
dslovinsky ade3a90
feat: merge spec sync into index workflow, simplify gh-pages trigger
dslovinsky 6a232e2
refactor: write upload-specs output to file, scope to spec dirs
dslovinsky 2b8e605
chore: remove unused detect-spec-changes.sh
dslovinsky eb58500
refactor: remove remote spec fetching, rename to readApiSpec
dslovinsky 7c4d2d5
Merge branch 'main' of github.com:alchemyplatform/docs into ds/spec-p…
dslovinsky b031c9e
refactor: remove dead isMainBranch check from preview-specs
dslovinsky 19fa589
refactor: replace recursive file search with buildSpecFileMap
dslovinsky 816fc37
refactor: reduce spec upload log noise, add --kill to watchers
dslovinsky 0fdc76e
refactor: quiet logging in preview mode
dslovinsky 9abb8c3
fix: propagate generator exit codes in generate script
dslovinsky d9232ae
fix: add remote-specs.json to workflow path triggers
dslovinsky 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
Some comments aren't visible on the classic Files Changed page.
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 was deleted.
Oops, something went wrong.
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,129 @@ | ||
| #!/usr/bin/env tsx | ||
| /** | ||
| * Standalone spec upload CLI with change detection. | ||
| * | ||
| * Scans fern/api-specs/{alchemy,chains}/, computes SHA-256 hashes, compares | ||
| * against Redis, uploads only changed specs, and writes changed URLs as JSON | ||
| * to an output file (default: fern/api-specs/changed-specs.json). | ||
| * | ||
| * Usage: pnpm upload-specs [--output path] | ||
| * Env: KV_REST_API_URL, KV_REST_API_TOKEN | ||
| */ | ||
| import crypto from "crypto"; | ||
| import { config as dotenvConfig } from "dotenv"; | ||
| import fs from "fs/promises"; | ||
| import path from "path"; | ||
|
|
||
| import { | ||
| DEV_DOCS_BASE, | ||
| buildSpecFileMap, | ||
| getSpecTypeFromUrl, | ||
| } from "@/content-indexer/utils/apiSpecs.ts"; | ||
| import { getRedis } from "@/content-indexer/utils/redis.ts"; | ||
|
|
||
| dotenvConfig({ path: path.resolve(process.cwd(), ".env"), quiet: true }); | ||
|
|
||
| const SPECS_DIR = path.resolve(process.cwd(), "fern/api-specs"); | ||
| const DEFAULT_OUTPUT = path.join(SPECS_DIR, "changed-specs.json"); | ||
| const HASH_KEY = "main:spec-hashes"; | ||
|
|
||
| type SpecHashMap = Record<string, string>; | ||
|
|
||
| const parseArgs = () => { | ||
| const args = process.argv.slice(2); | ||
| const outputFlag = args.find((arg) => arg.startsWith("--output=")); | ||
| const output = outputFlag | ||
| ? path.resolve(process.cwd(), outputFlag.split("=")[1]) | ||
| : DEFAULT_OUTPUT; | ||
| return { output }; | ||
| }; | ||
|
|
||
| const main = async () => { | ||
| const { output } = parseArgs(); | ||
|
|
||
| // Verify specs directory exists | ||
| try { | ||
| await fs.access(SPECS_DIR); | ||
| } catch { | ||
| console.error( | ||
| `Specs directory not found: ${SPECS_DIR}\nRun 'pnpm generate' first.`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const redis = getRedis(); | ||
|
|
||
| // 1. Build spec file map and read contents with hashes | ||
| const specFileMap = await buildSpecFileMap(SPECS_DIR); | ||
| console.info(`Found ${specFileMap.size} spec files`); | ||
|
|
||
| const specEntries = await Promise.all( | ||
| Array.from(specFileMap.values()).map(async (relativePath) => { | ||
| const specUrl = `${DEV_DOCS_BASE}/${relativePath}`; | ||
| const content = await fs.readFile( | ||
| path.join(SPECS_DIR, relativePath), | ||
| "utf-8", | ||
| ); | ||
| const hash = crypto.createHash("sha256").update(content).digest("hex"); | ||
| return { specUrl, content, hash }; | ||
| }), | ||
| ); | ||
|
|
||
| const newHashes: SpecHashMap = Object.fromEntries( | ||
| specEntries.map(({ specUrl, hash }) => [specUrl, hash]), | ||
| ); | ||
| const specContents: Record<string, string> = Object.fromEntries( | ||
| specEntries.map(({ specUrl, content }) => [specUrl, content]), | ||
| ); | ||
|
|
||
| // 2. Fetch existing hashes from Redis | ||
| const oldHashes = (await redis.get<SpecHashMap>(HASH_KEY)) ?? {}; | ||
|
|
||
| // 3. Find changed spec URLs (new or modified) | ||
| const changedUrls = Object.keys(newHashes).filter( | ||
| (url) => oldHashes[url] !== newHashes[url], | ||
| ); | ||
|
|
||
| // 4. Find deleted spec URLs (in old but not in new) | ||
| const deletedUrls = Object.keys(oldHashes).filter( | ||
| (url) => !(url in newHashes), | ||
| ); | ||
|
|
||
| const allAffectedUrls = [...changedUrls, ...deletedUrls]; | ||
|
|
||
| if (allAffectedUrls.length === 0) { | ||
| console.info("No spec changes detected"); | ||
| await fs.writeFile(output, JSON.stringify([])); | ||
| return; | ||
| } | ||
|
|
||
| const pipeline = redis.pipeline(); | ||
|
|
||
| // 5. Upload changed specs to Redis | ||
| changedUrls.forEach((specUrl) => { | ||
| const redisKey = `main:${getSpecTypeFromUrl(specUrl)}-spec:${specUrl}`; | ||
| pipeline.set(redisKey, specContents[specUrl]); | ||
| }); | ||
|
|
||
| // 6. Delete removed specs from Redis | ||
| deletedUrls.forEach((specUrl) => { | ||
| const redisKey = `main:${getSpecTypeFromUrl(specUrl)}-spec:${specUrl}`; | ||
| pipeline.del(redisKey); | ||
| }); | ||
|
|
||
| console.info(`${changedUrls.length} changed, ${deletedUrls.length} deleted`); | ||
|
|
||
| // 7. Update hash map | ||
| pipeline.set(HASH_KEY, JSON.stringify(newHashes)); | ||
|
|
||
| await pipeline.exec(); | ||
| console.info("Upload complete"); | ||
|
|
||
| // 8. Write all affected URLs to output file | ||
| await fs.writeFile(output, JSON.stringify(allAffectedUrls)); | ||
| }; | ||
|
|
||
| main().catch((error) => { | ||
| console.error("Fatal error:", error); | ||
| process.exit(1); | ||
| }); |
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.