p2c.ts
1.71 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
import { Command } from 'commander';
import { workspaceManager } from '../utils/workspace';
import { logger } from '../utils/logger';
import { readFile } from 'fs/promises';
import { generateCode } from '../nodes/code';
import { Protocol } from '../types/protocol-types';
import { GraphState } from '../state';
import { initialProject } from '../nodes/initial';
// p2c command: Protocol to Code
export const registerP2CCommand = (program: Command) => {
program
.command('protocol2code')
.alias('p2c')
.description('Generate code from protocol')
.option('-p, --protocol <path>', 'Protocol path')
.action(async (opts: { protocol: string }) => {
try {
const protocolContent = await readFile(opts.protocol, 'utf-8');
const protocolData = JSON.parse(protocolContent) as Protocol;
const workspace = workspaceManager.initWorkspace(protocolData.id || '');
const state: GraphState = {
protocol: protocolData,
workspace,
figmaInfo: { thumbnail: '' },
urlInfo: {
fileId: '',
name: '',
nodeId: '',
projectName: '',
},
messages: [],
config: {},
fillContainer: true,
};
await initialProject(state);
await generateCode(state);
} catch (error) {
logger.printErrorLog(`Error during p2c execution: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
});
};