UNPKG

@black-flag/core

Version:

A declarative framework for building fluent, deeply hierarchical command line interfaces with yargs

72 lines (71 loc) 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.capitalize = capitalize; exports.expectedHelpTextRegExp = expectedHelpTextRegExp; exports.getDeepestErrorCause = getDeepestErrorCause; exports.isArguments = isArguments; exports.isAssertionSystemError = isAssertionSystemError; exports.isNullArguments = isNullArguments; exports.isPreExecutionContext = isPreExecutionContext; exports.wrapExecutionContext = wrapExecutionContext; require("core-js/modules/es.iterator.constructor.js"); require("core-js/modules/es.iterator.for-each.js"); require("core-js/modules/es.iterator.map.js"); require("core-js/modules/es.regexp.escape.js"); var _types = require("node:util/types"); var _constant = require("./constant.js"); const trimEscapedNewlinesRegExp = /^(?:\\n|\s)*(.*?)(?:\\n|\s)*$/s; function isPreExecutionContext(obj) { return !!obj && typeof obj === 'object' && 'commands' in obj && 'debug' in obj && 'state' in obj && 'execute' in obj && 'rootPrograms' in obj && 'executionContext' in obj; } function isNullArguments(obj) { return isArguments(obj) && obj.$0 === _constant.nullArguments$0 && obj._.length === 0; } function isArguments(obj) { return !!obj && typeof obj === 'object' && '$0' in obj && typeof obj.$0 === 'string' && '_' in obj && Array.isArray(obj._) && _constant.$executionContext in obj; } function isAssertionSystemError(error) { return (0, _types.isNativeError)(error) && 'code' in error && error.code === 'ERR_ASSERTION' && 'actual' in error && 'expected' in error && 'operator' in error; } function wrapExecutionContext(context) { return { [_constant.$executionContext]: context }; } function capitalize(str) { return (str.at(0)?.toUpperCase() || '') + str.slice(1); } function getDeepestErrorCause(error) { return (0, _types.isNativeError)(error) && error.cause ? getDeepestErrorCause(error.cause) : error; } function expectedHelpTextRegExp({ parentFullName, usage = true, commandGroups, optionGroups, startsWith: start = /^/, endsWith: end = /$/ }) { let regExpString = ''; regExpString += stringOrRegExpToString(start); regExpString += usage === true ? String.raw`(.|\n)+?` : usage === false ? '' : stringOrRegExpToString(usage); Object.entries(commandGroups || {}).forEach(([group, lines], index) => { regExpString += String.raw`${index === 0 ? '\n' : ''}\n${stringOrRegExpToString(group)}:\n` + lines.map(line => { return String.raw`\s+${parentFullName ? stringOrRegExpToString(parentFullName) : ''}\s+${stringOrRegExpToString(Array.isArray(line) ? line[0] : line)}\s*${Array.isArray(line) ? stringOrRegExpToString(line[1]) : ''}[^\n]*\n`; }).join(''); }); regExpString = regExpString.match(trimEscapedNewlinesRegExp)[1]; Object.entries(optionGroups || {}).forEach(([group, lines], index) => { regExpString += String.raw`${index === 0 ? '\n' : ''}\n${stringOrRegExpToString(group)}:\n` + lines.map(line => { return String.raw`\s+${stringOrRegExpToString(Array.isArray(line) ? line[0] : line)}\s*${Array.isArray(line) ? stringOrRegExpToString(line[1]) : ''}[^\n]*\n`; }).join(''); }); regExpString = regExpString.match(trimEscapedNewlinesRegExp)[1]; regExpString += stringOrRegExpToString(end); return new RegExp(regExpString); } function stringOrRegExpToString(stringOrRegExp) { return typeof stringOrRegExp === 'string' ? RegExp.escape(stringOrRegExp) : stringOrRegExp?.source; }