|
| 1 | +import { existsSync, readFileSync } from 'node:fs' |
| 2 | +import { resolve } from 'node:path' |
| 3 | +import * as p from '@clack/prompts' |
| 4 | +import { type AgentEnvironment, detectAgents } from '../init/detect-agents.js' |
| 5 | +import { parsePlanSummary, renderPlanSummary } from '../init/lib/parse-plan.js' |
| 6 | +import { readContextFile } from '../init/lib/read-context.js' |
| 7 | +import { PLAN_REL_PATH } from '../init/lib/setup-prompt.js' |
| 8 | +import { |
| 9 | + CONTEXT_REL_PATH, |
| 10 | + type ContextFile, |
| 11 | +} from '../init/lib/write-context.js' |
| 12 | +import { CancelledError, type InitState } from '../init/types.js' |
| 13 | +import { detectPackageManager, runnerCommand } from '../init/utils.js' |
| 14 | +import { howToProceedStep } from './steps/how-to-proceed.js' |
| 15 | + |
| 16 | +function buildStateFromContext( |
| 17 | + ctx: ContextFile, |
| 18 | + agents: AgentEnvironment, |
| 19 | +): InitState { |
| 20 | + return { |
| 21 | + integration: ctx.integration, |
| 22 | + clientFilePath: ctx.encryptionClientPath, |
| 23 | + schemas: ctx.schemas, |
| 24 | + envKeys: ctx.envKeys, |
| 25 | + stackInstalled: true, |
| 26 | + cliInstalled: true, |
| 27 | + eqlInstalled: true, |
| 28 | + agents, |
| 29 | + mode: 'implement', |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Confirm before launching implementation when the user has chosen to |
| 35 | + * skip the planning checkpoint. Default-no is the security stance — |
| 36 | + * passing through this prompt by accident is the failure mode we're |
| 37 | + * guarding against. |
| 38 | + */ |
| 39 | +async function confirmContinueWithoutPlan(): Promise<void> { |
| 40 | + const confirmed = await p.confirm({ |
| 41 | + message: 'Implementation can take some time. Continue?', |
| 42 | + initialValue: false, |
| 43 | + }) |
| 44 | + if (p.isCancel(confirmed) || !confirmed) { |
| 45 | + throw new CancelledError() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * `stash impl` — execute an encryption plan. |
| 51 | + * |
| 52 | + * Always runs in implement mode. Behaviour branches on disk state and |
| 53 | + * flags: |
| 54 | + * |
| 55 | + * - **Plan exists** (TTY): parse the structured summary block, render |
| 56 | + * a confirmation panel, ask the user to proceed. Default-yes. |
| 57 | + * - **Plan exists** (non-TTY): proceed without confirmation. |
| 58 | + * - **No plan, `--continue-without-plan`**: confirm once, then implement. |
| 59 | + * - **No plan, TTY**: present a `p.select` — draft a plan first |
| 60 | + * (delegates to `planCommand`) or continue without one (confirms |
| 61 | + * once, then implements). |
| 62 | + * - **No plan, non-TTY**: error out with a clear next-action; CI must |
| 63 | + * pass `--continue-without-plan` or run `stash plan` first. |
| 64 | + */ |
| 65 | +export async function implCommand(flags: Record<string, boolean>) { |
| 66 | + const cwd = process.cwd() |
| 67 | + const pm = detectPackageManager() |
| 68 | + const cli = runnerCommand(pm, 'stash') |
| 69 | + |
| 70 | + const ctx = readContextFile(cwd) |
| 71 | + if (!ctx) { |
| 72 | + p.log.error( |
| 73 | + `No CipherStash context found at \`${CONTEXT_REL_PATH}\`. Run \`${cli} init\` first.`, |
| 74 | + ) |
| 75 | + process.exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + p.intro('CipherStash Implementation') |
| 79 | + |
| 80 | + const planPath = resolve(cwd, PLAN_REL_PATH) |
| 81 | + const planExists = existsSync(planPath) |
| 82 | + const continueWithoutPlan = flags['continue-without-plan'] === true |
| 83 | + const isTTY = process.stdout.isTTY |
| 84 | + |
| 85 | + try { |
| 86 | + if (planExists) { |
| 87 | + // Plan-summary checkpoint: the last save point before launching the |
| 88 | + // (potentially hour-long) implementation phase. |
| 89 | + if (isTTY) { |
| 90 | + const summary = parsePlanSummary(readFileSync(planPath, 'utf-8')) |
| 91 | + if (summary) { |
| 92 | + p.note(renderPlanSummary(summary), 'Plan summary') |
| 93 | + } else { |
| 94 | + p.note( |
| 95 | + `Plan at \`${PLAN_REL_PATH}\` doesn't include a machine-readable summary. Open it in your editor before proceeding.`, |
| 96 | + 'Plan ready', |
| 97 | + ) |
| 98 | + } |
| 99 | + const proceed = await p.confirm({ |
| 100 | + message: 'Proceed with implementation against this plan?', |
| 101 | + initialValue: true, |
| 102 | + }) |
| 103 | + if (p.isCancel(proceed) || !proceed) { |
| 104 | + throw new CancelledError() |
| 105 | + } |
| 106 | + } else { |
| 107 | + p.log.info( |
| 108 | + `Plan at \`${PLAN_REL_PATH}\` — agent will execute it as the source of truth.`, |
| 109 | + ) |
| 110 | + } |
| 111 | + } else { |
| 112 | + // No plan on disk. Branch on flag / TTY / interactive. |
| 113 | + if (continueWithoutPlan) { |
| 114 | + await confirmContinueWithoutPlan() |
| 115 | + } else if (!isTTY) { |
| 116 | + p.log.error( |
| 117 | + `No plan at \`${PLAN_REL_PATH}\`. Run \`${cli} plan\` first, or pass --continue-without-plan to skip planning.`, |
| 118 | + ) |
| 119 | + process.exit(1) |
| 120 | + } else { |
| 121 | + const choice = await p.select<'plan' | 'continue'>({ |
| 122 | + message: 'No plan found. What would you like to do?', |
| 123 | + options: [ |
| 124 | + { |
| 125 | + value: 'plan', |
| 126 | + label: 'Draft a plan first (recommended)', |
| 127 | + hint: `runs \`${cli} plan\` — usually 1–3 min`, |
| 128 | + }, |
| 129 | + { |
| 130 | + value: 'continue', |
| 131 | + label: 'Continue without a plan', |
| 132 | + hint: 'skip the planning checkpoint', |
| 133 | + }, |
| 134 | + ], |
| 135 | + initialValue: 'plan', |
| 136 | + }) |
| 137 | + if (p.isCancel(choice)) throw new CancelledError() |
| 138 | + |
| 139 | + if (choice === 'plan') { |
| 140 | + // Lazy import avoids a circular module load between plan ↔ impl. |
| 141 | + const { planCommand } = await import('../plan/index.js') |
| 142 | + // Close the current intro frame before plan opens its own. |
| 143 | + p.outro('Handing off to `stash plan`.') |
| 144 | + await planCommand() |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + await confirmContinueWithoutPlan() |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + const agents = detectAgents(cwd, process.env) |
| 153 | + const state = buildStateFromContext(ctx, agents) |
| 154 | + |
| 155 | + await howToProceedStep.run(state) |
| 156 | + |
| 157 | + p.outro( |
| 158 | + `Implementation handoff complete. Run \`${cli} db status\` to verify state.`, |
| 159 | + ) |
| 160 | + } catch (err) { |
| 161 | + if (err instanceof CancelledError) { |
| 162 | + p.cancel('Cancelled.') |
| 163 | + process.exit(0) |
| 164 | + } |
| 165 | + throw err |
| 166 | + } |
| 167 | +} |
0 commit comments