Skip to content

Commit ce71a9e

Browse files
committed
move to utils, add lazy
1 parent b6cf072 commit ce71a9e

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

bin/cli.mjs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,12 @@ import { Argument, Command, Option } from 'commander';
66
import interactive from './commands/interactive.mjs';
77
import list, { types } from './commands/list.mjs';
88
import commands from './commands/index.mjs';
9+
import { errorWrap } from './utils.mjs';
910

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

14-
/**
15-
* Wraps a function to catch both synchronous and asynchronous errors.
16-
*
17-
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
18-
* @returns {Function} A new function that handles errors and logs them.
19-
*/
20-
const errorWrap =
21-
fn =>
22-
async (...args) => {
23-
try {
24-
return await fn(...args);
25-
} catch (err) {
26-
console.error(err);
27-
process.exit(1);
28-
}
29-
};
30-
3115
// Registering generate and lint commands
3216
commands.forEach(({ name, description, options, action }) => {
3317
const cmd = program.command(name).description(description);

bin/utils.mjs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import createMarkdownLoader from '../src/loaders/markdown.mjs';
22
import createMarkdownParser from '../src/parsers/markdown.mjs';
33

4-
// Instantiate loader and parser once to reuse
5-
const loader = createMarkdownLoader();
6-
const parser = createMarkdownParser();
4+
/**
5+
* Generic lazy initializer.
6+
* @template T
7+
* @param {() => T} factory - Function to create the instance.
8+
* @returns {() => T} - A function that returns the singleton instance.
9+
*/
10+
export const lazy = factory => {
11+
let instance;
12+
return () => (instance ??= factory());
13+
};
14+
15+
// Instantiate loader and parser once to reuse,
16+
// but only if/when we actually need them. No need
17+
// to create these objects just to load a different
18+
// utility.
19+
const loader = lazy(createMarkdownLoader);
20+
const parser = lazy(createMarkdownParser);
721

822
/**
923
* Load and parse markdown API docs.
@@ -12,10 +26,27 @@ const parser = createMarkdownParser();
1226
* @returns {Promise<ApiDocMetadataEntry[]>} - Parsed documentation objects.
1327
*/
1428
export async function loadAndParse(input, ignore) {
15-
const files = await loader.loadFiles(input, ignore);
16-
return parser.parseApiDocs(files);
29+
const files = await loader().loadFiles(input, ignore);
30+
return parser().parseApiDocs(files);
1731
}
1832

33+
/**
34+
* Wraps a function to catch both synchronous and asynchronous errors.
35+
*
36+
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
37+
* @returns {Function} A new function that handles errors and logs them.
38+
*/
39+
export const errorWrap =
40+
fn =>
41+
async (...args) => {
42+
try {
43+
return await fn(...args);
44+
} catch (err) {
45+
console.error(err);
46+
process.exit(1);
47+
}
48+
};
49+
1950
/**
2051
* Represents a command-line option for the linter CLI.
2152
* @typedef {Object} Option

0 commit comments

Comments
 (0)