@friedbotstudio/create-baseline 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +222 -0
  3. package/bin/cli.js +247 -0
  4. package/obj/template/.claude/agents/swarm-worker.md +52 -0
  5. package/obj/template/.claude/bin/LICENSE +201 -0
  6. package/obj/template/.claude/bin/NOTICE +48 -0
  7. package/obj/template/.claude/commands/approve-spec.md +29 -0
  8. package/obj/template/.claude/commands/approve-swarm.md +27 -0
  9. package/obj/template/.claude/commands/grant-commit.md +19 -0
  10. package/obj/template/.claude/commands/init-project.md +191 -0
  11. package/obj/template/.claude/hooks/artifact_template_guard.sh +141 -0
  12. package/obj/template/.claude/hooks/consent_gate_grant.sh +89 -0
  13. package/obj/template/.claude/hooks/destructive_cmd_guard.sh +42 -0
  14. package/obj/template/.claude/hooks/env_guard.sh +36 -0
  15. package/obj/template/.claude/hooks/git_commit_guard.sh +93 -0
  16. package/obj/template/.claude/hooks/harness_continuation.sh +121 -0
  17. package/obj/template/.claude/hooks/lib/__pycache__/resume_writer.cpython-314.pyc +0 -0
  18. package/obj/template/.claude/hooks/lib/common.sh +328 -0
  19. package/obj/template/.claude/hooks/lib/resume_writer.py +341 -0
  20. package/obj/template/.claude/hooks/lint_runner.sh +55 -0
  21. package/obj/template/.claude/hooks/memory_pre_compact.sh +36 -0
  22. package/obj/template/.claude/hooks/memory_session_start.sh +244 -0
  23. package/obj/template/.claude/hooks/memory_stop.sh +173 -0
  24. package/obj/template/.claude/hooks/plantuml_syntax_guard.sh +161 -0
  25. package/obj/template/.claude/hooks/process_lifecycle_guard.sh +89 -0
  26. package/obj/template/.claude/hooks/setup_guard.sh +50 -0
  27. package/obj/template/.claude/hooks/spec_approval_guard.sh +81 -0
  28. package/obj/template/.claude/hooks/spec_design_calls_guard.sh +183 -0
  29. package/obj/template/.claude/hooks/spec_diagram_presence_guard.sh +141 -0
  30. package/obj/template/.claude/hooks/swarm_approval_guard.sh +39 -0
  31. package/obj/template/.claude/hooks/swarm_boundary_guard.sh +136 -0
  32. package/obj/template/.claude/hooks/tdd_order_guard.sh +176 -0
  33. package/obj/template/.claude/hooks/test_runner.sh +75 -0
  34. package/obj/template/.claude/hooks/tests/fixtures/ac008_byte_equal_reference.txt +12 -0
  35. package/obj/template/.claude/hooks/tests/memory_session_start_test.sh +285 -0
  36. package/obj/template/.claude/hooks/track_guard.sh +127 -0
  37. package/obj/template/.claude/hooks/verify_pass_guard.sh +88 -0
  38. package/obj/template/.claude/memory/README.md +108 -0
  39. package/obj/template/.claude/memory/_pending.md +15 -0
  40. package/obj/template/.claude/memory/_resume.md +12 -0
  41. package/obj/template/.claude/memory/conventions.md +26 -0
  42. package/obj/template/.claude/memory/decisions.md +29 -0
  43. package/obj/template/.claude/memory/landmarks.md +26 -0
  44. package/obj/template/.claude/memory/landmines.md +27 -0
  45. package/obj/template/.claude/memory/libraries.md +27 -0
  46. package/obj/template/.claude/memory/pending-questions.md +28 -0
  47. package/obj/template/.claude/project.json +221 -0
  48. package/obj/template/.claude/settings.json +110 -0
  49. package/obj/template/.claude/skills/archive/SKILL.md +48 -0
  50. package/obj/template/.claude/skills/archive/archive.sh +145 -0
  51. package/obj/template/.claude/skills/audit-baseline/SKILL.md +80 -0
  52. package/obj/template/.claude/skills/audit-baseline/audit.sh +919 -0
  53. package/obj/template/.claude/skills/brd/SKILL.md +44 -0
  54. package/obj/template/.claude/skills/brd/template.md +83 -0
  55. package/obj/template/.claude/skills/chore/SKILL.md +99 -0
  56. package/obj/template/.claude/skills/claude-automation-recommender/LICENSE +202 -0
  57. package/obj/template/.claude/skills/claude-automation-recommender/NOTICE +69 -0
  58. package/obj/template/.claude/skills/claude-automation-recommender/SKILL.md +358 -0
  59. package/obj/template/.claude/skills/claude-automation-recommender/references/hooks-patterns.md +226 -0
  60. package/obj/template/.claude/skills/claude-automation-recommender/references/mcp-servers.md +263 -0
  61. package/obj/template/.claude/skills/claude-automation-recommender/references/plugins-reference.md +98 -0
  62. package/obj/template/.claude/skills/claude-automation-recommender/references/skills-reference.md +408 -0
  63. package/obj/template/.claude/skills/claude-automation-recommender/references/subagent-templates.md +181 -0
  64. package/obj/template/.claude/skills/code-structure/SKILL.md +204 -0
  65. package/obj/template/.claude/skills/commit/SKILL.md +21 -0
  66. package/obj/template/.claude/skills/copywriting/SKILL.md +252 -0
  67. package/obj/template/.claude/skills/copywriting/evals/evals.json +111 -0
  68. package/obj/template/.claude/skills/copywriting/references/ai-writing-detection.md +200 -0
  69. package/obj/template/.claude/skills/copywriting/references/copy-frameworks.md +344 -0
  70. package/obj/template/.claude/skills/copywriting/references/natural-transitions.md +272 -0
  71. package/obj/template/.claude/skills/design-ui/SKILL.md +175 -0
  72. package/obj/template/.claude/skills/design-ui/references/design-vs-development.md +89 -0
  73. package/obj/template/.claude/skills/design-ui/references/intent-table.md +64 -0
  74. package/obj/template/.claude/skills/design-ui/references/orchestration.md +121 -0
  75. package/obj/template/.claude/skills/design-ui/references/state-machine.md +125 -0
  76. package/obj/template/.claude/skills/document/SKILL.md +66 -0
  77. package/obj/template/.claude/skills/documentation/SKILL.md +50 -0
  78. package/obj/template/.claude/skills/harness/SKILL.md +169 -0
  79. package/obj/template/.claude/skills/humanizer/SKILL.md +489 -0
  80. package/obj/template/.claude/skills/humanizer/references/ai-writing-detection.md +208 -0
  81. package/obj/template/.claude/skills/impeccable/PROJECT_NOTES.md +22 -0
  82. package/obj/template/.claude/skills/impeccable/SKILL.md +153 -0
  83. package/obj/template/.claude/skills/impeccable/agents/openai.yaml +4 -0
  84. package/obj/template/.claude/skills/impeccable/reference/adapt.md +190 -0
  85. package/obj/template/.claude/skills/impeccable/reference/animate.md +173 -0
  86. package/obj/template/.claude/skills/impeccable/reference/audit.md +134 -0
  87. package/obj/template/.claude/skills/impeccable/reference/bolder.md +113 -0
  88. package/obj/template/.claude/skills/impeccable/reference/brand.md +104 -0
  89. package/obj/template/.claude/skills/impeccable/reference/clarify.md +174 -0
  90. package/obj/template/.claude/skills/impeccable/reference/cognitive-load.md +106 -0
  91. package/obj/template/.claude/skills/impeccable/reference/color-and-contrast.md +105 -0
  92. package/obj/template/.claude/skills/impeccable/reference/colorize.md +154 -0
  93. package/obj/template/.claude/skills/impeccable/reference/craft.md +138 -0
  94. package/obj/template/.claude/skills/impeccable/reference/critique.md +213 -0
  95. package/obj/template/.claude/skills/impeccable/reference/delight.md +302 -0
  96. package/obj/template/.claude/skills/impeccable/reference/distill.md +111 -0
  97. package/obj/template/.claude/skills/impeccable/reference/document.md +427 -0
  98. package/obj/template/.claude/skills/impeccable/reference/extract.md +70 -0
  99. package/obj/template/.claude/skills/impeccable/reference/harden.md +347 -0
  100. package/obj/template/.claude/skills/impeccable/reference/heuristics-scoring.md +234 -0
  101. package/obj/template/.claude/skills/impeccable/reference/interaction-design.md +195 -0
  102. package/obj/template/.claude/skills/impeccable/reference/layout.md +141 -0
  103. package/obj/template/.claude/skills/impeccable/reference/live.md +513 -0
  104. package/obj/template/.claude/skills/impeccable/reference/motion-design.md +99 -0
  105. package/obj/template/.claude/skills/impeccable/reference/onboard.md +234 -0
  106. package/obj/template/.claude/skills/impeccable/reference/optimize.md +258 -0
  107. package/obj/template/.claude/skills/impeccable/reference/overdrive.md +130 -0
  108. package/obj/template/.claude/skills/impeccable/reference/personas.md +178 -0
  109. package/obj/template/.claude/skills/impeccable/reference/polish.md +232 -0
  110. package/obj/template/.claude/skills/impeccable/reference/product.md +62 -0
  111. package/obj/template/.claude/skills/impeccable/reference/quieter.md +99 -0
  112. package/obj/template/.claude/skills/impeccable/reference/responsive-design.md +114 -0
  113. package/obj/template/.claude/skills/impeccable/reference/shape.md +136 -0
  114. package/obj/template/.claude/skills/impeccable/reference/spatial-design.md +100 -0
  115. package/obj/template/.claude/skills/impeccable/reference/teach.md +137 -0
  116. package/obj/template/.claude/skills/impeccable/reference/typeset.md +124 -0
  117. package/obj/template/.claude/skills/impeccable/reference/typography.md +159 -0
  118. package/obj/template/.claude/skills/impeccable/reference/ux-writing.md +107 -0
  119. package/obj/template/.claude/skills/impeccable/scripts/cleanup-deprecated.mjs +284 -0
  120. package/obj/template/.claude/skills/impeccable/scripts/command-metadata.json +94 -0
  121. package/obj/template/.claude/skills/impeccable/scripts/design-parser.mjs +820 -0
  122. package/obj/template/.claude/skills/impeccable/scripts/detect-csp.mjs +198 -0
  123. package/obj/template/.claude/skills/impeccable/scripts/is-generated.mjs +69 -0
  124. package/obj/template/.claude/skills/impeccable/scripts/live-accept.mjs +465 -0
  125. package/obj/template/.claude/skills/impeccable/scripts/live-browser.js +4684 -0
  126. package/obj/template/.claude/skills/impeccable/scripts/live-inject.mjs +436 -0
  127. package/obj/template/.claude/skills/impeccable/scripts/live-poll.mjs +187 -0
  128. package/obj/template/.claude/skills/impeccable/scripts/live-server.mjs +679 -0
  129. package/obj/template/.claude/skills/impeccable/scripts/live-wrap.mjs +395 -0
  130. package/obj/template/.claude/skills/impeccable/scripts/live.mjs +247 -0
  131. package/obj/template/.claude/skills/impeccable/scripts/load-context.mjs +93 -0
  132. package/obj/template/.claude/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
  133. package/obj/template/.claude/skills/impeccable/scripts/pin.mjs +214 -0
  134. package/obj/template/.claude/skills/implement/SKILL.md +83 -0
  135. package/obj/template/.claude/skills/intake/SKILL.md +46 -0
  136. package/obj/template/.claude/skills/intake/template.md +61 -0
  137. package/obj/template/.claude/skills/integrate/SKILL.md +62 -0
  138. package/obj/template/.claude/skills/memory-flush/SKILL.md +172 -0
  139. package/obj/template/.claude/skills/memory-flush/sweep.py +286 -0
  140. package/obj/template/.claude/skills/memory-flush/tests/run.sh +327 -0
  141. package/obj/template/.claude/skills/prose/SKILL.md +119 -0
  142. package/obj/template/.claude/skills/rca/SKILL.md +42 -0
  143. package/obj/template/.claude/skills/rca/template.md +83 -0
  144. package/obj/template/.claude/skills/research/SKILL.md +75 -0
  145. package/obj/template/.claude/skills/scenario/SKILL.md +64 -0
  146. package/obj/template/.claude/skills/scout/SKILL.md +72 -0
  147. package/obj/template/.claude/skills/security/SKILL.md +75 -0
  148. package/obj/template/.claude/skills/simplify/SKILL.md +67 -0
  149. package/obj/template/.claude/skills/spec/SKILL.md +69 -0
  150. package/obj/template/.claude/skills/spec/template.md +274 -0
  151. package/obj/template/.claude/skills/spec-diagram-review/SKILL.md +81 -0
  152. package/obj/template/.claude/skills/spec-lint/SKILL.md +55 -0
  153. package/obj/template/.claude/skills/spec-lint/lint.sh +218 -0
  154. package/obj/template/.claude/skills/spec-render/SKILL.md +45 -0
  155. package/obj/template/.claude/skills/spec-render/render.sh +109 -0
  156. package/obj/template/.claude/skills/spec-traceability-review/SKILL.md +72 -0
  157. package/obj/template/.claude/skills/swarm-dispatch/SKILL.md +212 -0
  158. package/obj/template/.claude/skills/swarm-dispatch/swarm_merge.sh +154 -0
  159. package/obj/template/.claude/skills/swarm-plan/SKILL.md +90 -0
  160. package/obj/template/.claude/skills/swarm-plan/validate.sh +181 -0
  161. package/obj/template/.claude/skills/tdd/SKILL.md +100 -0
  162. package/obj/template/.claude/skills/technical-tutorials/SKILL.md +569 -0
  163. package/obj/template/.claude/skills/technical-tutorials/references/audience-context-README.md +53 -0
  164. package/obj/template/.claude/skills/technical-tutorials/references/audience-context.md +246 -0
  165. package/obj/template/.claude/skills/technical-tutorials/references/audience-example.md +175 -0
  166. package/obj/template/.claude/skills/technical-tutorials/references/audience-template.md +152 -0
  167. package/obj/template/.claude/skills/triage/SKILL.md +55 -0
  168. package/obj/template/.claude/skills/verify/SKILL.md +74 -0
  169. package/obj/template/.mcp.json +24 -0
  170. package/obj/template/CLAUDE.md +327 -0
  171. package/obj/template/docs/init/seed.md +585 -0
  172. package/obj/template/manifest.json +214 -0
  173. package/package.json +48 -0
  174. package/src/.mcp.template.json +24 -0
  175. package/src/.npmrc.template +2 -0
  176. package/src/CLAUDE.template.md +327 -0
  177. package/src/agents/swarm-worker.template.md +51 -0
  178. package/src/cli/conflict.js +31 -0
  179. package/src/cli/doctor.js +152 -0
  180. package/src/cli/install.js +93 -0
  181. package/src/cli/io.js +27 -0
  182. package/src/cli/manifest.js +38 -0
  183. package/src/cli/mcp.js +54 -0
  184. package/src/cli/merge.js +107 -0
  185. package/src/cli/plantuml.js +121 -0
  186. package/src/cli/util.js +10 -0
  187. package/src/memory/_pending.template.md +15 -0
  188. package/src/memory/_resume.template.md +12 -0
  189. package/src/memory/conventions.template.md +26 -0
  190. package/src/memory/decisions.template.md +29 -0
  191. package/src/memory/landmarks.template.md +26 -0
  192. package/src/memory/landmines.template.md +27 -0
  193. package/src/memory/libraries.template.md +27 -0
  194. package/src/memory/pending-questions.template.md +28 -0
  195. package/src/project.template.json +221 -0
  196. package/src/seed.template.md +585 -0
  197. package/src/settings.template.json +110 -0
