Skip to content

Commit 75eca87

Browse files
committed
fix: Changes for docs-gen v6 ESM compatibility
1 parent d2d3bec commit 75eca87

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

packages/documentation-framework/scripts/cli/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async function execFile(file, args) {
7676
}
7777

7878
async function build(cmd, options) {
79-
generate(options);
79+
await generate(options);
8080
const toBuild = cmd === 'all'
8181
? ['server', 'client']
8282
: cmd;

packages/documentation-framework/scripts/cli/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ program
99
program
1010
.command('generate')
1111
.description('generates source files')
12-
.action(options => {
12+
.action(async options => {
1313
const { generate } = require('./generate');
14-
generate(options);
14+
await generate(options);
1515
});
1616

1717
program

packages/documentation-framework/scripts/cli/generate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
const path = require('path');
2-
const { sourceMD, sourceProps, sourceFunctionDocs, writeIndex } = require('../md/parseMD');
2+
const { sourceMD, sourceProps, sourceFunctionDocs, writeIndex, waitForProps } = require('../md/parseMD');
33

44
function getSource(options) {
55
return require(path.join(process.cwd(), options.parent.source));
66
}
77

8-
function generate(options) {
8+
async function generate(options) {
99
const start = new Date();
1010
console.log('write source files to patternfly-docs/generated');
1111
const sourceMDWithOptions = (glob, source, ignore) => sourceMD(glob, source, ignore, options._name);
1212
getSource(options)(sourceMDWithOptions, sourceProps, sourceFunctionDocs);
13+
await waitForProps();
1314
const exitCode = writeIndex();
1415
if (exitCode !== 0) {
1516
process.exit(exitCode);

packages/documentation-framework/scripts/cli/start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function startDevServer(webpackConfig) {
1919
}
2020

2121
async function start(options) {
22-
generate(options, true);
22+
await generate(options, true);
2323
const webpackClientConfig = await clientConfig(null, { mode: 'development', ...getConfig(options) });
2424
console.log('start rspack-dev-server');
2525
watchMD();

packages/documentation-framework/scripts/md/parseMD.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const outputBase = path.join(process.cwd(), `patternfly-docs/generated`);
1919
const tsDocs = {};
2020
let functionDocs = {};
2121
const routes = {};
22+
const pendingProps = [];
2223
const globs = {
2324
props: [],
2425
md: [],
@@ -267,8 +268,8 @@ function toReactComponent(mdFilePath, source, buildMode) {
267268
};
268269
}
269270

270-
function sourcePropsFile(file) {
271-
tsDocgen(file)
271+
async function sourcePropsFile(file) {
272+
(await tsDocgen(file))
272273
.filter(({ hide }) => !hide)
273274
.forEach(({ name, description, props }) => {
274275
tsDocs[getTsDocName(name, getTsDocNameVariant(file))] = { name, description, props };
@@ -345,7 +346,11 @@ function getTsDocNameVariant(source) {
345346
module.exports = {
346347
sourceProps(glob, ignore) {
347348
globs.props.push({ glob, ignore });
348-
globSync(glob, { ignore }).forEach(sourcePropsFile);
349+
const promise = Promise.all(globSync(glob, { ignore }).map(sourcePropsFile));
350+
pendingProps.push(promise);
351+
},
352+
async waitForProps() {
353+
await Promise.all(pendingProps);
349354
},
350355
sourceMD(glob, source, ignore, buildMode) {
351356
globs.md.push({ glob, source, ignore });

packages/documentation-framework/scripts/tsDocgen.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
const fs = require("fs");
2-
const reactDocgen = require("react-docgen");
32
const ts = require("typescript");
43

4+
// react-docgen v6+ is ESM-only; lazy-load it asynchronously on first use
5+
let reactDocgenReady = null;
6+
function loadReactDocgen() {
7+
if (!reactDocgenReady) {
8+
reactDocgenReady = import("react-docgen");
9+
}
10+
return reactDocgenReady;
11+
}
12+
513
const annotations = [
614
{
715
regex: /@deprecated/,
@@ -41,7 +49,8 @@ function addAnnotations(prop) {
4149
return prop;
4250
}
4351

44-
function getComponentMetadata(filename, sourceText) {
52+
async function getComponentMetadata(filename, sourceText) {
53+
const reactDocgen = await loadReactDocgen();
4554
let parsedComponents = null;
4655
try {
4756
parsedComponents = reactDocgen.parse(sourceText, {
@@ -159,9 +168,9 @@ function normalizeProp([
159168
return res;
160169
}
161170

162-
function tsDocgen(file) {
171+
async function tsDocgen(file) {
163172
const sourceText = fs.readFileSync(file, "utf8");
164-
const componentMeta = getComponentMetadata(file, sourceText); // Array of components with props
173+
const componentMeta = await getComponentMetadata(file, sourceText); // Array of components with props
165174
const interfaceMeta = getInterfaceMetadata(file, sourceText); // Array of interfaces with props
166175
const typeAliasMeta = getTypeAliasMetadata(file, sourceText); // Array of type aliases with props
167176
const propsMetaMap = [...interfaceMeta, ...typeAliasMeta].reduce(function (

0 commit comments

Comments
 (0)