{"version":3,"file":"repl.mjs","names":[],"sources":["../../src/extension/repl.ts"],"sourcesContent":["import { defineInterceptor } from '../core/interceptors.ts';\nimport { parseCliInputToParts } from '../core/parse.ts';\nimport { withDrain } from '../core/results.ts';\nimport type {\n  AnyPadroneBuilder,\n  AnyPadroneCommand,\n  CommandTypesBase,\n  InterceptorStartContext,\n  PadroneCommand,\n  PadroneReplPreferences,\n} from '../types/index.ts';\nimport type { PadroneSchema } from '../types/schema.ts';\nimport type { WithCommand } from '../util/type-utils.ts';\nimport { passthroughSchema } from './utils.ts';\n\n// ── Types ────────────────────────────────────────────────────────────────\n\ntype ReplArgs = { scope?: string };\n\ntype ReplCommand = PadroneCommand<'repl', '', PadroneSchema<ReplArgs>, void, [], [], true>;\n\nexport type WithRepl<T> = WithCommand<T, 'repl', ReplCommand>;\n\n// ── Extension ────────────────────────────────────────────────────────────\n\n/**\n * Extension that adds REPL support:\n * - `repl` command that starts an interactive REPL\n * - `--repl` flag that starts the REPL from any invocation\n *\n * Usage:\n * ```ts\n * createPadrone('my-cli').extend(padroneRepl())\n * ```\n */\nexport function padroneRepl(\n  defaults?: PadroneReplPreferences & { disabled?: boolean },\n): <T extends CommandTypesBase>(builder: T) => WithRepl<T> {\n  const disabled = defaults?.disabled;\n  return ((builder: AnyPadroneBuilder) =>\n    builder\n      .command('repl', (c) =>\n        c\n          .configure({ description: 'Start an interactive REPL', hidden: true })\n          .arguments(passthroughSchema({ scope: 'string' }), { positional: ['scope'] })\n          .async()\n          .action(async (args, ctx) => {\n            const prefs: PadroneReplPreferences = { ...defaults, scope: args.scope ?? defaults?.scope };\n            const repl = ctx.program.repl(prefs);\n            const { value } = await repl.drain();\n            return value;\n          }),\n      )\n      .intercept(createReplInterceptor(defaults, disabled))) as any;\n}\n\nfunction createReplInterceptor(defaults?: PadroneReplPreferences, disabled?: boolean) {\n  return defineInterceptor({ id: 'padrone:repl', name: 'padrone:repl', order: -1000, disabled }, () => ({\n    start(ctx: InterceptorStartContext, next: () => unknown) {\n      const replInfo = checkReplFlag(ctx.input, ctx.command);\n      if (!replInfo) return next();\n\n      const program = ctx.program;\n      if (!program?.repl) return next();\n\n      const prefs: PadroneReplPreferences = { ...defaults, scope: replInfo.scope ?? defaults?.scope };\n\n      // Return a Promise so the pipeline awaits the REPL result\n      return program\n        .repl(prefs)\n        .drain()\n        .then((r: any) => withDrain({ command: ctx.command, args: undefined, result: r.value }));\n    },\n  }));\n}\n\n/** Check for --repl flag in input. */\nfunction checkReplFlag(input: string | undefined, rootCommand: AnyPadroneCommand): { scope?: string } | null {\n  if (!input) return null;\n\n  const parts = parseCliInputToParts(input);\n  const terms = parts.filter((p) => p.type === 'term').map((p) => p.value);\n  const args = parts.filter((p) => p.type === 'named');\n  const keyIs = (key: string[], name: string) => key.length === 1 && key[0] === name;\n\n  const hasReplFlag = args.some((p) => p.type === 'named' && keyIs(p.key, 'repl'));\n  if (!hasReplFlag) return null;\n\n  const normalizedTerms = [...terms];\n  if (normalizedTerms[0] === rootCommand.name) normalizedTerms.shift();\n\n  const scope = normalizedTerms.length > 0 ? normalizedTerms.join(' ') : undefined;\n  return { scope };\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmCA,SAAgB,YACd,UACyD;CACzD,MAAM,WAAW,UAAU;CAC3B,SAAS,YACP,QACG,QAAQ,SAAS,MAChB,EACG,UAAU;EAAE,aAAa;EAA6B,QAAQ;CAAK,CAAC,CAAC,CACrE,UAAU,kBAAkB,EAAE,OAAO,SAAS,CAAC,GAAG,EAAE,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC,CAC5E,MAAM,CAAC,CACP,OAAO,OAAO,MAAM,QAAQ;EAC3B,MAAM,QAAgC;GAAE,GAAG;GAAU,OAAO,KAAK,SAAS,UAAU;EAAM;EAE1F,MAAM,EAAE,UAAU,MADL,IAAI,QAAQ,KAAK,KACH,CAAC,CAAC,MAAM;EACnC,OAAO;CACT,CAAC,CACL,CAAC,CACA,UAAU,sBAAsB,UAAU,QAAQ,CAAC;A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