Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 20 additions & 3 deletions bin/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ 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 All @@ -33,21 +50,21 @@ commands.forEach(({ name, description, options, action }) => {
});

// Set the action for the command
cmd.action(action);
cmd.action(errorWrap(action));
});

// Register the interactive command
program
.command('interactive')
.description('Launch guided CLI wizard')
.action(interactive);
.action(errorWrap(interactive));

// Register the list command
program
.command('list')
.addArgument(new Argument('<types>', 'The type to list').choices(types))
.description('List the given type')
.action(list);
.action(errorWrap(list));

// Parse and execute command-line arguments
program.parse(process.argv);
6 changes: 5 additions & 1 deletion src/threading/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export default class WorkerPool {
this.changeActiveThreadCount(-1);
this.processQueue(threads);

(result?.error ? reject : resolve)(result);
if (result?.error) {
reject(result.error);
} else {
resolve(result);
}
});

// Handle worker thread errors
Expand Down
Loading