Skip to content

Commit 195cc73

Browse files
committed
refactor: create metadata generator
1 parent 322f4bf commit 195cc73

File tree

18 files changed

+228
-183
lines changed

18 files changed

+228
-183
lines changed

bin/commands/generate.mjs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { cpus } from 'node:os';
22
import { resolve } from 'node:path';
3-
import process from 'node:process';
43

54
import { coerce } from 'semver';
65

@@ -12,7 +11,6 @@ import createGenerator from '../../src/generators.mjs';
1211
import { publicGenerators } from '../../src/generators/index.mjs';
1312
import createNodeReleases from '../../src/releases.mjs';
1413
import { loadAndParse } from '../utils.mjs';
15-
import { runLint } from './lint.mjs';
1614

1715
const availableGenerators = Object.keys(publicGenerators);
1816

@@ -125,22 +123,29 @@ export default {
125123
async action(opts) {
126124
const docs = await loadAndParse(opts.input, opts.ignore);
127125

128-
if (!opts.skipLint && !runLint(docs)) {
129-
console.error('Lint failed; aborting generation.');
130-
process.exit(1);
131-
}
126+
// if (!opts.skipLint && !runLint(docs)) {
127+
// console.error('Lint failed; aborting generation.');
128+
// process.exit(1);
129+
// }
132130

133-
const { runGenerators } = createGenerator(docs);
134131
const { getAllMajors } = createNodeReleases(opts.changelog);
135132

136-
await runGenerators({
137-
generators: opts.target,
138-
input: opts.input,
139-
output: opts.output && resolve(opts.output),
140-
version: coerce(opts.version),
141-
releases: await getAllMajors(),
142-
gitRef: opts.gitRef,
143-
threads: parseInt(opts.threads, 10),
144-
});
133+
const releases = await getAllMajors();
134+
135+
await Promise.all(
136+
docs.map(async doc => {
137+
const { runGenerators } = createGenerator(doc);
138+
139+
await runGenerators({
140+
generators: opts.target,
141+
input: opts.input,
142+
output: opts.output && resolve(opts.output),
143+
version: coerce(opts.version),
144+
releases,
145+
gitRef: opts.gitRef,
146+
threads: parseInt(opts.threads, 10),
147+
});
148+
})
149+
);
145150
},
146151
};

bin/utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const parser = lazy(createMarkdownParser);
2323
* Load and parse markdown API docs.
2424
* @param {string[]} input - Glob patterns for input files.
2525
* @param {string[]} [ignore] - Glob patterns to ignore.
26-
* @returns {Promise<ApiDocMetadataEntry[]>} - Parsed documentation objects.
26+
* @returns {Promise<ParserOutput<import('mdast').Root>[]>}
2727
*/
2828
export async function loadAndParse(input, ignore) {
2929
const files = await loader().loadFiles(input, ignore);

src/generators.mjs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { allGenerators } from './generators/index.mjs';
44
import WorkerPool from './threading/index.mjs';
55

66
/**
7-
* @typedef {{ ast: GeneratorMetadata<ApiDocMetadataEntry, ApiDocMetadataEntry>}} AstGenerator The AST "generator" is a facade for the AST tree and it isn't really a generator
8-
* @typedef {AvailableGenerators & AstGenerator} AllGenerators A complete set of the available generators, including the AST one
9-
* @param markdownInput
10-
* @param jsInput
11-
*
127
* This method creates a system that allows you to register generators
138
* and then execute them in a specific order, keeping track of the
149
* generation process, and handling errors that may occur from the
@@ -21,18 +16,20 @@ import WorkerPool from './threading/index.mjs';
2116
* Generators can also write to files. These would usually be considered
2217
* the final generators in the chain.
2318
*
24-
* @param {ApiDocMetadataEntry} markdownInput The parsed API doc metadata entries
25-
* @param {Array<import('acorn').Program>} parsedJsFiles
19+
* @typedef {{ ast: GeneratorMetadata<ParserOutput, ParserOutput>}} AstGenerator The AST "generator" is a facade for the AST tree and it isn't really a generator
20+
* @typedef {AvailableGenerators & AstGenerator} AllGenerators A complete set of the available generators, including the AST one
21+
*
22+
* @param {ParserOutput} input The API doc AST tree
2623
*/
27-
const createGenerator = markdownInput => {
24+
const createGenerator = input => {
2825
/**
2926
* We store all the registered generators to be processed
3027
* within a Record, so we can access their results at any time whenever needed
3128
* (we store the Promises of the generator outputs)
3229
*
3330
* @type {{ [K in keyof AllGenerators]: ReturnType<AllGenerators[K]['generate']> }}
3431
*/
35-
const cachedGenerators = { ast: Promise.resolve(markdownInput) };
32+
const cachedGenerators = { ast: Promise.resolve(input) };
3633

3734
const threadPool = new WorkerPool();
3835

src/generators/addon-verify/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
description:
3131
'Generates a file list from code blocks extracted from `doc/api/addons.md` to facilitate C++ compilation and JavaScript runtime validations',
3232

33-
dependsOn: 'ast',
33+
dependsOn: 'metadata',
3434

3535
/**
3636
* Generates a file list from code blocks.

src/generators/ast-js/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default {
2020

2121
description: 'Parses Javascript source files passed into the input.',
2222

23-
dependsOn: 'ast',
23+
dependsOn: 'metadata',
2424

2525
/**
2626
* @param {Input} _

src/generators/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import apiLinks from './api-links/index.mjs';
1111
import oramaDb from './orama-db/index.mjs';
1212
import astJs from './ast-js/index.mjs';
1313
import llmsTxt from './llms-txt/index.mjs';
14+
import metadata from './metadata/index.mjs';
1415

1516
export const publicGenerators = {
17+
metadata: metadata,
1618
'json-simple': jsonSimple,
1719
'legacy-html': legacyHtml,
1820
'legacy-html-all': legacyHtmlAll,

src/generators/json-simple/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default {
2626
description:
2727
'Generates the simple JSON version of the API docs, and returns it as a string',
2828

29-
dependsOn: 'ast',
29+
dependsOn: 'metadata',
3030

3131
/**
3232
* Generates the simplified JSON version of the API docs

src/generators/legacy-html/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default {
4141
description:
4242
'Generates the legacy version of the API docs in HTML, with the assets and styles included as files',
4343

44-
dependsOn: 'ast',
44+
dependsOn: 'metadata',
4545

4646
/**
4747
* Generates the legacy version of the API docs in HTML

src/generators/legacy-json/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525

2626
description: 'Generates the legacy version of the JSON API docs.',
2727

28-
dependsOn: 'ast',
28+
dependsOn: 'metadata',
2929

3030
/**
3131
* Generates a legacy JSON file.

src/generators/llms-txt/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default {
1919
description:
2020
'Generates a llms.txt file to provide information to LLMs at inference time',
2121

22-
dependsOn: 'ast',
22+
dependsOn: 'metadata',
2323

2424
/**
2525
* Generates a llms.txt file

0 commit comments

Comments
 (0)