@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.
- package/LICENSE +202 -0
- package/README.md +222 -0
- package/bin/cli.js +247 -0
- package/obj/template/.claude/agents/swarm-worker.md +52 -0
- package/obj/template/.claude/bin/LICENSE +201 -0
- package/obj/template/.claude/bin/NOTICE +48 -0
- package/obj/template/.claude/commands/approve-spec.md +29 -0
- package/obj/template/.claude/commands/approve-swarm.md +27 -0
- package/obj/template/.claude/commands/grant-commit.md +19 -0
- package/obj/template/.claude/commands/init-project.md +191 -0
- package/obj/template/.claude/hooks/artifact_template_guard.sh +141 -0
- package/obj/template/.claude/hooks/consent_gate_grant.sh +89 -0
- package/obj/template/.claude/hooks/destructive_cmd_guard.sh +42 -0
- package/obj/template/.claude/hooks/env_guard.sh +36 -0
- package/obj/template/.claude/hooks/git_commit_guard.sh +93 -0
- package/obj/template/.claude/hooks/harness_continuation.sh +121 -0
- package/obj/template/.claude/hooks/lib/__pycache__/resume_writer.cpython-314.pyc +0 -0
- package/obj/template/.claude/hooks/lib/common.sh +328 -0
- package/obj/template/.claude/hooks/lib/resume_writer.py +341 -0
- package/obj/template/.claude/hooks/lint_runner.sh +55 -0
- package/obj/template/.claude/hooks/memory_pre_compact.sh +36 -0
- package/obj/template/.claude/hooks/memory_session_start.sh +244 -0
- package/obj/template/.claude/hooks/memory_stop.sh +173 -0
- package/obj/template/.claude/hooks/plantuml_syntax_guard.sh +161 -0
- package/obj/template/.claude/hooks/process_lifecycle_guard.sh +89 -0
- package/obj/template/.claude/hooks/setup_guard.sh +50 -0
- package/obj/template/.claude/hooks/spec_approval_guard.sh +81 -0
- package/obj/template/.claude/hooks/spec_design_calls_guard.sh +183 -0
- package/obj/template/.claude/hooks/spec_diagram_presence_guard.sh +141 -0
- package/obj/template/.claude/hooks/swarm_approval_guard.sh +39 -0
- package/obj/template/.claude/hooks/swarm_boundary_guard.sh +136 -0
- package/obj/template/.claude/hooks/tdd_order_guard.sh +176 -0
- package/obj/template/.claude/hooks/test_runner.sh +75 -0
- package/obj/template/.claude/hooks/tests/fixtures/ac008_byte_equal_reference.txt +12 -0
- package/obj/template/.claude/hooks/tests/memory_session_start_test.sh +285 -0
- package/obj/template/.claude/hooks/track_guard.sh +127 -0
- package/obj/template/.claude/hooks/verify_pass_guard.sh +88 -0
- package/obj/template/.claude/memory/README.md +108 -0
- package/obj/template/.claude/memory/_pending.md +15 -0
- package/obj/template/.claude/memory/_resume.md +12 -0
- package/obj/template/.claude/memory/conventions.md +26 -0
- package/obj/template/.claude/memory/decisions.md +29 -0
- package/obj/template/.claude/memory/landmarks.md +26 -0
- package/obj/template/.claude/memory/landmines.md +27 -0
- package/obj/template/.claude/memory/libraries.md +27 -0
- package/obj/template/.claude/memory/pending-questions.md +28 -0
- package/obj/template/.claude/project.json +221 -0
- package/obj/template/.claude/settings.json +110 -0
- package/obj/template/.claude/skills/archive/SKILL.md +48 -0
- package/obj/template/.claude/skills/archive/archive.sh +145 -0
- package/obj/template/.claude/skills/audit-baseline/SKILL.md +80 -0
- package/obj/template/.claude/skills/audit-baseline/audit.sh +919 -0
- package/obj/template/.claude/skills/brd/SKILL.md +44 -0
- package/obj/template/.claude/skills/brd/template.md +83 -0
- package/obj/template/.claude/skills/chore/SKILL.md +99 -0
- package/obj/template/.claude/skills/claude-automation-recommender/LICENSE +202 -0
- package/obj/template/.claude/skills/claude-automation-recommender/NOTICE +69 -0
- package/obj/template/.claude/skills/claude-automation-recommender/SKILL.md +358 -0
- package/obj/template/.claude/skills/claude-automation-recommender/references/hooks-patterns.md +226 -0
- package/obj/template/.claude/skills/claude-automation-recommender/references/mcp-servers.md +263 -0
- package/obj/template/.claude/skills/claude-automation-recommender/references/plugins-reference.md +98 -0
- package/obj/template/.claude/skills/claude-automation-recommender/references/skills-reference.md +408 -0
- package/obj/template/.claude/skills/claude-automation-recommender/references/subagent-templates.md +181 -0
- package/obj/template/.claude/skills/code-structure/SKILL.md +204 -0
- package/obj/template/.claude/skills/commit/SKILL.md +21 -0
- package/obj/template/.claude/skills/copywriting/SKILL.md +252 -0
- package/obj/template/.claude/skills/copywriting/evals/evals.json +111 -0
- package/obj/template/.claude/skills/copywriting/references/ai-writing-detection.md +200 -0
- package/obj/template/.claude/skills/copywriting/references/copy-frameworks.md +344 -0
- package/obj/template/.claude/skills/copywriting/references/natural-transitions.md +272 -0
- package/obj/template/.claude/skills/design-ui/SKILL.md +175 -0
- package/obj/template/.claude/skills/design-ui/references/design-vs-development.md +89 -0
- package/obj/template/.claude/skills/design-ui/references/intent-table.md +64 -0
- package/obj/template/.claude/skills/design-ui/references/orchestration.md +121 -0
- package/obj/template/.claude/skills/design-ui/references/state-machine.md +125 -0
- package/obj/template/.claude/skills/document/SKILL.md +66 -0
- package/obj/template/.claude/skills/documentation/SKILL.md +50 -0
- package/obj/template/.claude/skills/harness/SKILL.md +169 -0
- package/obj/template/.claude/skills/humanizer/SKILL.md +489 -0
- package/obj/template/.claude/skills/humanizer/references/ai-writing-detection.md +208 -0
- package/obj/template/.claude/skills/impeccable/PROJECT_NOTES.md +22 -0
- package/obj/template/.claude/skills/impeccable/SKILL.md +153 -0
- package/obj/template/.claude/skills/impeccable/agents/openai.yaml +4 -0
- package/obj/template/.claude/skills/impeccable/reference/adapt.md +190 -0
- package/obj/template/.claude/skills/impeccable/reference/animate.md +173 -0
- package/obj/template/.claude/skills/impeccable/reference/audit.md +134 -0
- package/obj/template/.claude/skills/impeccable/reference/bolder.md +113 -0
- package/obj/template/.claude/skills/impeccable/reference/brand.md +104 -0
- package/obj/template/.claude/skills/impeccable/reference/clarify.md +174 -0
- package/obj/template/.claude/skills/impeccable/reference/cognitive-load.md +106 -0
- package/obj/template/.claude/skills/impeccable/reference/color-and-contrast.md +105 -0
- package/obj/template/.claude/skills/impeccable/reference/colorize.md +154 -0
- package/obj/template/.claude/skills/impeccable/reference/craft.md +138 -0
- package/obj/template/.claude/skills/impeccable/reference/critique.md +213 -0
- package/obj/template/.claude/skills/impeccable/reference/delight.md +302 -0
- package/obj/template/.claude/skills/impeccable/reference/distill.md +111 -0
- package/obj/template/.claude/skills/impeccable/reference/document.md +427 -0
- package/obj/template/.claude/skills/impeccable/reference/extract.md +70 -0
- package/obj/template/.claude/skills/impeccable/reference/harden.md +347 -0
- package/obj/template/.claude/skills/impeccable/reference/heuristics-scoring.md +234 -0
- package/obj/template/.claude/skills/impeccable/reference/interaction-design.md +195 -0
- package/obj/template/.claude/skills/impeccable/reference/layout.md +141 -0
- package/obj/template/.claude/skills/impeccable/reference/live.md +513 -0
- package/obj/template/.claude/skills/impeccable/reference/motion-design.md +99 -0
- package/obj/template/.claude/skills/impeccable/reference/onboard.md +234 -0
- package/obj/template/.claude/skills/impeccable/reference/optimize.md +258 -0
- package/obj/template/.claude/skills/impeccable/reference/overdrive.md +130 -0
- package/obj/template/.claude/skills/impeccable/reference/personas.md +178 -0
- package/obj/template/.claude/skills/impeccable/reference/polish.md +232 -0
- package/obj/template/.claude/skills/impeccable/reference/product.md +62 -0
- package/obj/template/.claude/skills/impeccable/reference/quieter.md +99 -0
- package/obj/template/.claude/skills/impeccable/reference/responsive-design.md +114 -0
- package/obj/template/.claude/skills/impeccable/reference/shape.md +136 -0
- package/obj/template/.claude/skills/impeccable/reference/spatial-design.md +100 -0
- package/obj/template/.claude/skills/impeccable/reference/teach.md +137 -0
- package/obj/template/.claude/skills/impeccable/reference/typeset.md +124 -0
- package/obj/template/.claude/skills/impeccable/reference/typography.md +159 -0
- package/obj/template/.claude/skills/impeccable/reference/ux-writing.md +107 -0
- package/obj/template/.claude/skills/impeccable/scripts/cleanup-deprecated.mjs +284 -0
- package/obj/template/.claude/skills/impeccable/scripts/command-metadata.json +94 -0
- package/obj/template/.claude/skills/impeccable/scripts/design-parser.mjs +820 -0
- package/obj/template/.claude/skills/impeccable/scripts/detect-csp.mjs +198 -0
- package/obj/template/.claude/skills/impeccable/scripts/is-generated.mjs +69 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-accept.mjs +465 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-browser.js +4684 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-inject.mjs +436 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-poll.mjs +187 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-server.mjs +679 -0
- package/obj/template/.claude/skills/impeccable/scripts/live-wrap.mjs +395 -0
- package/obj/template/.claude/skills/impeccable/scripts/live.mjs +247 -0
- package/obj/template/.claude/skills/impeccable/scripts/load-context.mjs +93 -0
- package/obj/template/.claude/skills/impeccable/scripts/modern-screenshot.umd.js +14 -0
- package/obj/template/.claude/skills/impeccable/scripts/pin.mjs +214 -0
- package/obj/template/.claude/skills/implement/SKILL.md +83 -0
- package/obj/template/.claude/skills/intake/SKILL.md +46 -0
- package/obj/template/.claude/skills/intake/template.md +61 -0
- package/obj/template/.claude/skills/integrate/SKILL.md +62 -0
- package/obj/template/.claude/skills/memory-flush/SKILL.md +172 -0
- package/obj/template/.claude/skills/memory-flush/sweep.py +286 -0
- package/obj/template/.claude/skills/memory-flush/tests/run.sh +327 -0
- package/obj/template/.claude/skills/prose/SKILL.md +119 -0
- package/obj/template/.claude/skills/rca/SKILL.md +42 -0
- package/obj/template/.claude/skills/rca/template.md +83 -0
- package/obj/template/.claude/skills/research/SKILL.md +75 -0
- package/obj/template/.claude/skills/scenario/SKILL.md +64 -0
- package/obj/template/.claude/skills/scout/SKILL.md +72 -0
- package/obj/template/.claude/skills/security/SKILL.md +75 -0
- package/obj/template/.claude/skills/simplify/SKILL.md +67 -0
- package/obj/template/.claude/skills/spec/SKILL.md +69 -0
- package/obj/template/.claude/skills/spec/template.md +274 -0
- package/obj/template/.claude/skills/spec-diagram-review/SKILL.md +81 -0
- package/obj/template/.claude/skills/spec-lint/SKILL.md +55 -0
- package/obj/template/.claude/skills/spec-lint/lint.sh +218 -0
- package/obj/template/.claude/skills/spec-render/SKILL.md +45 -0
- package/obj/template/.claude/skills/spec-render/render.sh +109 -0
- package/obj/template/.claude/skills/spec-traceability-review/SKILL.md +72 -0
- package/obj/template/.claude/skills/swarm-dispatch/SKILL.md +212 -0
- package/obj/template/.claude/skills/swarm-dispatch/swarm_merge.sh +154 -0
- package/obj/template/.claude/skills/swarm-plan/SKILL.md +90 -0
- package/obj/template/.claude/skills/swarm-plan/validate.sh +181 -0
- package/obj/template/.claude/skills/tdd/SKILL.md +100 -0
- package/obj/template/.claude/skills/technical-tutorials/SKILL.md +569 -0
- package/obj/template/.claude/skills/technical-tutorials/references/audience-context-README.md +53 -0
- package/obj/template/.claude/skills/technical-tutorials/references/audience-context.md +246 -0
- package/obj/template/.claude/skills/technical-tutorials/references/audience-example.md +175 -0
- package/obj/template/.claude/skills/technical-tutorials/references/audience-template.md +152 -0
- package/obj/template/.claude/skills/triage/SKILL.md +55 -0
- package/obj/template/.claude/skills/verify/SKILL.md +74 -0
- package/obj/template/.mcp.json +24 -0
- package/obj/template/CLAUDE.md +327 -0
- package/obj/template/docs/init/seed.md +585 -0
- package/obj/template/manifest.json +214 -0
- package/package.json +48 -0
- package/src/.mcp.template.json +24 -0
- package/src/.npmrc.template +2 -0
- package/src/CLAUDE.template.md +327 -0
- package/src/agents/swarm-worker.template.md +51 -0
- package/src/cli/conflict.js +31 -0
- package/src/cli/doctor.js +152 -0
- package/src/cli/install.js +93 -0
- package/src/cli/io.js +27 -0
- package/src/cli/manifest.js +38 -0
- package/src/cli/mcp.js +54 -0
- package/src/cli/merge.js +107 -0
- package/src/cli/plantuml.js +121 -0
- package/src/cli/util.js +10 -0
- package/src/memory/_pending.template.md +15 -0
- package/src/memory/_resume.template.md +12 -0
- package/src/memory/conventions.template.md +26 -0
- package/src/memory/decisions.template.md +29 -0
- package/src/memory/landmarks.template.md +26 -0
- package/src/memory/landmines.template.md +27 -0
- package/src/memory/libraries.template.md +27 -0
- package/src/memory/pending-questions.template.md +28 -0
- package/src/project.template.json +221 -0
- package/src/seed.template.md +585 -0
- package/src/settings.template.json +110 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI helper: find an element in source and wrap it in a variant container.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* npx impeccable wrap --id SESSION_ID --count N --query "hero-combined-left" [--file path]
|
|
6
|
+
*
|
|
7
|
+
* Searches project files for the element matching the query (class name, ID, or
|
|
8
|
+
* text snippet), wraps it with the variant scaffolding, and prints the file path
|
|
9
|
+
* + line range where the agent should insert variant HTML.
|
|
10
|
+
*
|
|
11
|
+
* This replaces 3-4 agent tool calls (grep + read + edit) with a single CLI call.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { isGeneratedFile } from './is-generated.mjs';
|
|
17
|
+
|
|
18
|
+
const EXTENSIONS = ['.html', '.jsx', '.tsx', '.vue', '.svelte', '.astro'];
|
|
19
|
+
|
|
20
|
+
export async function wrapCli() {
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
|
|
23
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
24
|
+
console.log(`Usage: impeccable wrap [options]
|
|
25
|
+
|
|
26
|
+
Find an element in source and wrap it in a variant container.
|
|
27
|
+
|
|
28
|
+
Required:
|
|
29
|
+
--id ID Session ID for the variant wrapper
|
|
30
|
+
--count N Number of expected variants (1-8)
|
|
31
|
+
|
|
32
|
+
Element identification (at least one required):
|
|
33
|
+
--element-id ID HTML id attribute of the element
|
|
34
|
+
--classes A,B,C Comma-separated CSS class names
|
|
35
|
+
--tag TAG Tag name (div, section, etc.)
|
|
36
|
+
--query TEXT Fallback: raw text to search for
|
|
37
|
+
|
|
38
|
+
Optional:
|
|
39
|
+
--file PATH Source file to search in (skips auto-detection)
|
|
40
|
+
--help Show this help message
|
|
41
|
+
|
|
42
|
+
Output (JSON):
|
|
43
|
+
{ file, startLine, endLine, insertLine, commentSyntax }
|
|
44
|
+
|
|
45
|
+
The agent should insert variant HTML at insertLine.`);
|
|
46
|
+
process.exit(0);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const id = argVal(args, '--id');
|
|
50
|
+
const count = parseInt(argVal(args, '--count') || '3');
|
|
51
|
+
const elementId = argVal(args, '--element-id');
|
|
52
|
+
const classes = argVal(args, '--classes');
|
|
53
|
+
const tag = argVal(args, '--tag');
|
|
54
|
+
const query = argVal(args, '--query');
|
|
55
|
+
const filePath = argVal(args, '--file');
|
|
56
|
+
|
|
57
|
+
if (!id) { console.error('Missing --id'); process.exit(1); }
|
|
58
|
+
if (!elementId && !classes && !query) {
|
|
59
|
+
console.error('Need at least one of: --element-id, --classes, --query');
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Build search queries in priority order (most specific first)
|
|
64
|
+
const queries = buildSearchQueries(elementId, classes, tag, query);
|
|
65
|
+
|
|
66
|
+
const genOpts = { cwd: process.cwd() };
|
|
67
|
+
|
|
68
|
+
// Find the source file. Generated files are excluded from auto-search so we
|
|
69
|
+
// don't silently write variants into a file the next build will wipe.
|
|
70
|
+
let targetFile = filePath;
|
|
71
|
+
let matchedQuery = null;
|
|
72
|
+
if (!targetFile) {
|
|
73
|
+
for (const q of queries) {
|
|
74
|
+
targetFile = findFileWithQuery(q, process.cwd(), genOpts);
|
|
75
|
+
if (targetFile) { matchedQuery = q; break; }
|
|
76
|
+
}
|
|
77
|
+
if (!targetFile) {
|
|
78
|
+
// Nothing in source. Did the element show up in a generated file? That
|
|
79
|
+
// tells the agent "fall back to the agent-driven flow" vs "element just
|
|
80
|
+
// doesn't exist in this project."
|
|
81
|
+
let generatedHit = null;
|
|
82
|
+
for (const q of queries) {
|
|
83
|
+
generatedHit = findFileWithQuery(q, process.cwd(), { ...genOpts, includeGenerated: true });
|
|
84
|
+
if (generatedHit) break;
|
|
85
|
+
}
|
|
86
|
+
if (generatedHit) {
|
|
87
|
+
console.error(JSON.stringify({
|
|
88
|
+
error: 'element_not_in_source',
|
|
89
|
+
fallback: 'agent-driven',
|
|
90
|
+
generatedMatch: path.relative(process.cwd(), generatedHit),
|
|
91
|
+
hint: 'Element found only in a generated file. See "Handle fallback" in live.md.',
|
|
92
|
+
}));
|
|
93
|
+
} else {
|
|
94
|
+
console.error(JSON.stringify({
|
|
95
|
+
error: 'element_not_found',
|
|
96
|
+
fallback: 'agent-driven',
|
|
97
|
+
hint: 'Element not found in any project file. It may be runtime-injected (JS component, etc.). See "Handle fallback" in live.md.',
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
if (isGeneratedFile(targetFile, genOpts)) {
|
|
104
|
+
console.error(JSON.stringify({
|
|
105
|
+
error: 'file_is_generated',
|
|
106
|
+
fallback: 'agent-driven',
|
|
107
|
+
file: path.relative(process.cwd(), path.resolve(process.cwd(), targetFile)),
|
|
108
|
+
hint: 'Explicit --file points at a generated file. Writing here gets wiped by the next build. See "Handle fallback" in live.md.',
|
|
109
|
+
}));
|
|
110
|
+
process.exit(1);
|
|
111
|
+
}
|
|
112
|
+
matchedQuery = queries[0];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const content = fs.readFileSync(targetFile, 'utf-8');
|
|
116
|
+
const lines = content.split('\n');
|
|
117
|
+
|
|
118
|
+
// Find the element, trying each query in priority order.
|
|
119
|
+
// Pass tag hint so findElement can reject matches inside wrong element types
|
|
120
|
+
// and walk backward to the real opener on multi-line JSX tags.
|
|
121
|
+
let match = null;
|
|
122
|
+
for (const q of queries) {
|
|
123
|
+
match = findElement(lines, q, tag);
|
|
124
|
+
if (match) break;
|
|
125
|
+
}
|
|
126
|
+
if (!match) {
|
|
127
|
+
console.error(JSON.stringify({ error: 'Found file but could not locate element in ' + targetFile + '. Searched for: ' + queries.join(', ') }));
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const { startLine, endLine } = match;
|
|
132
|
+
const commentSyntax = detectCommentSyntax(targetFile);
|
|
133
|
+
const isJsx = commentSyntax.open === '{/*';
|
|
134
|
+
const indent = lines[startLine].match(/^(\s*)/)[1];
|
|
135
|
+
|
|
136
|
+
// Extract the original element
|
|
137
|
+
const originalLines = lines.slice(startLine, endLine + 1);
|
|
138
|
+
const originalIndented = originalLines.map(l => indent + ' ' + l.trimStart()).join('\n');
|
|
139
|
+
|
|
140
|
+
// Wrapper attributes differ by syntax. HTML allows plain string attrs;
|
|
141
|
+
// JSX requires object-literal style and parses string attrs as HTML (which
|
|
142
|
+
// either type-errors or renders a literal CSS string).
|
|
143
|
+
const styleContents = isJsx ? 'style={{ display: "contents" }}' : 'style="display: contents"';
|
|
144
|
+
|
|
145
|
+
// Build the wrapper
|
|
146
|
+
const wrapperLines = [
|
|
147
|
+
indent + commentSyntax.open + ' impeccable-variants-start ' + id + ' ' + commentSyntax.close,
|
|
148
|
+
indent + '<div data-impeccable-variants="' + id + '" data-impeccable-variant-count="' + count + '" ' + styleContents + '>',
|
|
149
|
+
indent + ' ' + commentSyntax.open + ' Original ' + commentSyntax.close,
|
|
150
|
+
indent + ' <div data-impeccable-variant="original">',
|
|
151
|
+
originalIndented,
|
|
152
|
+
indent + ' </div>',
|
|
153
|
+
indent + ' ' + commentSyntax.open + ' Variants: insert below this line ' + commentSyntax.close,
|
|
154
|
+
indent + '</div>',
|
|
155
|
+
indent + commentSyntax.open + ' impeccable-variants-end ' + id + ' ' + commentSyntax.close,
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
// Replace the original element with the wrapper
|
|
159
|
+
const newLines = [
|
|
160
|
+
...lines.slice(0, startLine),
|
|
161
|
+
...wrapperLines,
|
|
162
|
+
...lines.slice(endLine + 1),
|
|
163
|
+
];
|
|
164
|
+
fs.writeFileSync(targetFile, newLines.join('\n'), 'utf-8');
|
|
165
|
+
|
|
166
|
+
// Calculate insert line (the "insert below this line" comment)
|
|
167
|
+
const insertLine = startLine + 6; // 0-indexed in the new file
|
|
168
|
+
|
|
169
|
+
console.log(JSON.stringify({
|
|
170
|
+
file: path.relative(process.cwd(), targetFile),
|
|
171
|
+
startLine: startLine + 1, // 1-indexed for the agent
|
|
172
|
+
endLine: startLine + wrapperLines.length, // 1-indexed
|
|
173
|
+
insertLine: insertLine + 1, // 1-indexed: where variants go
|
|
174
|
+
commentSyntax: commentSyntax,
|
|
175
|
+
originalLineCount: originalLines.length,
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
// Helpers
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
function argVal(args, flag) {
|
|
184
|
+
const idx = args.indexOf(flag);
|
|
185
|
+
return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Build search query strings in priority order (most specific first).
|
|
190
|
+
* ID is most reliable, then specific class combos, then single classes, then raw query.
|
|
191
|
+
*/
|
|
192
|
+
function buildSearchQueries(elementId, classes, tag, query) {
|
|
193
|
+
const queries = [];
|
|
194
|
+
|
|
195
|
+
// 1. ID is the most specific
|
|
196
|
+
if (elementId) {
|
|
197
|
+
queries.push('id="' + elementId + '"');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 2. Full class attribute match (for elements with distinctive multi-class combos).
|
|
201
|
+
// Emit both class="..." (HTML) and className="..." (React/JSX) so whichever
|
|
202
|
+
// convention the file uses will match.
|
|
203
|
+
if (classes) {
|
|
204
|
+
const classList = classes.split(',').map(c => c.trim()).filter(Boolean);
|
|
205
|
+
if (classList.length > 1) {
|
|
206
|
+
const joined = classList.join(' ');
|
|
207
|
+
const sorted = [...classList].sort((a, b) => b.length - a.length);
|
|
208
|
+
queries.push('class="' + joined + '"');
|
|
209
|
+
queries.push('className="' + joined + '"');
|
|
210
|
+
queries.push(sorted[0]); // most distinctive single class, fallback
|
|
211
|
+
} else if (classList.length === 1) {
|
|
212
|
+
queries.push(classList[0]);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 3. Tag + class combo (e.g., <section class="hero">).
|
|
217
|
+
// Same dual-emit for JSX compatibility.
|
|
218
|
+
if (tag && classes) {
|
|
219
|
+
const firstClass = classes.split(',')[0].trim();
|
|
220
|
+
queries.push('<' + tag + ' class="' + firstClass);
|
|
221
|
+
queries.push('<' + tag + ' className="' + firstClass);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 4. Raw fallback query
|
|
225
|
+
if (query) {
|
|
226
|
+
queries.push(query);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return queries;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function detectCommentSyntax(filePath) {
|
|
233
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
234
|
+
if (ext === '.jsx' || ext === '.tsx') {
|
|
235
|
+
return { open: '{/*', close: '*/}' };
|
|
236
|
+
}
|
|
237
|
+
// HTML, Vue, Svelte, Astro all use HTML comments
|
|
238
|
+
return { open: '<!--', close: '-->' };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Search project files for the query string (class name, ID, etc.)
|
|
243
|
+
* Returns the first matching file path, or null.
|
|
244
|
+
*/
|
|
245
|
+
function findFileWithQuery(query, cwd, genOpts = {}) {
|
|
246
|
+
const searchDirs = ['src', 'app', 'pages', 'components', 'public', 'views', 'templates', '.'];
|
|
247
|
+
const seen = new Set();
|
|
248
|
+
|
|
249
|
+
for (const dir of searchDirs) {
|
|
250
|
+
const absDir = path.join(cwd, dir);
|
|
251
|
+
if (!fs.existsSync(absDir)) continue;
|
|
252
|
+
const result = searchDir(absDir, query, seen, 0, genOpts);
|
|
253
|
+
if (result) return result;
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function searchDir(dir, query, seen, depth, genOpts) {
|
|
259
|
+
if (depth > 5) return null; // don't go too deep
|
|
260
|
+
const realDir = fs.realpathSync(dir);
|
|
261
|
+
if (seen.has(realDir)) return null;
|
|
262
|
+
seen.add(realDir);
|
|
263
|
+
|
|
264
|
+
let entries;
|
|
265
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
|
|
266
|
+
catch { return null; }
|
|
267
|
+
|
|
268
|
+
// Check files first
|
|
269
|
+
for (const entry of entries) {
|
|
270
|
+
if (!entry.isFile()) continue;
|
|
271
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
272
|
+
if (!EXTENSIONS.includes(ext)) continue;
|
|
273
|
+
|
|
274
|
+
const filePath = path.join(dir, entry.name);
|
|
275
|
+
if (!genOpts.includeGenerated && isGeneratedFile(filePath, genOpts)) continue;
|
|
276
|
+
try {
|
|
277
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
278
|
+
if (content.includes(query)) return filePath;
|
|
279
|
+
} catch { /* skip unreadable files */ }
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Then recurse into directories. Always skip node_modules and .git (never
|
|
283
|
+
// project content). dist/build/out are left to the isGeneratedFile guard so
|
|
284
|
+
// the includeGenerated second-pass can still find the element there and
|
|
285
|
+
// report `generatedMatch`.
|
|
286
|
+
for (const entry of entries) {
|
|
287
|
+
if (!entry.isDirectory()) continue;
|
|
288
|
+
if (entry.name === 'node_modules' || entry.name === '.git') continue;
|
|
289
|
+
const result = searchDir(path.join(dir, entry.name), query, seen, depth + 1, genOpts);
|
|
290
|
+
if (result) return result;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Regex that matches a tag opener on a line. Allows the tag name to be
|
|
298
|
+
* followed by whitespace, `>`, `/`, or end-of-line so that multi-line JSX
|
|
299
|
+
* openers (e.g. `<section\n className="..."\n>`) are recognised.
|
|
300
|
+
*/
|
|
301
|
+
const OPENER_RE = /<([A-Za-z][A-Za-z0-9]*)(?=[\s/>]|$)/;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Find the element's start and end line in the file.
|
|
305
|
+
*
|
|
306
|
+
* `query` is a class name, attribute fragment (`class="..."`, `className="..."`,
|
|
307
|
+
* `id="..."`), or a raw text snippet. Because a query can appear on a
|
|
308
|
+
* continuation line of a multi-line tag (e.g. the `className="..."` row of a
|
|
309
|
+
* `<section\n className="..."\n>` JSX tag), we walk backward from the match
|
|
310
|
+
* line to find the actual tag opener. When `tag` is provided, opener candidates
|
|
311
|
+
* must match that tag name.
|
|
312
|
+
*/
|
|
313
|
+
function findElement(lines, query, tag = null) {
|
|
314
|
+
// Iterate all matches — the first substring hit isn't always the right one.
|
|
315
|
+
for (let i = 0; i < lines.length; i++) {
|
|
316
|
+
if (!lines[i].includes(query)) continue;
|
|
317
|
+
|
|
318
|
+
const stripped = lines[i].trim();
|
|
319
|
+
if (stripped.startsWith('<!--') || stripped.startsWith('{/*') || stripped.startsWith('//')) continue;
|
|
320
|
+
// Skip lines already inside a variant wrapper
|
|
321
|
+
if (lines[i].includes('data-impeccable-variant')) continue;
|
|
322
|
+
|
|
323
|
+
const openerLine = findOpenerLine(lines, i, tag);
|
|
324
|
+
if (openerLine === -1) continue;
|
|
325
|
+
|
|
326
|
+
const endLine = findClosingLine(lines, openerLine);
|
|
327
|
+
return { startLine: openerLine, endLine };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Resolve a match line to the real tag opener. If the match line itself opens
|
|
335
|
+
* a tag, return it. Otherwise walk up to 10 lines backward looking for the
|
|
336
|
+
* first tag opener. If `tag` is specified, the opener must match that tag
|
|
337
|
+
* name; an opener with a different tag name aborts the backward walk for this
|
|
338
|
+
* match (we don't jump across element boundaries).
|
|
339
|
+
*
|
|
340
|
+
* Returns the line index of the opener, or -1 if none can be resolved.
|
|
341
|
+
*/
|
|
342
|
+
function findOpenerLine(lines, matchLine, tag) {
|
|
343
|
+
const self = lines[matchLine].match(OPENER_RE);
|
|
344
|
+
if (self) {
|
|
345
|
+
if (!tag || self[1] === tag) return matchLine;
|
|
346
|
+
return -1;
|
|
347
|
+
}
|
|
348
|
+
const MAX_BACKWALK = 10;
|
|
349
|
+
for (let i = matchLine - 1; i >= Math.max(0, matchLine - MAX_BACKWALK); i--) {
|
|
350
|
+
const opener = lines[i].match(OPENER_RE);
|
|
351
|
+
if (!opener) continue;
|
|
352
|
+
if (!tag || opener[1] === tag) return i;
|
|
353
|
+
// Different tag name than requested — abort; we're inside a non-target opener.
|
|
354
|
+
return -1;
|
|
355
|
+
}
|
|
356
|
+
return -1;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Starting from a line with an opening tag, find the line with the matching
|
|
361
|
+
* closing tag by counting tag nesting depth.
|
|
362
|
+
*/
|
|
363
|
+
function findClosingLine(lines, start) {
|
|
364
|
+
const openMatch = lines[start].match(OPENER_RE);
|
|
365
|
+
if (!openMatch) return start; // caller passed a non-opener; nothing to span
|
|
366
|
+
|
|
367
|
+
const tagName = openMatch[1];
|
|
368
|
+
let depth = 0;
|
|
369
|
+
const openRe = new RegExp('<' + tagName + '(?=[\\s/>]|$)', 'g');
|
|
370
|
+
const selfCloseRe = new RegExp('<' + tagName + '[^>]*/>', 'g');
|
|
371
|
+
const closeRe = new RegExp('</' + tagName + '\\s*>', 'g');
|
|
372
|
+
|
|
373
|
+
for (let i = start; i < lines.length; i++) {
|
|
374
|
+
const line = lines[i];
|
|
375
|
+
const opens = (line.match(openRe) || []).length;
|
|
376
|
+
const selfCloses = (line.match(selfCloseRe) || []).length;
|
|
377
|
+
const closes = (line.match(closeRe) || []).length;
|
|
378
|
+
|
|
379
|
+
depth += opens - selfCloses - closes;
|
|
380
|
+
|
|
381
|
+
if (depth <= 0) return i;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// If we can't find the close, return a reasonable guess
|
|
385
|
+
return Math.min(start + 50, lines.length - 1);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Auto-execute when run directly (node live-wrap.mjs ...)
|
|
389
|
+
const _running = process.argv[1];
|
|
390
|
+
if (_running?.endsWith('live-wrap.mjs') || _running?.endsWith('live-wrap.mjs/')) {
|
|
391
|
+
wrapCli();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Test exports (used by tests/live-wrap.test.mjs)
|
|
395
|
+
export { buildSearchQueries, findElement, findClosingLine, detectCommentSyntax };
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI entry point: prepare everything needed to enter the live variant poll loop.
|
|
3
|
+
*
|
|
4
|
+
* Does (all in one command):
|
|
5
|
+
* 1. Check config.json (returns config_missing if first-ever run)
|
|
6
|
+
* 2. Start the live server in the background (or reuse a running one)
|
|
7
|
+
* 3. Inject the browser script tag into the project's entry file
|
|
8
|
+
* 4. Read .impeccable.md for design context (if present)
|
|
9
|
+
* 5. Print a single JSON blob with everything the agent needs
|
|
10
|
+
*
|
|
11
|
+
* After this, the agent's only remaining steps are:
|
|
12
|
+
* - Open the project's live dev/preview URL in the browser (optional, if browser automation exists)—not `serverPort`; that port is the Impeccable helper for /live.js and /poll
|
|
13
|
+
* - Enter the poll loop: `node live-poll.mjs`
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* node live.mjs # Prepare everything, print JSON, exit
|
|
17
|
+
* node live.mjs --help
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { execSync } from 'node:child_process';
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
import { loadContext } from './load-context.mjs';
|
|
25
|
+
import { resolveFiles } from './live-inject.mjs';
|
|
26
|
+
|
|
27
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
const PID_FILE = path.join(process.cwd(), '.impeccable-live.json');
|
|
29
|
+
|
|
30
|
+
async function liveCli() {
|
|
31
|
+
const args = process.argv.slice(2);
|
|
32
|
+
|
|
33
|
+
if (args.includes('--help') || args.includes('-h')) {
|
|
34
|
+
console.log(`Usage: node live.mjs
|
|
35
|
+
|
|
36
|
+
Prepare everything for live variant mode in a single command:
|
|
37
|
+
- Checks scripts/config.json (required, created once per project)
|
|
38
|
+
- Starts (or reuses) the live server in the background
|
|
39
|
+
- Injects the browser script tag
|
|
40
|
+
- Reads .impeccable.md for design context
|
|
41
|
+
|
|
42
|
+
On success, prints a JSON blob with:
|
|
43
|
+
{ ok, serverPort, serverToken, pageFile, hasContext, context }
|
|
44
|
+
|
|
45
|
+
On config_missing, prints:
|
|
46
|
+
{ ok: false, error: "config_missing", configPath, hint }
|
|
47
|
+
|
|
48
|
+
The agent should then:
|
|
49
|
+
1. If config_missing, create the config and re-run this script
|
|
50
|
+
2. Optionally open the project's dev/preview URL in the browser (see reference/live.md—not serverPort)
|
|
51
|
+
3. Enter the poll loop: node live-poll.mjs`);
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 1. Check config (fail fast if missing — no point starting anything else)
|
|
56
|
+
const checkOut = runScript('live-inject.mjs', ['--check']);
|
|
57
|
+
const checkResult = safeParse(checkOut);
|
|
58
|
+
if (!checkResult || !checkResult.ok) {
|
|
59
|
+
console.log(JSON.stringify(checkResult || { ok: false, error: 'check_failed', raw: checkOut }));
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 2. Start server (or reuse existing)
|
|
64
|
+
const serverInfo = ensureServerRunning();
|
|
65
|
+
if (!serverInfo) {
|
|
66
|
+
console.log(JSON.stringify({ ok: false, error: 'server_start_failed' }));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 3. Inject the script tag at the current port
|
|
71
|
+
const injectOut = runScript('live-inject.mjs', ['--port', String(serverInfo.port)]);
|
|
72
|
+
const injectResult = safeParse(injectOut);
|
|
73
|
+
if (!injectResult || !injectResult.ok) {
|
|
74
|
+
console.log(JSON.stringify({
|
|
75
|
+
ok: false,
|
|
76
|
+
error: 'inject_failed',
|
|
77
|
+
detail: injectResult || injectOut,
|
|
78
|
+
serverPort: serverInfo.port,
|
|
79
|
+
}));
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 4. Load PRODUCT.md + DESIGN.md context (auto-migrates legacy .impeccable.md)
|
|
84
|
+
const ctx = loadContext(process.cwd());
|
|
85
|
+
|
|
86
|
+
// 5. Compute drift-heal: compare resolved inject targets against the
|
|
87
|
+
// project's HTML files. Orphans are HTML files not covered by config.
|
|
88
|
+
// Warning only — the agent decides whether to act.
|
|
89
|
+
const resolvedFiles = resolveFiles(process.cwd(), checkResult.config);
|
|
90
|
+
const drift = scanForDrift(process.cwd(), resolvedFiles, checkResult.config);
|
|
91
|
+
|
|
92
|
+
// 6. Emit everything the agent needs
|
|
93
|
+
console.log(JSON.stringify({
|
|
94
|
+
ok: true,
|
|
95
|
+
serverPort: serverInfo.port,
|
|
96
|
+
serverToken: serverInfo.token,
|
|
97
|
+
pageFiles: resolvedFiles,
|
|
98
|
+
configDrift: drift,
|
|
99
|
+
hasProduct: ctx.hasProduct,
|
|
100
|
+
product: ctx.product,
|
|
101
|
+
productPath: ctx.productPath,
|
|
102
|
+
hasDesign: ctx.hasDesign,
|
|
103
|
+
design: ctx.design,
|
|
104
|
+
designPath: ctx.designPath,
|
|
105
|
+
migrated: ctx.migrated,
|
|
106
|
+
}, null, 2));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Drift-heal scan. Walks the project for HTML files under common
|
|
111
|
+
* page-source directories (public/, src/, app/, pages/) and reports any
|
|
112
|
+
* that aren't covered by the resolved inject targets. This is purely
|
|
113
|
+
* advisory — the agent can ignore it, or suggest the user add the
|
|
114
|
+
* orphans to config.files.
|
|
115
|
+
*
|
|
116
|
+
* Skipped if config.files already contains at least one glob pattern
|
|
117
|
+
* covering everything in practice (signaled by the orphan count being 0).
|
|
118
|
+
*/
|
|
119
|
+
function scanForDrift(rootDir, resolvedFiles, config) {
|
|
120
|
+
const SCAN_ROOTS = ['public', 'src', 'app', 'pages'];
|
|
121
|
+
const IGNORE_DIRS = new Set([
|
|
122
|
+
'node_modules', '.git', '.next', '.nuxt', '.svelte-kit', '.astro',
|
|
123
|
+
'.turbo', '.vercel', '.cache', 'coverage', 'dist', 'build',
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
const resolvedSet = new Set(resolvedFiles.map((f) => f.split(path.sep).join('/')));
|
|
127
|
+
|
|
128
|
+
// Files matching the user's `exclude` globs are intentional omissions,
|
|
129
|
+
// not drift. Compile them to regexes so the orphan list stays signal.
|
|
130
|
+
const userExcludeRegexes = (Array.isArray(config.exclude) ? config.exclude : [])
|
|
131
|
+
.map((p) => globToRegex(p));
|
|
132
|
+
const isUserExcluded = (rel) => userExcludeRegexes.some((re) => re.test(rel));
|
|
133
|
+
|
|
134
|
+
const orphans = [];
|
|
135
|
+
|
|
136
|
+
const walk = (dir, relBase) => {
|
|
137
|
+
let entries;
|
|
138
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
|
|
139
|
+
catch { return; }
|
|
140
|
+
for (const e of entries) {
|
|
141
|
+
const rel = relBase ? `${relBase}/${e.name}` : e.name;
|
|
142
|
+
if (e.isDirectory()) {
|
|
143
|
+
if (IGNORE_DIRS.has(e.name) || e.name.startsWith('.')) continue;
|
|
144
|
+
walk(path.join(dir, e.name), rel);
|
|
145
|
+
} else if (e.isFile() && e.name.endsWith('.html')) {
|
|
146
|
+
if (resolvedSet.has(rel)) continue;
|
|
147
|
+
if (isUserExcluded(rel)) continue;
|
|
148
|
+
orphans.push(rel);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
for (const root of SCAN_ROOTS) {
|
|
154
|
+
const abs = path.join(rootDir, root);
|
|
155
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
156
|
+
walk(abs, root);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (orphans.length === 0) return null;
|
|
161
|
+
const capped = orphans.slice(0, 20);
|
|
162
|
+
return {
|
|
163
|
+
orphans: capped,
|
|
164
|
+
orphanCount: orphans.length,
|
|
165
|
+
hint: `${orphans.length} HTML file(s) exist but aren't in config.files. Consider adding them, or use a glob pattern like "public/**/*.html".`,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Same glob-to-regex mapping used by live-inject.mjs. Kept inline here
|
|
171
|
+
* to avoid a circular import (live-inject.mjs already imports nothing
|
|
172
|
+
* from live.mjs). The two must stay in sync.
|
|
173
|
+
*/
|
|
174
|
+
function globToRegex(pattern) {
|
|
175
|
+
let re = '';
|
|
176
|
+
let i = 0;
|
|
177
|
+
while (i < pattern.length) {
|
|
178
|
+
const c = pattern[i];
|
|
179
|
+
if (c === '*') {
|
|
180
|
+
if (pattern[i + 1] === '*') {
|
|
181
|
+
if (pattern[i + 2] === '/') { re += '(?:.*/)?'; i += 3; }
|
|
182
|
+
else { re += '.*'; i += 2; }
|
|
183
|
+
} else {
|
|
184
|
+
re += '[^/]*';
|
|
185
|
+
i += 1;
|
|
186
|
+
}
|
|
187
|
+
} else if (c === '?') {
|
|
188
|
+
re += '[^/]';
|
|
189
|
+
i += 1;
|
|
190
|
+
} else if (/[.+^${}()|[\]\\]/.test(c)) {
|
|
191
|
+
re += '\\' + c;
|
|
192
|
+
i += 1;
|
|
193
|
+
} else {
|
|
194
|
+
re += c;
|
|
195
|
+
i += 1;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return new RegExp('^' + re + '$');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
// Helpers
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
|
|
205
|
+
function runScript(name, args) {
|
|
206
|
+
const scriptPath = path.join(__dirname, name);
|
|
207
|
+
const cmd = `node "${scriptPath}" ${args.map(a => `"${a}"`).join(' ')}`;
|
|
208
|
+
try {
|
|
209
|
+
return execSync(cmd, { encoding: 'utf-8', cwd: process.cwd(), timeout: 15_000 });
|
|
210
|
+
} catch (err) {
|
|
211
|
+
// execSync throws on non-zero exit; return stdout if any
|
|
212
|
+
return err.stdout || err.message || '';
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function safeParse(out) {
|
|
217
|
+
try { return JSON.parse(String(out).trim()); } catch { return null; }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Return { pid, port, token } for the running live server, starting one if needed.
|
|
222
|
+
*/
|
|
223
|
+
function ensureServerRunning() {
|
|
224
|
+
// Try to reuse an existing server
|
|
225
|
+
try {
|
|
226
|
+
const existing = JSON.parse(fs.readFileSync(PID_FILE, 'utf-8'));
|
|
227
|
+
if (existing && existing.pid) {
|
|
228
|
+
try {
|
|
229
|
+
process.kill(existing.pid, 0); // throws if dead
|
|
230
|
+
return existing;
|
|
231
|
+
} catch { /* stale PID file — the server script will clean it up */ }
|
|
232
|
+
}
|
|
233
|
+
} catch { /* no PID file */ }
|
|
234
|
+
|
|
235
|
+
// Start a new server
|
|
236
|
+
const out = runScript('live-server.mjs', ['--background']);
|
|
237
|
+
return safeParse(out);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
// Auto-execute
|
|
242
|
+
// ---------------------------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
const _running = process.argv[1];
|
|
245
|
+
if (_running?.endsWith('live.mjs') || _running?.endsWith('live.mjs/')) {
|
|
246
|
+
liveCli();
|
|
247
|
+
}
|