Skip to content

Commit b08d4f9

Browse files
feat: add a multi-package mode so that classes are exported under a module instead of at the top level of the API (#23)
1 parent bd45268 commit b08d4f9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/DocsParser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class DocsParser {
4040
private moduleVersion: string,
4141
private apiFiles: string[],
4242
private structureFiles: string[],
43+
private packageMode: 'single' | 'multi',
4344
) {}
4445

4546
private async parseBaseContainers(
@@ -132,6 +133,7 @@ export class DocsParser {
132133
const allTokens = md.parse(contents, {});
133134

134135
const baseInfos = await this.parseBaseContainers(filePath, contents, allTokens);
136+
let lastModule: ModuleDocumentationContainer | null = null;
135137
for (const { container, tokens, isClass } of baseInfos) {
136138
let isElement = false;
137139
if (container.name.endsWith('` Tag')) {
@@ -189,6 +191,11 @@ export class DocsParser {
189191
instanceEvents: parseEventBlocks(findContentInsideHeader(tokens, 'Instance Events', 3)),
190192
instanceName,
191193
});
194+
// If we're inside a module, pop off the class and put it in the module as an exported class
195+
// Only do this in "multi package" mode as when we are in a single package mode everything is exported at the
196+
// top level. In multi-package mode things are exported under each module so we need the nesting to be correct
197+
if (this.packageMode === 'multi' && lastModule)
198+
lastModule.exportedClasses.push(parsed.pop() as ClassDocumentationContainer);
192199
} else {
193200
// This is a module
194201
if (isElement) {
@@ -214,7 +221,10 @@ export class DocsParser {
214221
properties: parsePropertyBlocks(findContentInsideHeader(tokens, 'Properties', 2)),
215222
// ## Events
216223
events: parseEventBlocks(findContentInsideHeader(tokens, 'Events', 2)),
224+
// ## Class: MyClass
225+
exportedClasses: [],
217226
});
227+
lastModule = parsed[parsed.length - 1] as ModuleDocumentationContainer;
218228
}
219229
}
220230
}

src/ParsedDocumentation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export declare type ModuleDocumentationContainer = {
9191
instanceProperties?: undefined;
9292
staticProperties?: undefined;
9393
staticMethods?: undefined;
94+
exportedClasses: ClassDocumentationContainer[];
9495
} & BaseDocumentationContainer;
9596
export declare type StructureDocumentationContainer = {
9697
type: 'Structure';

src/bin.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ import pretty from 'pretty-ms';
99
import { parseDocs } from '.';
1010
import chalk from 'chalk';
1111

12-
const args = minimist(process.argv);
12+
const args = minimist(process.argv, {
13+
default: {
14+
packageMode: 'single',
15+
},
16+
});
1317

14-
const { dir, outDir, help } = args;
18+
const { dir, outDir, packageMode, help } = args;
19+
if (!['single', 'multi'].includes(packageMode)) {
20+
console.error(chalk.red('packageMode must be one of "single" and "multi"'));
21+
process.exit(1);
22+
}
1523

1624
if (help) {
1725
console.info(
@@ -58,6 +66,7 @@ fs.mkdirp(resolvedOutDir).then(() =>
5866
parseDocs({
5967
baseDirectory: resolvedDir,
6068
moduleVersion: pj.version,
69+
packageMode,
6170
})
6271
.then(data =>
6372
fs.writeJson(path.resolve(resolvedOutDir, './electron-api.json'), data, {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import { DocsParser } from './DocsParser';
55
type ParseOptions = {
66
baseDirectory: string;
77
moduleVersion: string;
8+
packageMode?: 'single' | 'multi';
89
};
910

1011
export async function parseDocs(options: ParseOptions) {
12+
const packageMode = options.packageMode || 'single';
1113
const electronDocsPath = path.resolve(options.baseDirectory, 'docs', 'api');
1214

1315
const parser = new DocsParser(
1416
options.baseDirectory,
1517
options.moduleVersion,
1618
await getAllMarkdownFiles(electronDocsPath),
1719
await getAllMarkdownFiles(path.resolve(electronDocsPath, 'structures')),
20+
packageMode,
1821
);
1922

2023
return await parser.parse();

0 commit comments

Comments
 (0)