Skip to content

Commit 35da72f

Browse files
authored
Merge pull request #56 from open-rpc/fix/race
Fix/race
2 parents 90f51ce + 220d782 commit 35da72f

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

.changeset/beige-hounds-jump.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@open-rpc/markdown-generator": patch
3+
"@open-rpc/docusaurus-plugin": patch
4+
---
5+
6+
Fixes a race condition when running in active development mode. This set of changes now allows for incremental deletion and proper removal ordering

packages/docusaurus-plugin/src/index.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,22 @@ import type { LoadContext, Plugin } from "@docusaurus/types";
55
import type { PluginContent } from "./types";
66
import type { Options, PluginOptions } from "./options";
77
import { normalizeOptions } from "./options";
8-
import { generateDocs } from "./lib";
8+
import { cleanUpExistingDocs, generateDocs } from "./lib";
99
import fs from "fs/promises";
1010
import path from "path";
11+
import { parseOpenRPCDocument } from "@open-rpc/schema-utils-js";
12+
import { DereffedOpenrpcDocument } from "@open-rpc/markdown-generator";
1113

1214
const PluginName = "@open-rpc/docusaurus-plugin";
1315

14-
async function initDocs(
16+
async function rebuildDocs(
1517
specPath: string,
1618
outputDir: string,
1719
options: PluginOptions,
1820
) {
19-
if (!(await fs.stat(specPath)).isFile()) {
20-
throw new Error(`OpenRPC spec file not found: ${specPath}`);
21-
}
22-
23-
try {
24-
const entries = await fs.readdir(outputDir, { withFileTypes: true });
25-
await Promise.all(
26-
entries
27-
.filter((entry) => entry.name !== "index.md")
28-
.map((entry) => {
29-
const fullPath = `${outputDir}/${entry.name}`;
30-
return fs.rm(fullPath, { recursive: true, force: true });
31-
}),
32-
);
33-
} catch {
34-
// Directory doesn't exist yet, that's fine
35-
}
36-
3721
try {
3822
await generateDocs(specPath, outputDir, options);
23+
await cleanUpExistingDocs(specPath, outputDir);
3924
} catch (err) {
4025
logger.error(`[${PluginName}] generateDocs failed: ${err}`);
4126
logger.error(
@@ -61,7 +46,7 @@ export default async function openRPCDocusaurusPlugin(
6146
normalizedOptions.docOutputPath,
6247
);
6348

64-
await initDocs(specPath, outputDir, normalizedOptions);
49+
await rebuildDocs(specPath, outputDir, normalizedOptions);
6550

6651
return {
6752
name: PluginName,
@@ -95,7 +80,7 @@ export default async function openRPCDocusaurusPlugin(
9580
*/
9681
async contentLoaded({ content, actions }): Promise<void> {
9782
logger.info(`[${PluginName}] contentLoaded called`);
98-
await initDocs(specPath, outputDir, normalizedOptions);
83+
await rebuildDocs(specPath, outputDir, normalizedOptions);
9984
},
10085

10186
/**

packages/docusaurus-plugin/src/lib.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@ methodEdits.editMethod = (content, method) => {
5858
] as OpenRPCMdContent[];
5959
};
6060

61+
export async function cleanUpExistingDocs(specPath: string, outputDir: string) {
62+
if (!(await fs.stat(specPath)).isFile()) {
63+
throw new Error(`OpenRPC spec file not found: ${specPath}`);
64+
}
65+
const raw = await fs.readFile(specPath, "utf8");
66+
const doc: DereffedOpenrpcDocument = (await parseOpenRPCDocument(
67+
raw,
68+
)) as DereffedOpenrpcDocument;
69+
70+
const methodFileNamesToGenerate = new Set(
71+
doc.methods.map((method) => `${method.name}.mdx`),
72+
);
73+
const entries = await fs.readdir(outputDir, {
74+
withFileTypes: true,
75+
recursive: true,
76+
});
77+
78+
const entriesToDelete = entries
79+
.filter(
80+
(entry) =>
81+
entry.isFile() &&
82+
methodFileNamesToGenerate.has(entry.name) === false &&
83+
entry.name !== "index.md",
84+
)
85+
.map((entry) => `${entry.parentPath}/${entry.name}`);
86+
87+
await Promise.all(
88+
entriesToDelete.map((entry) =>
89+
fs.rm(entry, { recursive: true, force: true }),
90+
),
91+
);
92+
}
93+
6194
export async function generateDocs(
6295
inputPath: string,
6396
outputPath: string,

packages/example-site/docs/api-reference/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# GENERATED DOCUMENTATION - DO NOT EDIT THIS FILE
33
title: "Ethereum JSON-RPC Specification"
44
description: "A specification of the standard interface for Ethereum clients."
5-
slug: /api-reference
5+
66

77
---
88

packages/example-site/docusaurus.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const config: Config = {
3939
openRPCSpecPath: `./error-group-openrpc.json`,
4040
docOutputPath: `./docs/api-reference`,
4141
showPoweredBy: true,
42-
indexSlug: '/api-reference',
4342
}]],
4443
presets: [
4544
[

0 commit comments

Comments
 (0)