(descriptor)
| 1650 | } |
| 1651 | |
| 1652 | function normalizeExternalCommand(descriptor) { |
| 1653 | const name = |
| 1654 | typeof descriptor?.name === "string" ? descriptor.name.trim() : ""; |
| 1655 | if (!name) { |
| 1656 | console.warn("Command registration skipped: missing name", descriptor); |
| 1657 | return null; |
| 1658 | } |
| 1659 | const exec = typeof descriptor?.exec === "function" ? descriptor.exec : null; |
| 1660 | if (!exec) { |
| 1661 | console.warn( |
| 1662 | `Command registration skipped for "${name}": exec must be a function.`, |
| 1663 | ); |
| 1664 | return null; |
| 1665 | } |
| 1666 | |
| 1667 | const requiresView = descriptor?.requiresView ?? true; |
| 1668 | const key = normalizeExternalKey(descriptor?.bindKey); |
| 1669 | |
| 1670 | return { |
| 1671 | name, |
| 1672 | description: descriptor?.description || humanizeCommandName(name), |
| 1673 | readOnly: descriptor?.readOnly ?? true, |
| 1674 | requiresView, |
| 1675 | key, |
| 1676 | run(view, args) { |
| 1677 | try { |
| 1678 | const resolvedView = resolveView(view); |
| 1679 | if (requiresView && !resolvedView) return false; |
| 1680 | const result = exec(resolvedView || null, args); |
| 1681 | return result !== false; |
| 1682 | } catch (error) { |
| 1683 | console.error(`Command \"${name}\" failed`, error); |
| 1684 | return false; |
| 1685 | } |
| 1686 | }, |
| 1687 | }; |
| 1688 | } |
| 1689 | |
| 1690 | function normalizeExternalKey(bindKey) { |
| 1691 | if (!bindKey) return null; |
no test coverage detected