diff --git a/bin/.gitkeep b/bin/.gitkeep deleted file mode 100644 index f935021..0000000 --- a/bin/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -!.gitignore diff --git a/bin/cli.mjs b/bin/cli.mjs new file mode 100755 index 0000000..11daeab --- /dev/null +++ b/bin/cli.mjs @@ -0,0 +1,57 @@ +#!/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 generators from '../src/generators/index.mjs'; + +const program = new Command(); + +program + .name('api-docs-tooling') + .description('CLI tool to generate API documentation of a Node.js project.') + .requiredOption( + '-i, --input ', + 'Specify input file patterns using glob syntax' + ) + .requiredOption('-o, --output ', 'Specify the output directory path') + .addOption( + new Option( + '-t, --target [mode...]', + 'Set the processing target mode' + ).choices(Object.keys(generators)) + ) + .parse(argv); + +/** + * @typedef {keyof generators} Target A list of the available generator names. + * + * @typedef {Object} Options + * @property {Array|string} input Specifies the glob/path for input files. + * @property {string} output Specifies the directory where output files will be saved. + * @property {Target} target Specifies the generator target mode. + * + * @name ProgramOptions + * @type {Options} + * @description The return type for values sent to the program from the CLI. + */ +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, + output: resolve(output), +}); diff --git a/package-lock.json b/package-lock.json index aa9b79d..85b00e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "packages": { "": { "dependencies": { + "commander": "^12.1.0", "github-slugger": "^2.0.0", "glob": "^11.0.0", "remark": "^15.0.1", @@ -553,7 +554,6 @@ "version": "12.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", - "dev": true, "engines": { "node": ">=18" } diff --git a/package.json b/package.json index abed3cc..835878f 100644 --- a/package.json +++ b/package.json @@ -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" + }, "devDependencies": { "@eslint/js": "^9.7.0", "@types/node": "^20.14.10", @@ -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", diff --git a/src/generators/index.mjs b/src/generators/index.mjs index 3bb6177..be8af5b 100644 --- a/src/generators/index.mjs +++ b/src/generators/index.mjs @@ -3,4 +3,4 @@ import jsonSimple from './json-simple/index.mjs'; import legacyHtml from './legacy-html/index.mjs'; -export default { jsonSimple, legacyHtml }; +export default { 'json-simple': jsonSimple, 'legacy-html': legacyHtml }; diff --git a/src/generators/json-simple/index.mjs b/src/generators/json-simple/index.mjs index e04bf32..5433bdc 100644 --- a/src/generators/json-simple/index.mjs +++ b/src/generators/json-simple/index.mjs @@ -15,7 +15,7 @@ import { join } from 'node:path'; * @type {import('../types.d.ts').GeneratorMetadata} */ export default { - name: 'jsonSimple', + name: 'json-simple', version: '1.0.0', diff --git a/src/generators/legacy-html/index.mjs b/src/generators/legacy-html/index.mjs index 23824ef..ed12ee1 100644 --- a/src/generators/legacy-html/index.mjs +++ b/src/generators/legacy-html/index.mjs @@ -12,7 +12,7 @@ * @type {import('../types.d.ts').GeneratorMetadata} */ export default { - name: 'legacyHtml', + name: 'legacy-html', version: '1.0.0',