project-profile.ts
6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { emptyProfile, type ProjectProfile } from '../types/project-profile';
/**
* Runtime project profile holder. Service mode spawns one engine child per
* job, so a module-level value is safe and mirrors the existing runtime
* model/figma config pattern in `config.ts`. When no profile is set, prompts
* render nothing and the engine behaves exactly as before (generic output).
*/
let runtimeProfile: ProjectProfile | null = null;
export function setProjectProfile(profile: ProjectProfile | null): void {
runtimeProfile = profile;
}
export function getProjectProfile(): ProjectProfile | null {
return runtimeProfile;
}
export function clearProjectProfile(): void {
runtimeProfile = null;
}
/**
* Load a project profile from a target project root. It reads two optional
* files:
* - `coderio.profile.json` — the structured manifest (tokens / components /
* utils / conventions). Any missing field falls back to the empty template.
* - `CODERIO.md` — prose constraints; its content is merged into
* `conventions` when the JSON does not already define it.
*
* Returns `null` when neither file exists (so callers can skip silently).
*/
export function loadProjectProfile(rootDir: string): ProjectProfile | null {
const jsonPath = join(rootDir, 'coderio.profile.json');
const mdPath = join(rootDir, 'CODERIO.md');
const hasJson = existsSync(jsonPath);
const hasMd = existsSync(mdPath);
if (!hasJson && !hasMd) return null;
const base = emptyProfile();
let parsed: Partial<ProjectProfile> = {};
if (hasJson) {
try {
parsed = JSON.parse(readFileSync(jsonPath, 'utf8')) as Partial<ProjectProfile>;
} catch {
parsed = {};
}
}
const mdContent = hasMd ? readFileSync(mdPath, 'utf8') : '';
return {
...base,
...parsed,
projectName: parsed.projectName || base.projectName,
rootDir: parsed.rootDir || rootDir,
tokens: parsed.tokens ?? [],
components: parsed.components ?? [],
utils: parsed.utils ?? [],
conventions: parsed.conventions ?? mdContent,
};
}
/**
* Render the project profile into a compact, model-friendly constraint block
* injected into the CODE node system instructions. When the profile is empty
* (no components / tokens / utils / conventions) it returns an empty string so
* generation stays fully generic.
*/
export function renderProfileConstraint(profile: ProjectProfile): string {
const hasContent =
profile.components.length > 0 ||
profile.tokens.length > 0 ||
profile.utils.length > 0 ||
(profile.conventions && profile.conventions.trim().length > 0);
if (!hasContent) return '';
const lines: string[] = [];
const meta = [
profile.projectName && `Target: ${profile.projectName}`,
profile.framework && `Framework: ${profile.framework}`,
profile.language && `Language: ${profile.language}`,
profile.styling && `Styling: ${profile.styling}`,
profile.componentLibrary && `Components: ${profile.componentLibrary}`,
profile.stateManagement && `State: ${profile.stateManagement}`,
profile.dataFetching && `Data: ${profile.dataFetching}`,
profile.routing && `Routing: ${profile.routing}`,
profile.importAlias && `Alias: ${profile.importAlias}`,
profile.landingPath && `Landing: ${profile.landingPath}`,
]
.filter(Boolean)
.join(' | ');
if (meta) lines.push(` <meta>${meta}</meta>`);
if (profile.tokens.length > 0) {
lines.push(' <tokens>');
for (const t of profile.tokens) {
const val = t.value ? ` (raw ${t.value})` : '';
lines.push(
` - ${t.category} ${t.name} => use \`${t.code}\`${val}`,
);
}
lines.push(' </tokens>');
}
if (profile.components.length > 0) {
lines.push(' <components>');
for (const c of profile.components) {
const props = c.props && c.props.length ? ` (props: ${c.props.join(', ')})` : '';
const desc = c.description ? ` — ${c.description}` : '';
const variantNote =
c.variants && Object.keys(c.variants).length
? ` [variants: ${Object.entries(c.variants)
.map(([k, v]) => `${k}=${v}`)
.join(', ')}]`
: '';
const kind = c.exportType === 'named' ? '{ ' + c.name + ' }' : c.name;
lines.push(
` - ${c.name} => import ${kind} from "${c.importPath}"${props}${desc}${variantNote}`,
);
}
lines.push(' </components>');
}
if (profile.utils.length > 0) {
lines.push(' <utils>');
for (const u of profile.utils) {
const desc = u.description ? ` — ${u.description}` : '';
lines.push(` - ${u.name} => import { ${u.name} } from "${u.importPath}"${desc}`);
}
lines.push(' </utils>');
}
if (profile.conventions && profile.conventions.trim().length > 0) {
lines.push(' <conventions>');
for (const ln of profile.conventions.trim().split('\n')) {
lines.push(` ${ln}`);
}
lines.push(' </conventions>');
}
return [
'',
' <project_profile>',
...lines,
' </project_profile>',
'',
' <profile_rules>',
' - You MUST prefer the components, utils, and design tokens listed in <project_profile>.',
' - Use the EXACT import paths given. Never invent a different path for a listed component.',
' - Reference design tokens via their code expression (e.g. `theme.colors.brand`) instead of hardcoding raw values like `#2563eb`.',
' - When a Figma node clearly maps to a listed component, use it; otherwise fall back to primitives and add a `// TODO: replace with <ProjectComponent>` comment.',
' - Follow the <conventions> rules strictly (naming, directory, state library, i18n, etc.).',
' </profile_rules>',
].join('\n');
}