@black-flag/core
Version:
A declarative framework for building fluent, deeply hierarchical command line interfaces with yargs
166 lines (165 loc) • 7.02 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.GracefulEarlyExitError = exports.CommandNotImplementedError = exports.CliError = exports.BfErrorMessage = exports.AssertionFailedError = void 0;
exports.isYargsError = isYargsError;
var _types = require("node:util/types");
var _error = require("@-xun/error");
var _constant = require("./constant.js");
function isYargsError(parameter) {
return (0, _types.isNativeError)(parameter) && parameter.name === 'YError';
}
const {
CliError
} = (0, _error.makeNamedError)(class CliError extends Error {
suggestedExitCode = _constant.FrameworkExitCode.DefaultError;
showHelp = 'default';
dangerouslyFatal = false;
constructor(reason, options = {}, message = undefined, superOptions = {}) {
const {
suggestedExitCode,
showHelp,
dangerouslyFatal
} = options;
let {
cause
} = options;
message = message ?? (typeof reason === 'string' ? reason : reason?.message) ?? BfErrorMessage.Generic();
if (!('cause' in options)) {
cause = typeof reason === 'string' ? undefined : reason;
}
super(message, {
cause,
...superOptions
});
if (suggestedExitCode !== undefined) {
this.suggestedExitCode = suggestedExitCode;
}
if (showHelp !== undefined) {
this.showHelp = showHelp === true ? 'short' : showHelp;
}
if (dangerouslyFatal !== undefined) {
this.dangerouslyFatal = dangerouslyFatal;
}
}
}, 'CliError');
exports.CliError = CliError;
const {
CommandNotImplementedError
} = (0, _error.makeNamedError)(class CommandNotImplementedError extends CliError {
constructor(error, options) {
super(error ? error.message : BfErrorMessage.CommandNotImplemented(), {
...(error ? {
cause: error
} : {}),
showHelp: false,
...options,
suggestedExitCode: _constant.FrameworkExitCode.NotImplemented
});
}
}, 'CommandNotImplementedError');
exports.CommandNotImplementedError = CommandNotImplementedError;
const {
GracefulEarlyExitError
} = (0, _error.makeNamedError)(class GracefulEarlyExitError extends CliError {
constructor(reason, options) {
super(reason ? typeof reason === 'string' ? reason : reason.message : BfErrorMessage.GracefulEarlyExit(), {
...(reason ? {
cause: typeof reason === 'string' ? new Error(reason) : reason
} : {}),
showHelp: false,
...options,
suggestedExitCode: _constant.FrameworkExitCode.Ok,
dangerouslyFatal: false
});
}
}, 'GracefulEarlyExitError');
exports.GracefulEarlyExitError = GracefulEarlyExitError;
const {
AssertionFailedError
} = (0, _error.makeNamedError)(class AssertionFailedError extends CliError {
constructor(errorOrMessage, options) {
super(typeof errorOrMessage === 'string' ? errorOrMessage : errorOrMessage?.message, {
showHelp: false,
...options,
suggestedExitCode: _constant.FrameworkExitCode.AssertionFailed,
cause: errorOrMessage
});
}
}, 'AssertionFailedError');
exports.AssertionFailedError = AssertionFailedError;
const BfErrorMessage = exports.BfErrorMessage = {
GuruMeditation() {
return 'sanity check failed';
},
BuilderCalledOnInvalidPass(pass) {
return `a builder function was invoked during Black Flag's ${pass.replace('-', ' ')} when it expected to be invoked during its ${pass === 'first-pass' ? 'second pass' : 'first pass'} instead`;
},
BuilderCannotBeAsync(commandName) {
return `command "${commandName}" exported an asynchronous builder function, which triggers buggy behavior in Yargs. Black Flag supports exporting an asynchronous default function in CJS files and top-level await in ESM files, so hoist any async logic out of this command's builder. See the documentation for details`;
},
Generic() {
return 'an error occurred that caused this software to crash';
},
CommandNotImplemented() {
return 'this command is currently unimplemented';
},
InvalidSubCommandInvocation() {
return 'invalid subcommand: you must call this with a valid subcommand argument';
},
FrameworkError(error) {
return `UNHANDLED FRAMEWORK EXCEPTION: an error occurred due to a misconfiguration. This is typically due to developer error and as such cannot be fixed by end-users. Please report this incident to the developer of this application. For more information about this error, rerun the command with the DEBUG='bf:*' or DEBUG='*' environment variable set.\n\nException details: ${(0, _types.isNativeError)(error) ? error.stack || error : String(error)}`;
},
GracefulEarlyExit() {
return 'execution is ending exceptionally early, which is not a bad thing!';
},
ConfigLoadFailure(path) {
return `failed to load configuration from file: ${path}`;
},
InvalidConfigureArgumentsReturnType() {
return 'configureArguments must return typeof process.argv';
},
InvalidConfigureExecutionEpilogueReturnType() {
return 'configureExecutionEpilogue must return Arguments';
},
InvalidConfigureExecutionContextReturnType() {
return 'configureExecutionContext must return ExecutionContext';
},
InvalidExecutionContextBadField(fieldName) {
return `encountered invalid or impossible value for ExecutionContext field "${fieldName}"`;
},
InvalidCharacters(str, violation) {
return `string "${str}" contains one or more illegal characters: ${violation}`;
},
PathIsNotDirectory() {
return 'path is not a directory';
},
DuplicateCommandName(parentFullName, name1, type1, name2, type2) {
return (parentFullName ? `one or more child commands of "${parentFullName}" are` : `the root command is`) + ` attempting to register conflicting command names and/or aliases: "${name1}" (${type1}) conflicts with "${name2}" (${type2})`;
},
InvalidCommandExportBadStart(name) {
return `the ${name}'s command export must start with either "$0" or "$0 "`;
},
InvalidCommandExportBadPositionals(name) {
return `the ${name}'s command export must be a valid Yargs command DSL string. See: https://github.com/yargs/yargs/blob/main/docs/advanced.md#positional-arguments for examples`;
},
NoConfigurationLoaded(path) {
return `auto-discovery failed to find any valid configuration files or directories at path: ${path}`;
},
BadConfigurationPath(path) {
return `auto-discovery failed because configuration module path is unreadable or does not exist: "${String(path)}"`;
},
InvocationNotAllowed(name) {
return `invocation of method "${name}" is not allowed here. See documentation for details`;
},
CannotExecuteMultipleTimes() {
return 'Yargs does not support safely calling "parse"/"parseAsync" more than once on the same instance. See documentation for details';
},
BadParameterCombination() {
return 'must provide exactly one of the following options: configurationHooks, preExecutionContext';
},
UseParseAsyncInstead() {
return '"parseSync" is not supported. Use "parseAsync" instead';
}
};
;