@ably/cli
Version:
Ably CLI for Pub/Sub, Chat and Spaces
53 lines (52 loc) • 1.92 kB
JavaScript
import { Command } from '@oclif/core';
/**
* Base command class that provides interactive-mode-safe error handling.
* When running in interactive mode, this class converts process.exit calls
* to thrown errors that can be caught and handled gracefully.
*/
export class InteractiveBaseCommand extends Command {
error(input, options) {
const error = typeof input === 'string' ? new Error(input) : input;
// In interactive mode, throw the error to be caught
if (process.env.ABLY_INTERACTIVE_MODE === 'true' && options?.exit !== false) {
// Add oclif error metadata
error.oclif = {
exit: options?.exit ?? 1,
code: options?.code
};
if (process.env.DEBUG) {
console.error('[InteractiveBaseCommand] Throwing error instead of exiting:', error.message);
}
throw error;
}
// In normal mode or when exit is false, use default behavior
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return super.error(input, options);
}
/**
* Override exit to throw instead of exit in interactive mode
*/
exit(code = 0) {
if (process.env.ABLY_INTERACTIVE_MODE === 'true') {
const error = new Error(`Command exited with code ${code}`);
error.exitCode = code;
error.code = 'EEXIT';
throw error;
}
super.exit(code);
// TypeScript needs this even though super.exit never returns
throw new Error('Unreachable');
}
/**
* Override log to ensure proper output in interactive mode
*/
log(message, ...args) {
// Ensure logs are displayed properly in interactive mode
if (message === undefined) {
console.log();
}
else {
console.log(message, ...args);
}
}
}