@chemx/starter-kit 26.9.11-481 → 26.9.12-107
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/cli/audit/ai-slop-detector.js +262 -0
- package/cli/audit/extended-visitors.js +359 -0
- package/cli/audit/history.js +19 -2
- package/cli/audit/metrics.js +69 -0
- package/cli/audit/prompts.js +175 -48
- package/cli/audit/reporter-banner.d.ts +3 -0
- package/cli/audit/reporter-banner.js +74 -0
- package/cli/audit/reporter-grades.js +11 -26
- package/cli/audit/reporter-grouping-markdown.js +83 -0
- package/cli/audit/reporter-grouping.d.ts +42 -0
- package/cli/audit/reporter-grouping.js +243 -0
- package/cli/audit/reporter-markdown.js +34 -22
- package/cli/audit/reporter-sections.d.ts +10 -0
- package/cli/audit/reporter-sections.js +259 -0
- package/cli/audit/reporter-summary.d.ts +4 -0
- package/cli/audit/reporter-summary.js +142 -0
- package/cli/audit/reporter-utils.js +13 -1
- package/cli/audit/reporter.d.ts +16 -0
- package/cli/audit/reporter.js +47 -420
- package/cli/audit/rules-helpers.js +16 -0
- package/cli/audit/rules-registry.js +88 -1
- package/cli/audit/rules.js +32 -1
- package/cli/audit/social-git.js +133 -131
- package/cli/audit/social.js +15 -2
- package/cli/audit/types.d.ts +28 -27
- package/cli/audit.js +56 -3
- package/cli/badge.js +262 -0
- package/cli/generator-templates.js +264 -0
- package/cli/generator.d.ts +10 -0
- package/cli/generator.js +151 -0
- package/cli/help.js +17 -7
- package/cli/index.js +26 -16
- package/cli/installer.d.ts +2 -0
- package/cli/installer.js +52 -38
- package/cli/license.js +17 -4
- package/cli/navigator-actions.js +63 -1
- package/cli/navigator-banner.js +17 -6
- package/cli/navigator-grades.js +9 -0
- package/cli/navigator-menu.js +36 -0
- package/cli/navigator-share.js +7 -4
- package/cli/navigator.d.ts +5 -1
- package/cli/navigator.js +49 -12
- package/cli/scaffold.js +3 -54
- package/cli/terminal.js +4 -1
- package/docs/CHANGELOG.md +50 -0
- package/package.json +1 -1
package/cli/badge.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
hasGum,
|
|
5
|
+
gumChoose,
|
|
6
|
+
gumInput,
|
|
7
|
+
promptQuestion,
|
|
8
|
+
renderBanner
|
|
9
|
+
} from './terminal.js';
|
|
10
|
+
import { copyToClipboard } from './audit/social-git.js';
|
|
11
|
+
import { getAuditHistory, getAuditBaseline } from './audit/history.js';
|
|
12
|
+
import { getStoredDiscussion } from './audit/discussion-store.js';
|
|
13
|
+
|
|
14
|
+
export const resolveLatestAuditInfo = (cwd = process.cwd()) => {
|
|
15
|
+
const stored = getStoredDiscussion(cwd);
|
|
16
|
+
const discussionUrl = stored?.url || null;
|
|
17
|
+
|
|
18
|
+
const history = getAuditHistory(cwd);
|
|
19
|
+
if (history.length > 0) {
|
|
20
|
+
const latest = history[history.length - 1];
|
|
21
|
+
return {
|
|
22
|
+
grade: latest.health?.grade || 'A+',
|
|
23
|
+
score: latest.health?.score || 100,
|
|
24
|
+
label: latest.health?.label || 'Optimal',
|
|
25
|
+
discussionUrl
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const baseline = getAuditBaseline(cwd);
|
|
29
|
+
if (baseline) {
|
|
30
|
+
return {
|
|
31
|
+
grade: baseline.health?.grade || 'A+',
|
|
32
|
+
score: baseline.health?.score || 100,
|
|
33
|
+
label: baseline.health?.label || 'Optimal',
|
|
34
|
+
discussionUrl
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return { grade: 'A+', score: 100, label: 'Optimal', discussionUrl };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const generateMinimalTextSnippet = (label, grade, discussionUrl = null) => {
|
|
41
|
+
const gradeElement = discussionUrl
|
|
42
|
+
? `<a href="${discussionUrl}" target="_blank" rel="noopener noreferrer" style="font-weight:bold;color:#a3e635;text-decoration:none;" title="View verified audit report on GitHub Discussions"> [${grade}]</a>`
|
|
43
|
+
: `<span style="font-weight:bold;color:#a3e635;"> [${grade}]</span>`;
|
|
44
|
+
|
|
45
|
+
return `<p style="font-family:monospace;font-size:11px;color:#94a3b8;margin:0.25rem 0;">
|
|
46
|
+
${label}
|
|
47
|
+
<a href="https://chemicalx.xophz.com" target="_blank" rel="noopener noreferrer" style="color:#38bdf8;text-decoration:none;font-weight:500;">Chemical X</a>
|
|
48
|
+
${gradeElement}
|
|
49
|
+
</p>`;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const generateHtmlBadgeSnippet = (label, grade, discussionUrl = null) => {
|
|
53
|
+
const targetHref = discussionUrl || 'https://chemicalx.xophz.com';
|
|
54
|
+
const targetTitle = discussionUrl ? 'Verified Chemical X Audit Report on GitHub Discussions' : 'Verified by Chemical X Protocol';
|
|
55
|
+
|
|
56
|
+
return `<a href="${targetHref}" target="_blank" rel="noopener noreferrer" style="display:inline-flex;align-items:center;gap:8px;padding:4px 12px;border-radius:9999px;font-family:monospace;font-size:11px;text-decoration:none;border:1px solid rgba(255,255,255,0.15);background:rgba(15,23,42,0.65);backdrop-filter:blur(8px);color:#e2e8f0;transition:all 0.2s ease;" title="${targetTitle}">
|
|
57
|
+
<span style="width:8px;height:8px;border-radius:50%;background:#a3e635;box-shadow:0 0 6px rgba(163,230,53,0.6);"></span>
|
|
58
|
+
<span>${label}</span>
|
|
59
|
+
<span style="padding:2px 7px;border-radius:9999px;font-weight:bold;font-size:10px;background:rgba(163,230,53,0.15);color:#a3e635;border:1px solid rgba(163,230,53,0.3);">${grade}</span>
|
|
60
|
+
</a>`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const generateMarkdownBadgeSnippet = (label, grade, discussionUrl = null) => {
|
|
64
|
+
const targetHref = discussionUrl || 'https://chemicalx.xophz.com';
|
|
65
|
+
const encodedLabel = encodeURIComponent(label.replace(/ /g, '_'));
|
|
66
|
+
const encodedGrade = encodeURIComponent(`[${grade}]`);
|
|
67
|
+
return `[](${targetHref})`;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const generateVueBadgeSnippet = (label, grade, discussionUrl = null) => {
|
|
71
|
+
const defaultReportUrl = discussionUrl ? `'${discussionUrl}'` : 'undefined';
|
|
72
|
+
|
|
73
|
+
return `<script setup lang="ts">
|
|
74
|
+
interface Props {
|
|
75
|
+
label?: string;
|
|
76
|
+
grade?: string;
|
|
77
|
+
href?: string;
|
|
78
|
+
reportUrl?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
withDefaults(defineProps<Props>(), {
|
|
82
|
+
label: '${label}',
|
|
83
|
+
grade: '${grade}',
|
|
84
|
+
href: 'https://chemicalx.xophz.com',
|
|
85
|
+
reportUrl: ${defaultReportUrl}
|
|
86
|
+
});
|
|
87
|
+
</script>
|
|
88
|
+
|
|
89
|
+
<template>
|
|
90
|
+
<p class="chemx-footer-line font-mono text-[0.65rem] text-zinc-500 dark:text-zinc-400">
|
|
91
|
+
{{ label }}
|
|
92
|
+
<a
|
|
93
|
+
:href="href"
|
|
94
|
+
target="_blank"
|
|
95
|
+
rel="noopener noreferrer"
|
|
96
|
+
class="text-sky-600 dark:text-sky-400 hover:underline font-medium"
|
|
97
|
+
>Chemical X</a>
|
|
98
|
+
<a
|
|
99
|
+
v-if="reportUrl && grade"
|
|
100
|
+
:href="reportUrl"
|
|
101
|
+
target="_blank"
|
|
102
|
+
rel="noopener noreferrer"
|
|
103
|
+
class="font-bold text-lime-600 dark:text-lime-400 hover:underline"
|
|
104
|
+
title="View verified Chemical X audit report"
|
|
105
|
+
> [{{ grade }}]</a>
|
|
106
|
+
<span v-else-if="grade" class="font-bold text-lime-600 dark:text-lime-400"> [{{ grade }}]</span>
|
|
107
|
+
</p>
|
|
108
|
+
</template>
|
|
109
|
+
`;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const generateReactBadgeSnippet = (label, grade, discussionUrl = null) => {
|
|
113
|
+
const defaultReportUrl = discussionUrl ? `'${discussionUrl}'` : 'undefined';
|
|
114
|
+
|
|
115
|
+
return `import React from 'react';
|
|
116
|
+
|
|
117
|
+
export interface ChemicalXBadgeProps {
|
|
118
|
+
label?: string;
|
|
119
|
+
grade?: string;
|
|
120
|
+
href?: string;
|
|
121
|
+
reportUrl?: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const ChemicalXBadge: React.FC<ChemicalXBadgeProps> = ({
|
|
125
|
+
label = '${label}',
|
|
126
|
+
grade = '${grade}',
|
|
127
|
+
href = 'https://chemicalx.xophz.com',
|
|
128
|
+
reportUrl = ${defaultReportUrl}
|
|
129
|
+
}) => (
|
|
130
|
+
<p className="chemx-footer-line" style={{ fontFamily: 'monospace', fontSize: 11, color: '#94a3b8' }}>
|
|
131
|
+
{label}{' '}
|
|
132
|
+
<a href={href} target="_blank" rel="noopener noreferrer" style={{ color: '#38bdf8', textDecoration: 'none' }}>
|
|
133
|
+
Chemical X
|
|
134
|
+
</a>
|
|
135
|
+
{reportUrl && grade ? (
|
|
136
|
+
<a
|
|
137
|
+
href={reportUrl}
|
|
138
|
+
target="_blank"
|
|
139
|
+
rel="noopener noreferrer"
|
|
140
|
+
style={{ fontWeight: 'bold', color: '#a3e635', textDecoration: 'none' }}
|
|
141
|
+
title="View verified Chemical X audit report on GitHub Discussions"
|
|
142
|
+
>
|
|
143
|
+
{' '}[{grade}]
|
|
144
|
+
</a>
|
|
145
|
+
) : (
|
|
146
|
+
grade && <span style={{ fontWeight: 'bold', color: '#a3e635' }}> [{grade}]</span>
|
|
147
|
+
)}
|
|
148
|
+
</p>
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
export default ChemicalXBadge;
|
|
152
|
+
`;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const generateSvgBadgeSnippet = (label, grade) => {
|
|
156
|
+
const width = Math.max(280, label.length * 8 + 60);
|
|
157
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="28" viewBox="0 0 ${width} 28" fill="none">
|
|
158
|
+
<rect width="${width}" height="28" rx="14" fill="#090d16" stroke="#26354a" stroke-width="1"/>
|
|
159
|
+
<circle cx="16" cy="14" r="4" fill="#a3e635"/>
|
|
160
|
+
<text x="28" y="17" fill="#cbd5e1" font-family="monospace" font-size="11" font-weight="500">${label}</text>
|
|
161
|
+
<rect x="${width - 44}" y="6" width="34" height="16" rx="8" fill="#a3e635" fill-opacity="0.18" stroke="#a3e635" stroke-opacity="0.4"/>
|
|
162
|
+
<text x="${width - 27}" y="17" text-anchor="middle" fill="#a3e635" font-family="monospace" font-size="10" font-weight="700">${grade}</text>
|
|
163
|
+
</svg>`;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export const runBadgeCommand = async (rawArgs = []) => {
|
|
167
|
+
renderBanner('Chemical X: Verified Footer Badge Generator');
|
|
168
|
+
|
|
169
|
+
const detected = resolveLatestAuditInfo();
|
|
170
|
+
const gradeFlag = rawArgs.find((a) => a.startsWith('--grade='));
|
|
171
|
+
const labelFlag = rawArgs.find((a) => a.startsWith('--label='));
|
|
172
|
+
const reportFlag = rawArgs.find((a) => a.startsWith('--report-url=') || a.startsWith('--discussion='));
|
|
173
|
+
const formatFlag = rawArgs.find((a) => a.startsWith('--format='));
|
|
174
|
+
const isCopy = rawArgs.includes('--copy');
|
|
175
|
+
|
|
176
|
+
const grade = gradeFlag ? gradeFlag.split('=')[1].toUpperCase() : detected.grade;
|
|
177
|
+
const label = labelFlag ? labelFlag.split('=')[1] : 'AI Slop cleaned with Chemical X';
|
|
178
|
+
const discussionUrl = reportFlag ? reportFlag.split('=')[1] : detected.discussionUrl;
|
|
179
|
+
|
|
180
|
+
process.stdout.write(`Active Codebase Grade: \x1b[32;1m[${grade}]\x1b[0m (Score: ${detected.score}/100)\n`);
|
|
181
|
+
process.stdout.write(`Badge Copy: \x1b[36m"${label}"\x1b[0m\n`);
|
|
182
|
+
if (discussionUrl) {
|
|
183
|
+
process.stdout.write(`Discussion Report: \x1b[35m${discussionUrl}\x1b[0m\n\n`);
|
|
184
|
+
} else {
|
|
185
|
+
process.stdout.write(`Discussion Report: \x1b[33mNone (Run 'npx chemx audit --share' to publish)\x1b[0m\n\n`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
let chosenFormat = formatFlag ? formatFlag.split('=')[1].toLowerCase() : null;
|
|
189
|
+
|
|
190
|
+
if (!chosenFormat) {
|
|
191
|
+
if (hasGum()) {
|
|
192
|
+
const choice = gumChoose([
|
|
193
|
+
'1. 📝 Minimal Inline Footer Text (HTML <p> & <a> - Matches any footer)',
|
|
194
|
+
'2. 🌐 Glassmorphic Pill Badge (HTML / Inline CSS)',
|
|
195
|
+
'3. 💚 Vue 3.4+ Component (<script setup lang="ts">)',
|
|
196
|
+
'4. ⚛️ React 19 Component (TSX)',
|
|
197
|
+
'5. 📋 Markdown / Shields.io (README & Docs)',
|
|
198
|
+
'6. 🖼️ SVG Standalone Asset (chemx-badge.svg)',
|
|
199
|
+
'7. 📦 Print All Snippets'
|
|
200
|
+
], 'Select Badge Format');
|
|
201
|
+
|
|
202
|
+
if (choice?.includes('1.')) chosenFormat = 'text';
|
|
203
|
+
else if (choice?.includes('2.')) chosenFormat = 'html';
|
|
204
|
+
else if (choice?.includes('3.')) chosenFormat = 'vue';
|
|
205
|
+
else if (choice?.includes('4.')) chosenFormat = 'react';
|
|
206
|
+
else if (choice?.includes('5.')) chosenFormat = 'markdown';
|
|
207
|
+
else if (choice?.includes('6.')) chosenFormat = 'svg';
|
|
208
|
+
else chosenFormat = 'all';
|
|
209
|
+
} else {
|
|
210
|
+
process.stdout.write('Select Badge Format:\n');
|
|
211
|
+
process.stdout.write(' [1] Minimal Inline Footer Text (HTML <p> & <a>)\n');
|
|
212
|
+
process.stdout.write(' [2] Glassmorphic Pill Badge (HTML / CSS)\n');
|
|
213
|
+
process.stdout.write(' [3] Vue 3.4+ Component\n');
|
|
214
|
+
process.stdout.write(' [4] React 19 Component\n');
|
|
215
|
+
process.stdout.write(' [5] Markdown / Shields.io\n');
|
|
216
|
+
process.stdout.write(' [6] SVG Standalone Asset\n');
|
|
217
|
+
process.stdout.write(' [7] Print All Snippets\n');
|
|
218
|
+
const input = await promptQuestion('Choice [1]: ');
|
|
219
|
+
if (input === '2') chosenFormat = 'html';
|
|
220
|
+
else if (input === '3') chosenFormat = 'vue';
|
|
221
|
+
else if (input === '4') chosenFormat = 'react';
|
|
222
|
+
else if (input === '5') chosenFormat = 'markdown';
|
|
223
|
+
else if (input === '6') chosenFormat = 'svg';
|
|
224
|
+
else if (input === '7') chosenFormat = 'all';
|
|
225
|
+
else chosenFormat = 'text';
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const snippets = {
|
|
230
|
+
text: generateMinimalTextSnippet(label, grade, discussionUrl),
|
|
231
|
+
html: generateHtmlBadgeSnippet(label, grade, discussionUrl),
|
|
232
|
+
vue: generateVueBadgeSnippet(label, grade, discussionUrl),
|
|
233
|
+
react: generateReactBadgeSnippet(label, grade, discussionUrl),
|
|
234
|
+
markdown: generateMarkdownBadgeSnippet(label, grade, discussionUrl),
|
|
235
|
+
svg: generateSvgBadgeSnippet(label, grade)
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
if (chosenFormat === 'all') {
|
|
239
|
+
for (const [key, content] of Object.entries(snippets)) {
|
|
240
|
+
process.stdout.write(`\x1b[1m\x1b[36m--- ${key.toUpperCase()} ---\x1b[0m\n${content}\n\n`);
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const outputSnippet = snippets[chosenFormat] || snippets.text;
|
|
246
|
+
process.stdout.write(`\n\x1b[1m\x1b[32m✔ Chemical X Footer Badge (${chosenFormat.toUpperCase()}):\x1b[0m\n\n`);
|
|
247
|
+
process.stdout.write(outputSnippet + '\n\n');
|
|
248
|
+
|
|
249
|
+
if (chosenFormat === 'svg') {
|
|
250
|
+
const saveSvg = path.resolve(process.cwd(), 'chemx-badge.svg');
|
|
251
|
+
fs.writeFileSync(saveSvg, outputSnippet, 'utf-8');
|
|
252
|
+
process.stdout.write(`\x1b[32m✔ Saved SVG badge asset to:\x1b[0m ${saveSvg}\n\n`);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const shouldCopy = isCopy || (hasGum() ? true : true);
|
|
256
|
+
if (shouldCopy) {
|
|
257
|
+
const success = copyToClipboard(outputSnippet);
|
|
258
|
+
if (success) {
|
|
259
|
+
process.stdout.write('\x1b[32m✔ Badge snippet copied to clipboard! Paste directly into your footer.\x1b[0m\n\n');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
export const toPascalCase = (str) =>
|
|
2
|
+
str
|
|
3
|
+
.replace(/^([a-z])-/, '')
|
|
4
|
+
.split('-')
|
|
5
|
+
.map((p) => p.charAt(0).toUpperCase() + p.slice(1))
|
|
6
|
+
.join('');
|
|
7
|
+
|
|
8
|
+
export const buildReactComponent = (name, pascalName) => `import React from 'react';
|
|
9
|
+
import type { ${pascalName}Props } from './types';
|
|
10
|
+
import { use${pascalName}Controller } from './${name}.controller';
|
|
11
|
+
|
|
12
|
+
export const ${pascalName}: React.FC<${pascalName}Props> = ({
|
|
13
|
+
title,
|
|
14
|
+
subtitle,
|
|
15
|
+
variant = 'standard',
|
|
16
|
+
onAction
|
|
17
|
+
}) => {
|
|
18
|
+
const { state, canProceed, descriptor, handleAction } = use${pascalName}Controller({
|
|
19
|
+
initialState: { status: 'idle' },
|
|
20
|
+
onAction
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<div className="${name}">
|
|
25
|
+
<div className="${name}__header">
|
|
26
|
+
<h3 className="${name}__title">{title}</h3>
|
|
27
|
+
{subtitle && <p className="${name}__subtitle">{subtitle}</p>}
|
|
28
|
+
<span className={descriptor.className}>{descriptor.text}</span>
|
|
29
|
+
</div>
|
|
30
|
+
<div className="${name}__body">
|
|
31
|
+
<button
|
|
32
|
+
type="button"
|
|
33
|
+
className="${name}__action"
|
|
34
|
+
disabled={!canProceed}
|
|
35
|
+
onClick={handleAction}
|
|
36
|
+
>
|
|
37
|
+
{variant}
|
|
38
|
+
</button>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default ${pascalName};
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
export const buildVueComponent = (name, pascalName) => `<script setup lang="ts">
|
|
48
|
+
import type { ${pascalName}Props, ${pascalName}Emits } from './types';
|
|
49
|
+
import { use${pascalName}Controller } from './${name}.controller';
|
|
50
|
+
|
|
51
|
+
const props = withDefaults(defineProps<${pascalName}Props>(), {
|
|
52
|
+
variant: 'standard'
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const emit = defineEmits<${pascalName}Emits>();
|
|
56
|
+
|
|
57
|
+
const { state, canProceed, descriptor, handleAction } = use${pascalName}Controller({
|
|
58
|
+
initialState: { status: 'idle' },
|
|
59
|
+
onAction: () => emit('action', props.title)
|
|
60
|
+
});
|
|
61
|
+
</script>
|
|
62
|
+
|
|
63
|
+
<template>
|
|
64
|
+
<div class="${name}">
|
|
65
|
+
<div class="${name}__header">
|
|
66
|
+
<h3 class="${name}__title">{{ title }}</h3>
|
|
67
|
+
<p v-if="subtitle" class="${name}__subtitle">{{ subtitle }}</p>
|
|
68
|
+
<span :class="descriptor.className">{{ descriptor.text }}</span>
|
|
69
|
+
</div>
|
|
70
|
+
<div class="${name}__body">
|
|
71
|
+
<button
|
|
72
|
+
type="button"
|
|
73
|
+
class="${name}__action"
|
|
74
|
+
:disabled="!canProceed"
|
|
75
|
+
@click="handleAction"
|
|
76
|
+
>
|
|
77
|
+
{{ variant }}
|
|
78
|
+
</button>
|
|
79
|
+
</div>
|
|
80
|
+
</div>
|
|
81
|
+
</template>
|
|
82
|
+
|
|
83
|
+
<style lang="scss" scoped>
|
|
84
|
+
@use './_${name}.scss';
|
|
85
|
+
</style>
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
export const buildSvelteComponent = (name, pascalName) => `<script lang="ts">
|
|
89
|
+
import type { ${pascalName}Props } from './types';
|
|
90
|
+
import { create${pascalName}Controller } from './${name}.controller';
|
|
91
|
+
|
|
92
|
+
const {
|
|
93
|
+
title,
|
|
94
|
+
subtitle = '',
|
|
95
|
+
variant = 'standard',
|
|
96
|
+
onAction
|
|
97
|
+
}: ${pascalName}Props = $props();
|
|
98
|
+
|
|
99
|
+
const controller = create${pascalName}Controller({
|
|
100
|
+
initialState: { status: 'idle' },
|
|
101
|
+
onAction
|
|
102
|
+
});
|
|
103
|
+
</script>
|
|
104
|
+
|
|
105
|
+
<div class="${name}">
|
|
106
|
+
<div class="${name}__header">
|
|
107
|
+
<h3 class="${name}__title">{title}</h3>
|
|
108
|
+
{#if subtitle}
|
|
109
|
+
<p class="${name}__subtitle">{subtitle}</p>
|
|
110
|
+
{/if}
|
|
111
|
+
<span class={controller.descriptor.className}>{controller.descriptor.text}</span>
|
|
112
|
+
</div>
|
|
113
|
+
<div class="${name}__body">
|
|
114
|
+
<button
|
|
115
|
+
type="button"
|
|
116
|
+
class="${name}__action"
|
|
117
|
+
disabled={!controller.canProceed}
|
|
118
|
+
onclick={controller.handleAction}
|
|
119
|
+
>
|
|
120
|
+
{variant}
|
|
121
|
+
</button>
|
|
122
|
+
</div>
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
<style lang="scss">
|
|
126
|
+
@use './_${name}.scss';
|
|
127
|
+
</style>
|
|
128
|
+
`;
|
|
129
|
+
|
|
130
|
+
export const buildController = (name, pascalName) => `import { useState, useMemo } from 'react';
|
|
131
|
+
import type { ${pascalName}State, ${pascalName}Descriptor } from './types';
|
|
132
|
+
|
|
133
|
+
interface ControllerOptions {
|
|
134
|
+
readonly initialState?: ${pascalName}State;
|
|
135
|
+
readonly onAction?: () => void;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export const use${pascalName}Controller = (options: ControllerOptions = {}) => {
|
|
139
|
+
const [state, setState] = useState<${pascalName}State>(
|
|
140
|
+
options.initialState || { status: 'idle' }
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const isIdle = state.status === 'idle';
|
|
144
|
+
const isPending = state.status === 'loading';
|
|
145
|
+
const canProceed = isIdle && !isPending;
|
|
146
|
+
|
|
147
|
+
const descriptor: ${pascalName}Descriptor = useMemo(() => {
|
|
148
|
+
if (state.status === 'loading') {
|
|
149
|
+
return { text: 'Loading...', className: '${name}__badge ${name}__badge--pending' };
|
|
150
|
+
}
|
|
151
|
+
if (state.status === 'active') {
|
|
152
|
+
return { text: 'Active', className: '${name}__badge ${name}__badge--active' };
|
|
153
|
+
}
|
|
154
|
+
return { text: 'Ready', className: '${name}__badge ${name}__badge--ready' };
|
|
155
|
+
}, [state.status]);
|
|
156
|
+
|
|
157
|
+
const handleAction = () => {
|
|
158
|
+
if (!canProceed) return;
|
|
159
|
+
setState({ status: 'active', activeId: 'item-1' });
|
|
160
|
+
options.onAction?.();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return { state, canProceed, descriptor, handleAction };
|
|
164
|
+
};
|
|
165
|
+
`;
|
|
166
|
+
|
|
167
|
+
export const buildTypes = (name, pascalName) => `export type ${pascalName}State =
|
|
168
|
+
| { readonly status: 'idle' }
|
|
169
|
+
| { readonly status: 'loading'; readonly progress: number }
|
|
170
|
+
| { readonly status: 'active'; readonly activeId: string }
|
|
171
|
+
| { readonly status: 'fault'; readonly faultMessage: string };
|
|
172
|
+
|
|
173
|
+
export interface ${pascalName}Descriptor {
|
|
174
|
+
readonly text: string;
|
|
175
|
+
readonly className: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface ${pascalName}Props {
|
|
179
|
+
readonly title: string;
|
|
180
|
+
readonly subtitle?: string;
|
|
181
|
+
readonly variant?: 'standard' | 'highlight';
|
|
182
|
+
readonly onAction?: () => void;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface ${pascalName}Emits {
|
|
186
|
+
(e: 'action', title: string): void;
|
|
187
|
+
}
|
|
188
|
+
`;
|
|
189
|
+
|
|
190
|
+
export const buildScss = (name) => `.${name} {
|
|
191
|
+
display: flex;
|
|
192
|
+
flex-direction: column;
|
|
193
|
+
padding: 16px;
|
|
194
|
+
background: rgba(19, 30, 58, 0.7);
|
|
195
|
+
backdrop-filter: blur(12px);
|
|
196
|
+
border: 1px solid rgba(98, 201, 255, 0.15);
|
|
197
|
+
border-radius: 8px;
|
|
198
|
+
|
|
199
|
+
&__header {
|
|
200
|
+
display: flex;
|
|
201
|
+
justify-content: space-between;
|
|
202
|
+
align-items: center;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
&__title {
|
|
206
|
+
margin: 0;
|
|
207
|
+
font-size: 16px;
|
|
208
|
+
color: #ffffff;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
&__subtitle {
|
|
212
|
+
margin: 4px 0 0;
|
|
213
|
+
font-size: 12px;
|
|
214
|
+
color: #94a3b8;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
&__badge {
|
|
218
|
+
font-size: 11px;
|
|
219
|
+
padding: 2px 8px;
|
|
220
|
+
border-radius: 4px;
|
|
221
|
+
background: #0b1329;
|
|
222
|
+
|
|
223
|
+
&--ready { color: #62c9ff; }
|
|
224
|
+
&--active { color: #4ade80; }
|
|
225
|
+
&--pending { color: #facc15; }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
&__body {
|
|
229
|
+
margin-top: 14px;
|
|
230
|
+
display: flex;
|
|
231
|
+
justify-content: flex-end;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
&__action {
|
|
235
|
+
padding: 6px 14px;
|
|
236
|
+
border-radius: 4px;
|
|
237
|
+
background: #1e293b;
|
|
238
|
+
border: 1px solid #334155;
|
|
239
|
+
color: #ffffff;
|
|
240
|
+
cursor: pointer;
|
|
241
|
+
|
|
242
|
+
&:hover:not(:disabled) {
|
|
243
|
+
background: #334155;
|
|
244
|
+
border-color: #62c9ff;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
&:disabled {
|
|
248
|
+
opacity: 0.5;
|
|
249
|
+
cursor: not-allowed;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
`;
|
|
254
|
+
|
|
255
|
+
export const buildIndex = (name, pascalName, ext) => {
|
|
256
|
+
const compExport = ext === 'vue' || ext === 'svelte'
|
|
257
|
+
? `export { default as ${pascalName} } from './${name}.${ext}';`
|
|
258
|
+
: `export { ${pascalName} } from './${name}';`;
|
|
259
|
+
|
|
260
|
+
return `${compExport}
|
|
261
|
+
export { use${pascalName}Controller } from './${name}.controller';
|
|
262
|
+
export type { ${pascalName}Props, ${pascalName}State } from './types';
|
|
263
|
+
`;
|
|
264
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface GeneratorOptions {
|
|
2
|
+
readonly name?: string;
|
|
3
|
+
readonly tier?: 'm' | 'a' | 'o' | 't';
|
|
4
|
+
readonly framework?: 'react' | 'vue' | 'svelte';
|
|
5
|
+
readonly dir?: string;
|
|
6
|
+
readonly isLean?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export declare function runGenerateWizard(rawArgs?: string[]): Promise<void>;
|
|
10
|
+
export declare function runGenerateCapsule(capsuleName: string): Promise<void>;
|
package/cli/generator.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { hasGum, gumChoose, gumInput, promptQuestion, renderBanner } from './terminal.js';
|
|
4
|
+
import { checkOrPromptEvaluation } from './license.js';
|
|
5
|
+
import {
|
|
6
|
+
toPascalCase,
|
|
7
|
+
buildReactComponent,
|
|
8
|
+
buildVueComponent,
|
|
9
|
+
buildSvelteComponent,
|
|
10
|
+
buildController,
|
|
11
|
+
buildTypes,
|
|
12
|
+
buildScss,
|
|
13
|
+
buildIndex
|
|
14
|
+
} from './generator-templates.js';
|
|
15
|
+
|
|
16
|
+
const TIERS = [
|
|
17
|
+
{ prefix: 'm-', label: '1. m- Molecule (Self-contained feature block < 100 lines - Recommended)', tier: 'm' },
|
|
18
|
+
{ prefix: 'a-', label: '2. a- Atom (Single foundational UI element)', tier: 'a' },
|
|
19
|
+
{ prefix: 'o-', label: '3. o- Organism (Complex module combining molecules)', tier: 'o' },
|
|
20
|
+
{ prefix: 't-', label: '4. t- Template (Structural layout blueprint)', tier: 't' }
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const FRAMEWORKS = [
|
|
24
|
+
{ id: 'react', ext: 'tsx', label: '1. React 19 (TSX + Controller Hook)', builder: buildReactComponent },
|
|
25
|
+
{ id: 'vue', ext: 'vue', label: '2. Vue 3.4+ (SFC <script setup lang="ts">)', builder: buildVueComponent },
|
|
26
|
+
{ id: 'svelte', ext: 'svelte', label: '3. Svelte 5 (Runes + {prop} Shorthand)', builder: buildSvelteComponent }
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const detectBaseDir = () => {
|
|
30
|
+
const candidates = ['src/components/molecules', 'src/components', 'components', 'src'];
|
|
31
|
+
for (const c of candidates) {
|
|
32
|
+
if (fs.existsSync(path.resolve(process.cwd(), c))) return c;
|
|
33
|
+
}
|
|
34
|
+
return '.';
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const runGenerateWizard = async (rawArgs = []) => {
|
|
38
|
+
renderBanner('Chemical X: Molecular Capsule Wizard');
|
|
39
|
+
await checkOrPromptEvaluation('generate capsule');
|
|
40
|
+
|
|
41
|
+
const useGum = hasGum();
|
|
42
|
+
const isYes = rawArgs.includes('-y') || rawArgs.includes('--yes');
|
|
43
|
+
|
|
44
|
+
const nameArg = rawArgs.find((a) => !a.startsWith('-') && a !== 'generate' && a !== 'capsule' && a !== 'add');
|
|
45
|
+
const frameworkArg = (rawArgs.find((a) => a.startsWith('--framework=')) || '').split('=')[1]
|
|
46
|
+
|| (rawArgs.includes('-f') ? rawArgs[rawArgs.indexOf('-f') + 1] : null);
|
|
47
|
+
const tierArg = (rawArgs.find((a) => a.startsWith('--tier=')) || '').split('=')[1];
|
|
48
|
+
const dirArg = (rawArgs.find((a) => a.startsWith('--dir=')) || '').split('=')[1];
|
|
49
|
+
const isLean = rawArgs.includes('--lean');
|
|
50
|
+
|
|
51
|
+
let rawName = nameArg;
|
|
52
|
+
if (!rawName) {
|
|
53
|
+
rawName = useGum
|
|
54
|
+
? gumInput('Capsule feature name (e.g. user-avatar, spark-kpi):', 'user-avatar')
|
|
55
|
+
: await promptQuestion('Capsule feature name [user-avatar]: ');
|
|
56
|
+
}
|
|
57
|
+
const cleanName = (rawName || 'user-avatar').trim().toLowerCase();
|
|
58
|
+
|
|
59
|
+
let selectedPrefix = 'm-';
|
|
60
|
+
const existingPrefixMatch = cleanName.match(/^([a-z])-+/);
|
|
61
|
+
if (tierArg) {
|
|
62
|
+
selectedPrefix = `${tierArg.replace(/[^a-z]/g, '')}-`;
|
|
63
|
+
} else if (!isYes && !existingPrefixMatch) {
|
|
64
|
+
const tierChoice = useGum
|
|
65
|
+
? gumChoose(TIERS.map((t) => t.label), 'Select Architectural Tier')
|
|
66
|
+
: await promptQuestion('Select Architectural Tier [1=m, 2=a, 3=o, 4=t] (default: 1): ');
|
|
67
|
+
const matched = TIERS.find((t) => tierChoice && (tierChoice.includes(t.label) || tierChoice.startsWith(t.tier) || tierChoice === t.prefix));
|
|
68
|
+
if (matched) selectedPrefix = matched.prefix;
|
|
69
|
+
} else if (existingPrefixMatch) {
|
|
70
|
+
selectedPrefix = existingPrefixMatch[0];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const baseSlug = cleanName.replace(/^([a-z])-/, '');
|
|
74
|
+
const capsuleName = `${selectedPrefix}${baseSlug}`;
|
|
75
|
+
const pascalName = toPascalCase(capsuleName);
|
|
76
|
+
|
|
77
|
+
let selectedFramework = FRAMEWORKS[0];
|
|
78
|
+
if (frameworkArg) {
|
|
79
|
+
const found = FRAMEWORKS.find((f) => f.id === frameworkArg.toLowerCase() || f.ext === frameworkArg.toLowerCase());
|
|
80
|
+
if (found) selectedFramework = found;
|
|
81
|
+
} else if (!isYes) {
|
|
82
|
+
const fwChoice = useGum
|
|
83
|
+
? gumChoose(FRAMEWORKS.map((f) => f.label), 'Select Framework Flavor')
|
|
84
|
+
: await promptQuestion('Select Framework Flavor [1=React, 2=Vue 3, 3=Svelte 5] (default: 1): ');
|
|
85
|
+
const found = FRAMEWORKS.find((f) => fwChoice && (fwChoice.includes(f.label) || fwChoice.toLowerCase().includes(f.id)));
|
|
86
|
+
if (found) selectedFramework = found;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const detectedDir = detectBaseDir();
|
|
90
|
+
let targetParent = dirArg || (isYes ? detectedDir : null);
|
|
91
|
+
if (!targetParent) {
|
|
92
|
+
const dirChoices = [
|
|
93
|
+
`1. Detected components directory (${detectedDir}/${capsuleName})`,
|
|
94
|
+
`2. Current working directory (./${capsuleName})`,
|
|
95
|
+
'3. Custom directory path'
|
|
96
|
+
];
|
|
97
|
+
const dirPick = useGum
|
|
98
|
+
? gumChoose(dirChoices, 'Select Destination Directory')
|
|
99
|
+
: await promptQuestion(`Destination Directory [1=${detectedDir}, 2=current, 3=custom] (default: 1): `);
|
|
100
|
+
|
|
101
|
+
if (dirPick && dirPick.startsWith('2.')) {
|
|
102
|
+
targetParent = '.';
|
|
103
|
+
} else if (dirPick && dirPick.startsWith('3.')) {
|
|
104
|
+
targetParent = useGum
|
|
105
|
+
? gumInput('Enter custom parent directory path:', detectedDir)
|
|
106
|
+
: await promptQuestion(`Enter custom parent directory path [${detectedDir}]: `);
|
|
107
|
+
} else {
|
|
108
|
+
targetParent = detectedDir;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const resolvedParent = path.resolve(process.cwd(), targetParent || '.');
|
|
113
|
+
const targetDir = path.resolve(resolvedParent, capsuleName);
|
|
114
|
+
|
|
115
|
+
if (fs.existsSync(targetDir)) {
|
|
116
|
+
process.stderr.write(`\x1b[31m✕ Error: Directory ${capsuleName} already exists at ${targetDir}.\x1b[0m\n`);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
121
|
+
|
|
122
|
+
const compFile = `${capsuleName}.${selectedFramework.ext}`;
|
|
123
|
+
const compContent = selectedFramework.builder(capsuleName, pascalName);
|
|
124
|
+
const typesContent = buildTypes(capsuleName, pascalName);
|
|
125
|
+
const indexContent = buildIndex(capsuleName, pascalName, selectedFramework.ext);
|
|
126
|
+
|
|
127
|
+
fs.writeFileSync(path.join(targetDir, compFile), compContent, 'utf-8');
|
|
128
|
+
fs.writeFileSync(path.join(targetDir, 'types.d.ts'), typesContent, 'utf-8');
|
|
129
|
+
fs.writeFileSync(path.join(targetDir, 'index.ts'), indexContent, 'utf-8');
|
|
130
|
+
|
|
131
|
+
const filesCreated = [compFile, 'types.d.ts', 'index.ts'];
|
|
132
|
+
|
|
133
|
+
if (!isLean) {
|
|
134
|
+
const controllerFile = `${capsuleName}.controller.ts`;
|
|
135
|
+
const scssFile = `_${capsuleName}.scss`;
|
|
136
|
+
fs.writeFileSync(path.join(targetDir, controllerFile), buildController(capsuleName, pascalName), 'utf-8');
|
|
137
|
+
fs.writeFileSync(path.join(targetDir, scssFile), buildScss(capsuleName), 'utf-8');
|
|
138
|
+
filesCreated.push(controllerFile, scssFile);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const relTargetDir = path.relative(process.cwd(), targetDir);
|
|
142
|
+
process.stdout.write(`\n\x1b[1m\x1b[32m✔ Successfully generated crystalline capsule:\x1b[0m \x1b[36m${relTargetDir}/\x1b[0m\n`);
|
|
143
|
+
for (const f of filesCreated) {
|
|
144
|
+
process.stdout.write(` \x1b[32m✔\x1b[0m ${f}\n`);
|
|
145
|
+
}
|
|
146
|
+
process.stdout.write('\n\x1b[2mChemical X Standards verified: < 100 lines per file, 2-stage booleans, zero inline styles.\x1b[0m\n\n');
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const runGenerateCapsule = async (capsuleName) => {
|
|
150
|
+
return runGenerateWizard([capsuleName, '-y']);
|
|
151
|
+
};
|