@@ -0,0 +1,465 @@
1
+ /**
2
+ * CLI helper: deterministic accept/discard of variant sessions.
3
+ *
4
+ * Usage:
5
+ * node live-accept.mjs --id SESSION_ID --discard
6
+ * node live-accept.mjs --id SESSION_ID --variant N
7
+ *
8
+ * For discard: removes the entire variant wrapper and restores the original.
9
+ * For accept: replaces the wrapper with the chosen variant's content. If the
10
+ * session had a colocated <style> block, it's preserved with carbonize markers
11
+ * for a background agent to integrate into the project's CSS.
12
+ *
13
+ * Output: JSON to stdout.
14
+ */
15
+
16
+ import fs from 'node:fs';
17
+ import path from 'node:path';
18
+ import { isGeneratedFile } from './is-generated.mjs';
19
+
20
+ const EXTENSIONS = ['.html', '.jsx', '.tsx', '.vue', '.svelte', '.astro'];
21
+
22
+ // ---------------------------------------------------------------------------
23
+ // CLI
24
+ // ---------------------------------------------------------------------------
25
+
26
+ export async function acceptCli() {
27
+ const args = process.argv.slice(2);
28
+
29
+ if (args.includes('--help') || args.includes('-h')) {
30
+ console.log(`Usage: node live-accept.mjs [options]
31
+
32
+ Deterministic accept/discard for live variant sessions.
33
+
34
+ Modes:
35
+ --discard Remove variants, restore original
36
+ --variant N Accept variant N, discard the rest
37
+
38
+ Required:
39
+ --id SESSION_ID Session ID of the variant wrapper
40
+
41
+ Output (JSON):
42
+ { handled, file, carbonize }`);
43
+ process.exit(0);
44
+ }
45
+
46
+ const id = argVal(args, '--id');
47
+ const variantNum = argVal(args, '--variant');
48
+ const paramValuesRaw = argVal(args, '--param-values');
49
+ const isDiscard = args.includes('--discard');
50
+
51
+ if (!id) { console.error('Missing --id'); process.exit(1); }
52
+ if (!isDiscard && !variantNum) { console.error('Need --discard or --variant N'); process.exit(1); }
53
+
54
+ let paramValues = null;
55
+ if (paramValuesRaw) {
56
+ try { paramValues = JSON.parse(paramValuesRaw); }
57
+ catch { paramValues = null; } // malformed blob: skip the comment rather than failing the accept
58
+ }
59
+
60
+ // Find the file containing this session's markers
61
+ const found = findSessionFile(id, process.cwd());
62
+ if (!found) {
63
+ console.log(JSON.stringify({ handled: false, error: 'Session markers not found for id: ' + id }));
64
+ process.exit(0);
65
+ }
66
+
67
+ const { file: targetFile, content, lines } = found;
68
+ const relFile = path.relative(process.cwd(), targetFile);
69
+
70
+ // Bail if the session lives in a generated file. The agent manually wrote
71
+ // the wrapper there for preview, and is responsible for writing the
72
+ // accepted variant to true source (or cleaning up on discard). See
73
+ // "Handle fallback" in live.md.
74
+ if (isGeneratedFile(targetFile, { cwd: process.cwd() })) {
75
+ console.log(JSON.stringify({
76
+ handled: false,
77
+ mode: 'fallback',
78
+ file: relFile,
79
+ hint: 'Session is in a generated file. Persist the accepted variant in source; do not rely on this script.',
80
+ }));
81
+ process.exit(0);
82
+ }
83
+
84
+ if (isDiscard) {
85
+ const result = handleDiscard(id, lines, targetFile);
86
+ console.log(JSON.stringify({ handled: true, file: relFile, carbonize: false, ...result }));
87
+ } else {
88
+ const result = handleAccept(id, variantNum, lines, targetFile, paramValues);
89
+ // Single-line attention-grabber when cleanup is required. The full
90
+ // five-step checklist lives in reference/live.md (loaded once per
91
+ // session); repeating it per-event would waste tokens.
92
+ if (result.carbonize) {
93
+ result.todo = 'REQUIRED before next poll: carbonize cleanup in ' + relFile + '. See reference/live.md "Required after accept".';
94
+ }
95
+ console.log(JSON.stringify({ handled: true, file: relFile, ...result }));
96
+ }
97
+ }
98
+
99
+ // ---------------------------------------------------------------------------
100
+ // Discard
101
+ // ---------------------------------------------------------------------------
102
+
103
+ function handleDiscard(id, lines, targetFile) {
104
+ const block = findMarkerBlock(id, lines);
105
+ if (!block) return { handled: false, error: 'Markers not found' };
106
+
107
+ const original = extractOriginal(lines, block);
108
+ const indent = lines[block.start].match(/^(\s*)/)[1];
109
+
110
+ // De-indent the original content back to the marker's indentation level
111
+ const restored = deindentContent(original, indent);
112
+
113
+ const newLines = [
114
+ ...lines.slice(0, block.start),
115
+ ...restored,
116
+ ...lines.slice(block.end + 1),
117
+ ];
118
+ fs.writeFileSync(targetFile, newLines.join('\n'), 'utf-8');
119
+ return {};
120
+ }
121
+
122
+ // ---------------------------------------------------------------------------
123
+ // Accept
124
+ // ---------------------------------------------------------------------------
125
+
126
+ function handleAccept(id, variantNum, lines, targetFile, paramValues) {
127
+ const block = findMarkerBlock(id, lines);
128
+ if (!block) return { handled: false, error: 'Markers not found' };
129
+
130
+ const indent = lines[block.start].match(/^(\s*)/)[1];
131
+ const commentSyntax = detectCommentSyntax(targetFile);
132
+
133
+ // Extract the chosen variant's inner content
134
+ const variantContent = extractVariant(lines, block, variantNum);
135
+ if (!variantContent) return { handled: false, error: 'Variant ' + variantNum + ' not found' };
136
+
137
+ // Extract CSS block if present
138
+ const cssContent = extractCss(lines, block, id);
139
+
140
+ // Check if carbonizing is needed:
141
+ // - CSS block exists, OR
142
+ // - variant HTML contains helper classes/attributes that need cleanup
143
+ const variantText = variantContent.join('\n');
144
+ const hasHelperAttrs = variantText.includes('data-impeccable-variant');
145
+ const needsCarbonize = !!(cssContent || hasHelperAttrs);
146
+
147
+ // Build the replacement
148
+ const restored = deindentContent(variantContent, indent);
149
+ const replacement = [];
150
+
151
+ if (cssContent) {
152
+ const isJsx = commentSyntax.open === '{/*';
153
+ replacement.push(indent + commentSyntax.open + ' impeccable-carbonize-start ' + id + ' ' + commentSyntax.close);
154
+ // JSX targets need the CSS body wrapped in a template literal so that the
155
+ // `{` and `}` in CSS rules don't get parsed as JSX expressions.
156
+ replacement.push(indent + '<style data-impeccable-css="' + id + '">' + (isJsx ? '{`' : ''));
157
+ // Re-indent CSS content to match
158
+ for (const cssLine of cssContent) {
159
+ replacement.push(indent + cssLine.trimStart());
160
+ }
161
+ replacement.push(indent + (isJsx ? '`}</style>' : '</style>'));
162
+ if (paramValues && Object.keys(paramValues).length > 0) {
163
+ // Preserve the user's knob positions for the carbonize-cleanup agent
164
+ // to bake into the final CSS when it collapses scoped rules.
165
+ replacement.push(indent + commentSyntax.open + ' impeccable-param-values ' + id + ': ' + JSON.stringify(paramValues) + ' ' + commentSyntax.close);
166
+ }
167
+ replacement.push(indent + commentSyntax.open + ' impeccable-carbonize-end ' + id + ' ' + commentSyntax.close);
168
+ }
169
+
170
+ // Keep the `@scope ([data-impeccable-variant="N"])` selectors in the
171
+ // carbonize CSS block working visually by re-wrapping the accepted content
172
+ // in a data-impeccable-variant="N" div with `display: contents` (so layout
173
+ // isn't affected). The carbonize agent strips this attribute + wrapper when
174
+ // it moves the CSS to a proper stylesheet.
175
+ //
176
+ // Style attribute syntax has to follow the host file's flavor — JSX files
177
+ // need the object form, otherwise React 19 throws "Failed to set indexed
178
+ // property [0] on CSSStyleDeclaration" while parsing the string char-by-char.
179
+ if (cssContent) {
180
+ const isJsx = commentSyntax.open === '{/*';
181
+ const styleAttr = isJsx ? "style={{ display: 'contents' }}" : 'style="display: contents"';
182
+ replacement.push(indent + '<div data-impeccable-variant="' + variantNum + '" ' + styleAttr + '>');
183
+ replacement.push(...restored);
184
+ replacement.push(indent + '</div>');
185
+ } else {
186
+ replacement.push(...restored);
187
+ }
188
+
189
+ const newLines = [
190
+ ...lines.slice(0, block.start),
191
+ ...replacement,
192
+ ...lines.slice(block.end + 1),
193
+ ];
194
+ fs.writeFileSync(targetFile, newLines.join('\n'), 'utf-8');
195
+
196
+ return { carbonize: needsCarbonize };
197
+ }
198
+
199
+ // ---------------------------------------------------------------------------
200
+ // Parsing helpers
201
+ // ---------------------------------------------------------------------------
202
+
203
+ /**
204
+ * Find the start/end marker lines for a session.
205
+ * Returns { start, end } (0-indexed line numbers) or null.
206
+ */
207
+ function findMarkerBlock(id, lines) {
208
+ let start = -1;
209
+ let end = -1;
210
+ const startPattern = 'impeccable-variants-start ' + id;
211
+ const endPattern = 'impeccable-variants-end ' + id;
212
+
213
+ for (let i = 0; i < lines.length; i++) {
214
+ if (start === -1 && lines[i].includes(startPattern)) start = i;
215
+ if (lines[i].includes(endPattern)) { end = i; break; }
216
+ }
217
+
218
+ return (start !== -1 && end !== -1) ? { start, end } : null;
219
+ }
220
+
221
+ /**
222
+ * Join wrapper lines into a single string with `<style>` elements removed so
223
+ * marker matching and div-depth tracking aren't confused by:
224
+ * - CSS `@scope ([data-impeccable-variant="N"])` strings that look like the
225
+ * HTML marker we're searching for
226
+ * - JSX self-closing `<style ... />` (no separate `</style>` to close on)
227
+ * - Same-line `<style>…</style>` blocks
228
+ * - Multi-line `<style>\n…\n</style>` blocks
229
+ */
230
+ function stripStyleAndJoin(lines, block) {
231
+ const out = [];
232
+ let inStyle = false;
233
+ for (let i = block.start; i <= block.end; i++) {
234
+ let line = lines[i];
235
+
236
+ if (!inStyle) {
237
+ // Strip any complete <style> elements on this line (self-closed or
238
+ // same-line-closed), including their body content.
239
+ line = line
240
+ .replace(/<style\b[^>]*>[\s\S]*?<\/style\s*>/g, '')
241
+ .replace(/<style\b[^>]*\/\s*>/g, '');
242
+
243
+ // If a <style> opener remains (multi-line body starts here), strip from
244
+ // the opener to end-of-line and flip into skip mode.
245
+ const openerIdx = line.search(/<style\b/);
246
+ if (openerIdx !== -1) {
247
+ line = line.slice(0, openerIdx);
248
+ inStyle = true;
249
+ }
250
+ out.push(line);
251
+ } else {
252
+ // In multi-line style body; drop everything until we see </style>.
253
+ const closeIdx = line.search(/<\/style\s*>/);
254
+ if (closeIdx !== -1) {
255
+ inStyle = false;
256
+ out.push(line.slice(closeIdx).replace(/<\/style\s*>/, ''));
257
+ }
258
+ // else: skip line entirely
259
+ }
260
+ }
261
+ return out.join('\n');
262
+ }
263
+
264
+ /**
265
+ * Find the inner content of `<TAG ...attrMatch...>…</TAG>` inside `text`,
266
+ * handling nested same-tag elements via depth counting. `attrMatch` is a
267
+ * regex source fragment that must appear inside the opener tag.
268
+ * Returns the inner string (may be empty), or null if not found.
269
+ */
270
+ function extractInnerByAttr(text, attrMatch) {
271
+ const openerRe = new RegExp('<([A-Za-z][A-Za-z0-9]*)\\b[^>]*' + attrMatch + '[^>]*>');
272
+ const openMatch = text.match(openerRe);
273
+ if (!openMatch) return null;
274
+
275
+ const tagName = openMatch[1];
276
+ const innerStart = openMatch.index + openMatch[0].length;
277
+
278
+ // Match any opener or closer of this tag name after innerStart.
279
+ // (Does not match self-closing <TAG … />, which doesn't contribute to depth.)
280
+ const tagRe = new RegExp('<(?:/)?' + tagName + '\\b[^>]*>', 'g');
281
+ tagRe.lastIndex = innerStart;
282
+
283
+ let depth = 1;
284
+ let m;
285
+ while ((m = tagRe.exec(text))) {
286
+ const isClose = m[0].startsWith('</');
287
+ const isSelfClose = !isClose && /\/\s*>$/.test(m[0]);
288
+ if (isClose) {
289
+ depth--;
290
+ if (depth === 0) return text.slice(innerStart, m.index);
291
+ } else if (!isSelfClose) {
292
+ depth++;
293
+ }
294
+ }
295
+ return null;
296
+ }
297
+
298
+ /**
299
+ * Extract the original element content from within the variant wrapper.
300
+ * Returns an array of lines.
301
+ */
302
+ function extractOriginal(lines, block) {
303
+ const text = stripStyleAndJoin(lines, block);
304
+ const inner = extractInnerByAttr(text, 'data-impeccable-variant="original"');
305
+ if (inner === null) return [];
306
+ return inner.split('\n');
307
+ }
308
+
309
+ /**
310
+ * Extract a specific variant's inner content (stripping the wrapper div).
311
+ * Returns an array of lines, or null if not found.
312
+ */
313
+ function extractVariant(lines, block, variantNum) {
314
+ const text = stripStyleAndJoin(lines, block);
315
+ const inner = extractInnerByAttr(text, 'data-impeccable-variant="' + variantNum + '"');
316
+ if (inner === null) return null;
317
+ const result = inner.split('\n');
318
+ // Collapse a lone empty leading/trailing line (common after string splice).
319
+ while (result.length > 1 && result[0].trim() === '') result.shift();
320
+ while (result.length > 1 && result[result.length - 1].trim() === '') result.pop();
321
+ return result.length > 0 ? result : null;
322
+ }
323
+
324
+ /**
325
+ * Extract the colocated <style> block content (between the style tags).
326
+ * Returns an array of CSS lines, or null if no style block found.
327
+ *
328
+ * Handles three shapes of `<style data-impeccable-css="ID" ...>`:
329
+ * 1. Self-closing: `<style ... />` — no body; return null (nothing to carbonize).
330
+ * 2. Same-line open+close: `<style>...</style>` — return the inner content.
331
+ * 3. Multi-line: `<style>` on one line, `</style>` on a later line — return
332
+ * the lines between them.
333
+ */
334
+ function extractCss(lines, block, id) {
335
+ const styleAttr = 'data-impeccable-css="' + id + '"';
336
+ let inStyle = false;
337
+ const content = [];
338
+
339
+ for (let i = block.start; i <= block.end; i++) {
340
+ const line = lines[i];
341
+
342
+ if (!inStyle && line.includes(styleAttr)) {
343
+ // Self-closing: nothing to carbonize.
344
+ if (/<style\b[^>]*\/\s*>/.test(line)) return null;
345
+ // Same-line open + close: extract inner text.
346
+ const sameLine = line.match(/<style\b[^>]*>([\s\S]*?)<\/style\s*>/);
347
+ if (sameLine) {
348
+ const inner = sameLine[1];
349
+ return inner.length > 0 ? inner.split('\n') : null;
350
+ }
351
+ inStyle = true;
352
+ continue; // skip the <style> opening tag
353
+ }
354
+
355
+ if (inStyle) {
356
+ // Detect </style> anywhere on the line — JSX template-literal closes
357
+ // (`}</style>`) put the close mid-line, and we don't want to absorb the
358
+ // template-literal punctuation as CSS content.
359
+ const closeIdx = line.indexOf('</style>');
360
+ if (closeIdx !== -1) break;
361
+ content.push(line);
362
+ }
363
+ }
364
+
365
+ return content.length > 0 ? content : null;
366
+ }
367
+
368
+ /**
369
+ * De-indent content that was indented by live-wrap.mjs.
370
+ * The wrap script adds `indent + ' '` (4 extra spaces) to each line.
371
+ * We restore to just `indent` level.
372
+ */
373
+ function deindentContent(contentLines, baseIndent) {
374
+ // Find the minimum indentation in the content to determine how much was added
375
+ let minIndent = Infinity;
376
+ for (const line of contentLines) {
377
+ if (line.trim() === '') continue;
378
+ const leadingSpaces = line.match(/^(\s*)/)[1].length;
379
+ minIndent = Math.min(minIndent, leadingSpaces);
380
+ }
381
+ if (minIndent === Infinity) minIndent = 0;
382
+
383
+ // Strip the extra indentation and re-add base indent
384
+ return contentLines.map(line => {
385
+ if (line.trim() === '') return '';
386
+ return baseIndent + line.slice(minIndent);
387
+ });
388
+ }
389
+
390
+ function detectCommentSyntax(filePath) {
391
+ const ext = path.extname(filePath).toLowerCase();
392
+ if (ext === '.jsx' || ext === '.tsx') {
393
+ return { open: '{/*', close: '*/}' };
394
+ }
395
+ return { open: '<!--', close: '-->' };
396
+ }
397
+
398
+ // ---------------------------------------------------------------------------
399
+ // File search (find the file containing session markers)
400
+ // ---------------------------------------------------------------------------
401
+
402
+ function findSessionFile(id, cwd) {
403
+ const marker = 'impeccable-variants-start ' + id;
404
+ const searchDirs = ['src', 'app', 'pages', 'components', 'public', 'views', 'templates', '.'];
405
+ const seen = new Set();
406
+
407
+ for (const dir of searchDirs) {
408
+ const absDir = path.join(cwd, dir);
409
+ if (!fs.existsSync(absDir)) continue;
410
+ const result = searchDir(absDir, marker, seen, 0);
411
+ if (result) {
412
+ const content = fs.readFileSync(result, 'utf-8');
413
+ return { file: result, content, lines: content.split('\n') };
414
+ }
415
+ }
416
+ return null;
417
+ }
418
+
419
+ function searchDir(dir, query, seen, depth) {
420
+ if (depth > 5) return null;
421
+ let realDir;
422
+ try { realDir = fs.realpathSync(dir); } catch { return null; }
423
+ if (seen.has(realDir)) return null;
424
+ seen.add(realDir);
425
+
426
+ let entries;
427
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
428
+ catch { return null; }
429
+
430
+ for (const entry of entries) {
431
+ if (!entry.isFile()) continue;
432
+ if (!EXTENSIONS.includes(path.extname(entry.name).toLowerCase())) continue;
433
+ const filePath = path.join(dir, entry.name);
434
+ try {
435
+ const content = fs.readFileSync(filePath, 'utf-8');
436
+ if (content.includes(query)) return filePath;
437
+ } catch { /* skip */ }
438
+ }
439
+
440
+ for (const entry of entries) {
441
+ if (!entry.isDirectory()) continue;
442
+ if (['node_modules', '.git', 'dist', 'build'].includes(entry.name)) continue;
443
+ const result = searchDir(path.join(dir, entry.name), query, seen, depth + 1);
444
+ if (result) return result;
445
+ }
446
+
447
+ return null;
448
+ }
449
+
450
+ // ---------------------------------------------------------------------------
451
+ // Utilities
452
+ // ---------------------------------------------------------------------------
453
+
454
+ function argVal(args, flag) {
455
+ const idx = args.indexOf(flag);
456
+ return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : null;
457
+ }
458
+
459
+ // Auto-execute when run directly
460
+ const _running = process.argv[1];
461
+ if (_running?.endsWith('live-accept.mjs') || _running?.endsWith('live-accept.mjs/')) {
462
+ acceptCli();
463
+ }
464
+
465
+ export { findMarkerBlock, extractOriginal, extractVariant, extractCss, deindentContent, detectCommentSyntax };