d2c.ts
2.43 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
import { Command } from 'commander';
import { logger } from '../utils/logger';
import { design2code } from '../graph';
import { ValidationMode } from '../types/graph-types';
import { patchEvoltagentStreamingForceOff } from '../utils/patch-evoltagent';
/**
* Validate and map CLI mode parameter to internal validationMode.
* @param mode - The mode string from CLI
* @returns The mapped ValidationMode, or null if invalid
*/
function getValidationMode(mode: string): ValidationMode | null {
const validModes = ['code', 'with-report', 'full'] as const;
type ValidMode = (typeof validModes)[number];
const isValidMode = (m: string): m is ValidMode => {
return validModes.includes(m as ValidMode);
};
if (!isValidMode(mode)) {
return null;
}
// Map CLI mode to internal validationMode
const modeMap: Record<ValidMode, ValidationMode> = {
code: ValidationMode.CodeOnly,
'with-report': ValidationMode.ReportOnly,
full: ValidationMode.Full,
};
return modeMap[mode];
}
// d2c command: Design to Code
export const registerD2CCommand = (program: Command) => {
program
.command('design2code')
.alias('d2c')
.description('Generate frontend code from design')
.option('-s, --source <url>', 'Figma Link')
.option(
'-m, --mode [type]',
'Execution mode: code (generate component code only), with-report (single validation and generate report), full (iterative validation)',
'code'
)
.action(async (opts: { source: string; mode?: string }) => {
try {
// Force evoltagent agents to use non-streaming calls (see patch module).
patchEvoltagentStreamingForceOff();
const { source, mode = 'code' } = opts;
const validationMode = getValidationMode(mode);
if (!validationMode) {
logger.printErrorLog(`Invalid mode: ${mode}. Must be one of: code, with-report, full`);
process.exit(1);
}
await design2code(source, validationMode);
logger.printSuccessLog('Successfully completed code generation from design! Happy coding!');
} catch (error) {
logger.printErrorLog(`Error during d2c execution: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
});
};