Spaces:
Runtime error
Runtime error
File size: 2,932 Bytes
a6b96c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | "use strict";
/**
* CJS Command Router Adapter Module
*
* Compatibility routing for gsd-tools.cjs command families. Uses generated
* command metadata for availability and small family-local argument shapers for
* CJS handler calls.
*
* ADR-457 build-at-publish: the hand-written bin/lib/cjs-command-router-adapter.cjs
* collapsed to a TypeScript source of truth. Behaviour is preserved byte-for-behaviour
* from the prior hand-written .cjs; only types are added.
*/
// eslint-disable-next-line @typescript-eslint/no-require-imports
const commandRoutingHub = require("./command-routing-hub.cjs");
const { createHub, ERROR_KINDS } = commandRoutingHub;
// βββ Implementation βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function routeCjsCommandFamily({ args, subcommands, handlers, defaultSubcommand, unsupported = {}, unknownMessage, error, cwd, raw, }) {
routeHubCommandFamily({
family: '__legacy_cjs_family__',
args,
subcommands,
handlers,
defaultSubcommand,
unsupported,
unknownMessage,
error,
cwd,
raw,
});
}
/**
* Hub-backed family router adapter.
*
* Deepens the command-topology seam by routing family handlers through
* CommandRoutingHub's typed Result contract instead of ad-hoc per-router
* lookup + error handling branches.
*/
function routeHubCommandFamily({ family, args, subcommands, handlers, defaultSubcommand, unsupported = {}, unknownMessage, error, cwd, raw, }) {
const subcommand = args[1] || defaultSubcommand;
if (subcommand && unsupported[subcommand]) {
error(unsupported[subcommand]);
return;
}
const available = subcommands.filter((s) => !unsupported[s]);
const registryHandlers = Object.fromEntries(Object.entries(handlers).map(([name, handler]) => [
name,
() => {
const result = handler();
if (result && typeof result === 'object' && Object.prototype.hasOwnProperty.call(result, 'ok')) {
return result;
}
return { ok: true, data: null };
},
]));
const hub = createHub({
cjsRegistry: { [family]: registryHandlers },
manifest: { [family]: available },
});
const result = hub.dispatch({
family,
subcommand,
args: args.slice(2),
cwd,
raw,
});
if (result.ok)
return;
if (result.kind === ERROR_KINDS.UnknownCommand) {
error(unknownMessage(subcommand ?? '', available));
return;
}
if (result.kind === ERROR_KINDS.InvalidArgs || result.kind === ERROR_KINDS.HandlerRefusal) {
error(result.reason);
return;
}
error(result.message);
}
module.exports = {
routeCjsCommandFamily,
routeHubCommandFamily,
};
|