Skip to content
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

feat: introduced CLI mode to the parser #36

Merged
merged 10 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bin/.gitkeep

This file was deleted.

53 changes: 53 additions & 0 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

import { argv } from 'node:process';
import { resolve } from 'node:path';

import { Command, Option } from 'commander';

import createGenerator from '../src/generators.mjs';
import createLoader from '../src/loader.mjs';
import createParser from '../src/parser.mjs';
import { CLI_TARGET_MAPPING } from '../src/constants.mjs';

const program = new Command();

program
.requiredOption(
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
'-i, --input <patterns...>',
'Specify input file patterns using glob syntax'
)
.requiredOption('-o, --output <path>', 'Specify the output directory path')
.addOption(
new Option(
'-t, --target [mode...]',
'Set the processing target mode'
).choices(Object.keys(CLI_TARGET_MAPPING))
)
.parse(argv);

/**
* Defines the structure for command line options after parsing.
*
* @typedef {Object} Options
* @property {Array<string>|string} input Specifies the glob/path for input files.
* @property {string} output Specifies the directory where output files will be saved.
* @property {keyof CLI_TARGET_MAPPING} target Specifies the execution target mode.
*/

/** @type {Options} */
const { input, output, target } = program.opts();

const { loadFiles } = createLoader();
const { parseApiDocs } = createParser();

const apiDocFiles = loadFiles(input);

const parsedApiDocs = await parseApiDocs(apiDocFiles);

const { runGenerators } = createGenerator(parsedApiDocs);

await runGenerators({
generators: target.map(mode => CLI_TARGET_MAPPING[mode]),
output: resolve(output),
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"name": "@node-core/api-docs-tooling",
"scripts": {
"lint": "eslint .",
"format": "prettier --write .",
"prepare": "husky"
},
"bin": {
"api-docs-tooling": "./bin/cli.mjs"
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@eslint/js": "^9.7.0",
"@types/node": "^20.14.10",
Expand All @@ -16,6 +20,7 @@
},
"dependencies": {
"github-slugger": "^2.0.0",
"commander": "^12.1.0",
"glob": "^11.0.0",
"remark": "^15.0.1",
"remark-gfm": "^4.0.0",
Expand Down
7 changes: 7 additions & 0 deletions src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,10 @@ export const DOC_TYPES_MAPPING_OTHER = {
Response: `${DOC_MDN_BASE_URL}/API/Response`,
Request: `${DOC_MDN_BASE_URL}/API/Request`,
};

// Mapping that determines which generators are run on the target modes used in the CLI
export const CLI_TARGET_MAPPING = {
canerakdas marked this conversation as resolved.
Show resolved Hide resolved
mdx: 'mdx',
json: 'jsonSimple',
html: 'legacyHtml',
};
Loading