val.ts
3.81 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
import * as fs from 'node:fs';
import * as path from 'node:path';
import { Command } from 'commander';
import type { Protocol } from '../types/protocol-types';
import { ValidationConfig, ValidationMode } from '../types/graph-types';
import { logger } from '../utils/logger';
import { runValidation } from '../nodes/validation';
import { workspaceManager } from '../utils/workspace';
import { WorkspaceStructure } from '../types';
type ValCommandOptions = {
workspace?: string;
url: string;
reportonly?: boolean;
};
function readJsonFile<T>(absolutePath: string): T {
const raw = fs.readFileSync(absolutePath, 'utf-8');
return JSON.parse(raw) as T;
}
/**
* Register the 'validate' CLI command.
* Validates position alignment using an existing workspace without re-running d2p/d2c.
*
* @param program - Commander program instance
*/
export function registerValidateCommand(program: Command): void {
program
.command('validate')
.alias('val')
.description('Validate position misalignment using an existing generated workspace')
.option(
'-w, --workspace [path]',
'Workspace root path (contains process/ and generated app folder). Defaults to coderio/<current-dir> if not specified'
)
.requiredOption('-u, --url <url>', 'Figma thumbnail URL for validation')
.option('--reportonly', 'Run report-only validation (no code edits)', false)
.action(async (opts: ValCommandOptions) => {
try {
// Determine workspace and project name
let workspace: WorkspaceStructure;
let projectName: string;
if (opts.workspace) {
// Use explicit workspace path
const workspacePath = path.resolve(opts.workspace);
const parentPath = path.dirname(path.dirname(workspacePath)); // Go up to parent of 'coderio' folder
projectName = path.basename(workspacePath);
workspace = workspaceManager.initWorkspace(projectName, parentPath);
} else {
// Default: use current directory name as project name
projectName = path.basename(process.env.CODERIO_CLI_USER_CWD ?? process.cwd());
workspace = workspaceManager.initWorkspace(projectName);
}
const protocolPath = path.join(workspace.process, 'protocol.json');
if (!fs.existsSync(protocolPath)) {
throw new Error(`Missing protocol at: ${protocolPath}. Run d2p/d2c first to generate process/protocol.json.`);
}
const protocol = readJsonFile<Protocol>(protocolPath);
// Use the provided URL directly
const figmaThumbnailUrl = opts.url;
// Build ValidationConfig from CLI options
const config: ValidationConfig = {
validationMode: opts.reportonly ? ValidationMode.ReportOnly : ValidationMode.Full,
};
logger.printInfoLog(`正在对工作区 ${workspace.root} 运行校验(模式:${config.validationMode})`);
await runValidation({
urlInfo: { fileId: null, name: projectName, nodeId: null, projectName: null },
workspace,
figmaInfo: { thumbnail: figmaThumbnailUrl },
protocol,
messages: [],
config,
fillContainer: true,
});
// If we reach here, validation passed
process.exit(0);
} catch (error) {
logger.printErrorLog(`val 执行出错:${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
});
}