Skip to content

Extract drafting-command boilerplate into three shared helpers#57

Merged
4nkur merged 1 commit intomainfrom
dedup/drafting-context
Apr 29, 2026
Merged

Extract drafting-command boilerplate into three shared helpers#57
4nkur merged 1 commit intomainfrom
dedup/drafting-context

Conversation

@4nkur
Copy link
Copy Markdown
Owner

@4nkur 4nkur commented Apr 29, 2026

Summary

Bundle of three audit items (P2 #10, P2 #11, P3 #12). Three near-identical patterns lived across new / tech / tasks (and a chunk of one across all eight non-init commands):

  • Scan-context branch in new/tech/tasks — the same if (isGreenfield) { readOverview } else { scan + compactScan + describeScanWarnings } block, three copies, same error strings modulo command name. Now loadScanContext({ cwd, config, log, scan, readOverview, commandName }) in src/utils/scan-context.js returning { scanForPrompt, packageMeta, overview }.
  • Overwrite-confirm dance in new/tech/tasks — same if !force && exists { TTY ? confirm-or-cancel : throw } three times with the same inline confirm({ default: false }) prompt. Now confirmOverwriteOrCancel(...) in src/utils/overwrite-guard.js returning a proceed boolean. Default prompt moved into the helper; commands no longer carry per-file confirmOverwrite definitions in their DEFAULT_PROMPTS (test injection still works through deps.prompts).
  • .draftwise/ existence guard, eight callers — same three-line block (if (!await pathExists(dir)) throw '.draftwise/ not found, run init first') at the top of explain, list, new, scaffold, scan, show, tasks, tech. Now await requireDraftwiseDir(cwd) returning the resolved path. Lives at src/utils/draftwise-dir.js. init.js is exempt — it asserts the directory does NOT exist.

Net production code: command files lose ~130 lines; helpers add ~80. Tests gain ~250 lines (12 new direct tests across the three new helper files). Single source of truth for: how Draftwise loads scan data into a prompt, how it guards a hand-edited spec, how it refuses to run pre-init.

Test plan

  • npm test — 332 passing (was 320 before this branch); 12 new direct tests across test/utils/{scan-context,overwrite-guard,draftwise-dir}.test.js
  • npm run lint — clean
  • No public CLI behavior change — same flags, same error strings, same prompts. Audit'd by reading the diff: every removed log call shows up in a helper, every removed throw shows up in a helper.
  • init.js deliberately untouched (its .draftwise/-existence check is the inverse invariant).

Three near-identical patterns lived across new / tech / tasks (and a chunk of one across eight non-init commands):

1. **Scan-context branch in new/tech/tasks.** The same `if (isGreenfield) { readOverview } else { scan + compactScan + describeScanWarnings }` block, three copies, same error strings modulo command name. Now `loadScanContext({ cwd, config, log, scan, readOverview, commandName })` returns `{ scanForPrompt, packageMeta, overview }`. Lives at `src/utils/scan-context.js`.

2. **Overwrite-confirm dance in new/tech/tasks.** Same `if !force && exists { TTY ? confirm-or-cancel : throw }` shape three times, with the same inline `confirm({ default: false })` prompt definition. Now `confirmOverwriteOrCancel({ targetPath, slug, file, force, isInteractive, log, confirmOverwrite })` returns a `proceed` boolean. The default prompt moved into `src/utils/overwrite-guard.js`; commands no longer carry per-file `confirmOverwrite` definitions in their `DEFAULT_PROMPTS` (test injection still works through `deps.prompts`).

3. **`.draftwise/` existence guard, eight callers.** Same three-line block (`const dir = join(cwd, '.draftwise'); if (!await pathExists(dir)) throw 'not found, run init first'`) at the top of `explain`, `list`, `new`, `scaffold`, `scan`, `show`, `tasks`, `tech`. Now `await requireDraftwiseDir(cwd)` returns the resolved path. Lives at `src/utils/draftwise-dir.js`. `init.js` is exempt — it asserts the directory does NOT exist and has its own bespoke error.

Net: command files lose ~130 lines, helpers add ~80, tests gain ~250. Single source of truth for "how Draftwise loads scan data into a prompt," "how Draftwise guards a hand-edited spec from clobbering," and "how Draftwise refuses to run before init."

12 new direct tests across `test/utils/{scan-context,overwrite-guard,draftwise-dir}.test.js`. Full suite at 332 passing (was 320 before this branch). Lint clean. No public CLI behavior change — same flags, same error strings, same prompts.
@4nkur 4nkur merged commit 0effef7 into main Apr 29, 2026
2 checks passed
@4nkur 4nkur deleted the dedup/drafting-context branch April 29, 2026 11:24
4nkur added a commit that referenced this pull request Apr 29, 2026
The polish + cleanup release. Half ergonomic improvements (`draftwise init` auto-detects new project vs existing codebase from the filesystem instead of asking; plain-language UX throughout init — #51), half code-quality work (five small dedup + tidy PRs that shrink the public API surface, fix two user-visible `draft` → `draftwise` typos in error messages, and consolidate drafting-command boilerplate behind shared helpers — #53/#54/#55/#56/#57). One genuinely new surface: `draftwise skills <install|uninstall|help>` drops standalone slash-command skills into Claude Code, Cursor, and Gemini CLI's user-level skill dirs (#52) — same SKILL.md, per-provider frontmatter trim — independent of the Claude Code marketplace plugin.

Bumps `package.json` + `package-lock.json` from 0.2.0 → 0.2.1. Patch bump because the additive `skills` verb is non-breaking and the removed prompt aliases (`PLAN_SYSTEM`, `SPEC_SYSTEM`, `tech.SYSTEM`, `tasks.SYSTEM`) were never imported by anyone outside this repo (verified by audit). Rolls all [Unreleased] entries into [0.2.1] — 2026-04-29 — Ankur, with a fresh empty [Unreleased] block above.

After merge, ritual: `git tag -a v0.2.1 -m "v0.2.1"`, `git push origin v0.2.1`, `npm publish` (interactive — OTP + login may be needed if the npm session expired).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant