Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 1 addition & 17 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@ import { Argument, Command, Option } from 'commander';
import interactive from './commands/interactive.mjs';
import list, { types } from './commands/list.mjs';
import commands from './commands/index.mjs';
import { errorWrap } from './utils.mjs';

const program = new Command()
.name('api-docs-tooling')
.description('CLI tool to generate and lint Node.js API documentation');

/**
* Wraps a function to catch both synchronous and asynchronous errors.
*
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
* @returns {Function} A new function that handles errors and logs them.
*/
const errorWrap =
fn =>
async (...args) => {
try {
return await fn(...args);
} catch (err) {
console.error(err);
process.exit(1);
}
};

// Registering generate and lint commands
commands.forEach(({ name, description, options, action }) => {
const cmd = program.command(name).description(description);
Expand Down
41 changes: 36 additions & 5 deletions bin/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import createMarkdownLoader from '../src/loaders/markdown.mjs';
import createMarkdownParser from '../src/parsers/markdown.mjs';

// Instantiate loader and parser once to reuse
const loader = createMarkdownLoader();
const parser = createMarkdownParser();
/**
* Generic lazy initializer.
* @template T
* @param {() => T} factory - Function to create the instance.
* @returns {() => T} - A function that returns the singleton instance.
*/
export const lazy = factory => {
let instance;
return () => (instance ??= factory());
};

// Instantiate loader and parser once to reuse,
// but only if/when we actually need them. No need
// to create these objects just to load a different
// utility.
const loader = lazy(createMarkdownLoader);
const parser = lazy(createMarkdownParser);

/**
* Load and parse markdown API docs.
Expand All @@ -12,10 +26,27 @@ const parser = createMarkdownParser();
* @returns {Promise<ApiDocMetadataEntry[]>} - Parsed documentation objects.
*/
export async function loadAndParse(input, ignore) {
const files = await loader.loadFiles(input, ignore);
return parser.parseApiDocs(files);
const files = await loader().loadFiles(input, ignore);
return parser().parseApiDocs(files);
}

/**
* Wraps a function to catch both synchronous and asynchronous errors.
*
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
* @returns {Function} A new function that handles errors and logs them.
*/
export const errorWrap =
fn =>
async (...args) => {
try {
return await fn(...args);
} catch (err) {
console.error(err);
process.exit(1);
}
};

/**
* Represents a command-line option for the linter CLI.
* @typedef {Object} Option
Expand Down
Loading