|
| 1 | +import { commands, field, object, program, help } from "configliere"; |
| 2 | +import packageJSON from "../package.json" with { type: "json" }; |
| 3 | +import { type } from "arktype"; |
| 4 | +import { scope, player } from "../lib/protocols.ts"; |
| 5 | +import { combine } from "../mod.ts"; |
| 6 | +import type { ParsedConfig } from "./types.ts"; |
| 7 | + |
| 8 | +export const inspector = combine.protocols(scope.protocol, player.protocol); |
| 9 | + |
| 10 | +const commandBase = object({ |
| 11 | + out: { |
| 12 | + description: "write out the response to a file", |
| 13 | + ...field(type("string | undefined"), field.default(undefined)), |
| 14 | + }, |
| 15 | + host: { |
| 16 | + description: "inspector base URL (overrides default)", |
| 17 | + ...field(type("string"), field.default("http://localhost:41000")), |
| 18 | + }, |
| 19 | +}); |
| 20 | +type InspectorProtocolCommandBase = Record< |
| 21 | + keyof typeof inspector.methods, |
| 22 | + { description: string } & typeof commandBase |
| 23 | +>; |
| 24 | +const inspectorProtocolEntries = ( |
| 25 | + Object.keys(inspector.methods) as (keyof typeof inspector.methods)[] |
| 26 | +).reduce((base, current) => { |
| 27 | + base[current] = { |
| 28 | + description: `/${current} API`, |
| 29 | + ...commandBase, |
| 30 | + }; |
| 31 | + return base satisfies InspectorProtocolCommandBase; |
| 32 | +}, {} as InspectorProtocolCommandBase); |
| 33 | + |
| 34 | +const protocolCommands = commands(inspectorProtocolEntries); |
| 35 | + |
| 36 | +export type ProtocolCommandConfig = ParsedConfig<typeof protocolCommands>; |
| 37 | + |
| 38 | +const runBase = object({ |
| 39 | + // TODO this throws an error if we have a remainder of more than one arg, |
| 40 | + // which is a problem for the `run` command since we want to support passing |
| 41 | + // through args to the program being run. |
| 42 | + // entrypoint: { |
| 43 | + // description: "entrypoint file", |
| 44 | + // ...field(type("string"), cli.argument()), |
| 45 | + // }, |
| 46 | + inspectRecord: { |
| 47 | + description: "write inspector recording to the given file", |
| 48 | + ...field(type("string | undefined"), field.default(undefined)), |
| 49 | + }, |
| 50 | + inspectRuntime: { |
| 51 | + description: |
| 52 | + "which JavaScript runtime to launch (node, deno, bun).\n" + |
| 53 | + "If omitted we infer from the executable that invoked the CLI", |
| 54 | + ...field(type("'node'|'deno'|'bun'"), field.default("node")), |
| 55 | + }, |
| 56 | + inspectPause: { |
| 57 | + description: "start program paused until resumed by inspector", |
| 58 | + ...field(type("boolean"), field.default(false)), |
| 59 | + }, |
| 60 | + inspectPort: { |
| 61 | + description: "port number to give to the inspector loader", |
| 62 | + ...field(type("number"), field.default(41000)), |
| 63 | + }, |
| 64 | + inspectPackage: { |
| 65 | + description: "package spec to preload/import/require (defaults to @effectionx/inspector)", |
| 66 | + ...field(type("string"), field.default("@effectionx/inspector")), |
| 67 | + }, |
| 68 | + import: { |
| 69 | + description: "tracking loader passed in from the user", |
| 70 | + ...field(type("string[]"), field.array(), field.default([])), |
| 71 | + }, |
| 72 | + preload: { |
| 73 | + description: "tracking loader passed in from the user", |
| 74 | + ...field(type("string[]"), field.array(), field.default([])), |
| 75 | + }, |
| 76 | + require: { |
| 77 | + description: "tracking loader passed in from the user", |
| 78 | + aliases: ["-r"], |
| 79 | + ...field(type("string[]"), field.array(), field.default([])), |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +export const config = program({ |
| 84 | + name: "inspector", |
| 85 | + version: packageJSON.version, |
| 86 | + config: commands( |
| 87 | + { |
| 88 | + help, |
| 89 | + ui: { |
| 90 | + description: "start up the inspector UI", |
| 91 | + ...object({ |
| 92 | + inspectPort: { |
| 93 | + description: "port number to give to the inspector loader", |
| 94 | + ...field(type("number"), field.default(41000)), |
| 95 | + }, |
| 96 | + }), |
| 97 | + }, |
| 98 | + call: { |
| 99 | + description: "invoke a inspector protocol method directly (low level)", |
| 100 | + aliases: ["c"], |
| 101 | + ...protocolCommands, |
| 102 | + }, |
| 103 | + run: { |
| 104 | + description: "inspect a CLI program", |
| 105 | + ...runBase, |
| 106 | + }, |
| 107 | + }, |
| 108 | + { default: "run" }, |
| 109 | + ), |
| 110 | +}); |
| 111 | + |
| 112 | +export type RunConfig = ParsedConfig<typeof runBase>; |
0 commit comments