scaffold-engine 0.1.0

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.
Files changed (52) hide show
  1. package/README.md +117 -0
  2. package/engine.project.example.json +23 -0
  3. package/package.json +49 -0
  4. package/scripts/postinstall.cjs +42 -0
  5. package/specs/catalogs/action-templates.yaml +189 -0
  6. package/specs/catalogs/child-templates.yaml +54 -0
  7. package/specs/catalogs/field-fragments.yaml +203 -0
  8. package/specs/catalogs/object-catalog.yaml +35 -0
  9. package/specs/catalogs/object-name-suggestions.yaml +30 -0
  10. package/specs/catalogs/object-templates.yaml +45 -0
  11. package/specs/catalogs/pattern-catalog.yaml +48 -0
  12. package/specs/catalogs/status-templates.yaml +16 -0
  13. package/specs/projects/crm-pilot/customer.yaml +122 -0
  14. package/specs/projects/crm-pilot/lead.from-nl.yaml +76 -0
  15. package/specs/projects/crm-pilot/lead.yaml +82 -0
  16. package/specs/projects/generated-from-nl/crm-customer.yaml +158 -0
  17. package/specs/projects/generated-from-nl/crm-lead.yaml +76 -0
  18. package/specs/projects/generated-from-nl/crm-opportunity.yaml +78 -0
  19. package/specs/projects/generated-from-nl/crm-quote.yaml +78 -0
  20. package/specs/projects/generated-from-nl/custom-documentLines.yaml +125 -0
  21. package/specs/projects/generated-from-nl/custom-treeEntity.yaml +78 -0
  22. package/specs/projects/generated-from-nl/erp-material-pattern-test.yaml +79 -0
  23. package/specs/projects/generated-from-nl/erp-material.yaml +78 -0
  24. package/specs/projects/generated-from-nl/hr-orgUnit.yaml +100 -0
  25. package/specs/projects/pattern-examples/document-lines-demo.yaml +125 -0
  26. package/specs/projects/pattern-examples/tree-entity-demo.yaml +79 -0
  27. package/specs/rules/business-model.schema.json +262 -0
  28. package/specs/rules/extension-boundaries.json +26 -0
  29. package/specs/rules/requirement-draft.schema.json +75 -0
  30. package/specs/rules/spec-governance.json +29 -0
  31. package/specs/templates/crm/customer.template.yaml +121 -0
  32. package/specs/templates/crm/lead.template.yaml +82 -0
  33. package/tools/analyze-requirement.cjs +950 -0
  34. package/tools/cli.cjs +59 -0
  35. package/tools/create-draft.cjs +18 -0
  36. package/tools/engine.cjs +47 -0
  37. package/tools/generate-draft.cjs +33 -0
  38. package/tools/generate-module.cjs +1218 -0
  39. package/tools/init-project.cjs +194 -0
  40. package/tools/lib/draft-toolkit.cjs +357 -0
  41. package/tools/lib/model-toolkit.cjs +482 -0
  42. package/tools/lib/pattern-renderers.cjs +166 -0
  43. package/tools/lib/renderers/detail-page-renderer.cjs +327 -0
  44. package/tools/lib/renderers/form-page-renderer.cjs +553 -0
  45. package/tools/lib/renderers/list-page-renderer.cjs +371 -0
  46. package/tools/lib/runtime-config.cjs +154 -0
  47. package/tools/patch-draft.cjs +57 -0
  48. package/tools/prompts/business-model-prompt.md +58 -0
  49. package/tools/run-requirement.cjs +672 -0
  50. package/tools/validate-draft.cjs +32 -0
  51. package/tools/validate-model.cjs +140 -0
  52. package/tools/verify-patterns.cjs +67 -0
@@ -0,0 +1,672 @@
1
+ #!/usr/bin/env node
2
+ const path = require('path');
3
+ const { spawn, spawnSync } = require('child_process');
4
+ const { analyzeRequirement, parseArgs, readRequirementText, renderMarkdown, writeSuggestedSpec } = require('./analyze-requirement.cjs');
5
+ const { loadRuntimeConfig, resolveScaffoldPath, validateModelFile } = require('./lib/model-toolkit.cjs');
6
+ const { resolveGeneratedSpecPath } = require('./lib/runtime-config.cjs');
7
+
8
+ const options = parseRunnerArgs(process.argv.slice(2));
9
+
10
+ class PipelineError extends Error {
11
+ constructor(stage, code, message, details = {}, exitCode = 1) {
12
+ super(message);
13
+ this.name = 'PipelineError';
14
+ this.stage = stage;
15
+ this.code = code;
16
+ this.details = details;
17
+ this.exitCode = exitCode;
18
+ }
19
+ }
20
+
21
+ (async () => {
22
+ try {
23
+ const runtimeConfig = loadRuntimeConfig({ configPath: options.configPath });
24
+ const requirementText = readRequirementText(options);
25
+ const analysis = analyzeRequirement(requirementText, { configPath: options.configPath });
26
+
27
+ if (analysis.decision === 'not_extendable') {
28
+ console.log(renderMarkdown(analysis));
29
+ process.exit(2);
30
+ }
31
+
32
+ if (analysis.decision === 'need_more_info') {
33
+ if (!analysis.suggestedModel) {
34
+ console.log(renderMarkdown(analysis));
35
+ process.exit(3);
36
+ }
37
+
38
+ const fallbackSpecPath = options.writeSpec
39
+ ? path.resolve(options.writeSpec)
40
+ : buildDefaultSpecPath(analysis, runtimeConfig);
41
+
42
+ writeSuggestedSpec(analysis.suggestedModel, fallbackSpecPath);
43
+ const fallbackValidation = validateModelFile(fallbackSpecPath, runtimeConfig);
44
+
45
+ if (options.format === 'json') {
46
+ console.log(JSON.stringify({
47
+ decision: analysis.decision,
48
+ nextAction: analysis.nextAction,
49
+ requirement: analysis.requirement,
50
+ specPath: fallbackSpecPath,
51
+ inferred: analysis.inferred,
52
+ missingInfo: analysis.missingInfo || [],
53
+ reason: analysis.reason || [],
54
+ validation: {
55
+ valid: fallbackValidation.valid,
56
+ schemaErrors: fallbackValidation.schemaErrors || [],
57
+ boundaryErrors: fallbackValidation.boundaryErrors || [],
58
+ governanceErrors: fallbackValidation.governanceErrors || [],
59
+ warnings: fallbackValidation.warnings || [],
60
+ location: fallbackValidation.location || null,
61
+ },
62
+ }, null, 2));
63
+ } else {
64
+ console.log(renderFallbackMarkdown({
65
+ ...analysis,
66
+ generatedSpecPath: fallbackSpecPath,
67
+ validation: fallbackValidation,
68
+ }));
69
+ }
70
+ process.exit(0);
71
+ }
72
+
73
+ const specPath = options.writeSpec
74
+ ? path.resolve(options.writeSpec)
75
+ : buildDefaultSpecPath(analysis, runtimeConfig);
76
+
77
+ writeSuggestedSpec(analysis.suggestedModel, specPath);
78
+
79
+ const validation = validateModelFile(specPath, runtimeConfig);
80
+ if (!validation.valid) {
81
+ throw new PipelineError(
82
+ 'validate',
83
+ 'MODEL_VALIDATION_FAILED',
84
+ '模型校验未通过',
85
+ {
86
+ specPath,
87
+ validation: {
88
+ valid: validation.valid,
89
+ schemaErrors: validation.schemaErrors || [],
90
+ boundaryErrors: validation.boundaryErrors || [],
91
+ governanceErrors: validation.governanceErrors || [],
92
+ warnings: validation.warnings || [],
93
+ location: validation.location || null,
94
+ },
95
+ analysis,
96
+ }
97
+ );
98
+ }
99
+
100
+ const pipeline = [];
101
+ const generateResult = runGenerate(specPath, options.force, options.configPath, runtimeConfig);
102
+ pipeline.push({
103
+ step: 'generate',
104
+ status: 'passed',
105
+ summary: `已生成 ${generateResult.files.length} 个文件`,
106
+ });
107
+
108
+ if (!options.skipCodegen) {
109
+ const codegenResult = runPnpmCommand(['-F', '@scaffold/api', 'codegen'], 'API client codegen', runtimeConfig);
110
+ pipeline.push({
111
+ step: 'codegen',
112
+ status: 'passed',
113
+ summary: 'API client 已更新',
114
+ output: codegenResult.stdout,
115
+ });
116
+ }
117
+
118
+ if (!options.skipInitDb) {
119
+ const initDbResult = runPnpmCommand(['--dir', 'scaffold', 'init-db'], '数据库初始化', runtimeConfig);
120
+ pipeline.push({
121
+ step: 'init-db',
122
+ status: 'passed',
123
+ summary: '数据库初始化完成',
124
+ output: initDbResult.stdout,
125
+ });
126
+ }
127
+
128
+ let smokeResult = null;
129
+ if (!options.skipSmoke) {
130
+ smokeResult = await runSmokeCheck(analysis, options, runtimeConfig);
131
+ pipeline.push({
132
+ step: 'smoke',
133
+ status: 'passed',
134
+ summary: `接口冒烟通过: ${smokeResult.endpoint}`,
135
+ output: JSON.stringify(smokeResult.response, null, 2),
136
+ });
137
+ }
138
+
139
+ const summary = {
140
+ ...analysis,
141
+ generatedSpecPath: specPath,
142
+ generatedFiles: generateResult.files,
143
+ generateOutput: generateResult.stdout,
144
+ pipeline,
145
+ smokeResult,
146
+ };
147
+
148
+ if (options.format === 'json') {
149
+ console.log(JSON.stringify({
150
+ decision: summary.decision,
151
+ nextAction: 'implement',
152
+ requirement: summary.requirement,
153
+ specPath: summary.generatedSpecPath,
154
+ inferred: summary.inferred,
155
+ generatedFiles: summary.generatedFiles,
156
+ pipeline: summary.pipeline,
157
+ smoke: summary.smokeResult,
158
+ warnings: summary.warnings || [],
159
+ }, null, 2));
160
+ } else {
161
+ console.log(renderRunMarkdown(summary));
162
+ }
163
+ } catch (error) {
164
+ const normalized = normalizeRunError(error);
165
+ if (options.format === 'json') {
166
+ console.log(JSON.stringify(normalized, null, 2));
167
+ } else {
168
+ console.error(renderRunFailureMarkdown(normalized));
169
+ }
170
+ process.exit(normalized.exitCode);
171
+ }
172
+ })();
173
+
174
+ function parseRunnerArgs(argv) {
175
+ const parsed = parseArgs(stripRunnerFlags(argv));
176
+ return {
177
+ ...parsed,
178
+ configPath: readStringArg(argv, '--config'),
179
+ force: !argv.includes('--no-force'),
180
+ skipCodegen: argv.includes('--skip-codegen'),
181
+ skipInitDb: argv.includes('--skip-init-db'),
182
+ skipSmoke: argv.includes('--skip-smoke'),
183
+ seedSmoke: argv.includes('--seed-smoke'),
184
+ smokePort: readNumberArg(argv, '--smoke-port') || 3011,
185
+ };
186
+ }
187
+
188
+ function buildDefaultSpecPath(analysis, runtimeConfig) {
189
+ const domain = analysis.inferred.domain || 'custom';
190
+ const object = analysis.inferred.object || analysis.inferred.pattern || 'suggested-model';
191
+ return resolveGeneratedSpecPath(runtimeConfig, `${domain}-${object}.yaml`);
192
+ }
193
+
194
+ function runGenerate(specPath, force, configPath, runtimeConfig) {
195
+ const commandArgs = [path.join(resolveScaffoldPath('tools'), 'generate-module.cjs'), specPath];
196
+ if (force) {
197
+ commandArgs.push('--force');
198
+ }
199
+ if (configPath) {
200
+ commandArgs.push('--config', path.resolve(configPath));
201
+ }
202
+
203
+ const result = spawnSync(process.execPath, commandArgs, {
204
+ cwd: runtimeConfig.targets.toolCwd,
205
+ encoding: 'utf8',
206
+ });
207
+
208
+ if (result.status !== 0) {
209
+ throw new PipelineError('generate', 'MODULE_GENERATE_FAILED', result.stderr || result.stdout || '模块生成失败', {
210
+ specPath,
211
+ stdout: result.stdout || '',
212
+ stderr: result.stderr || '',
213
+ });
214
+ }
215
+
216
+ const files = String(result.stdout || '')
217
+ .split('\n')
218
+ .map((line) => line.trim())
219
+ .filter((line) => line.startsWith('- ['))
220
+ .map((line) => line.replace(/^- \[[^\]]+\]\s+/, ''));
221
+
222
+ return {
223
+ stdout: result.stdout || '',
224
+ files,
225
+ };
226
+ }
227
+
228
+ function runPnpmCommand(args, label, runtimeConfig) {
229
+ const result = spawnSync('pnpm', args, {
230
+ cwd: runtimeConfig.targets.pnpmCwd,
231
+ encoding: 'utf8',
232
+ });
233
+
234
+ if (result.status !== 0) {
235
+ const stage = resolvePnpmStage(args);
236
+ throw new PipelineError(stage, `${stage.toUpperCase()}_FAILED`, `${label}失败`, {
237
+ command: ['pnpm', ...args].join(' '),
238
+ stdout: result.stdout || '',
239
+ stderr: result.stderr || '',
240
+ });
241
+ }
242
+
243
+ return {
244
+ stdout: result.stdout || '',
245
+ stderr: result.stderr || '',
246
+ };
247
+ }
248
+
249
+ async function runSmokeCheck(analysis, options, runtimeConfig) {
250
+ const apiDir = runtimeConfig.targets.apiPackageDir;
251
+ const port = options.smokePort;
252
+ const baseUrl = `http://127.0.0.1:${port}/api/${analysis.inferred.domain}/${analysis.inferred.object}`;
253
+ const endpoint = `http://127.0.0.1:${port}/api/${analysis.inferred.domain}/${analysis.inferred.object}/page?pageNo=1&pageSize=1`;
254
+ const env = {
255
+ ...process.env,
256
+ PORT: String(port),
257
+ };
258
+ const server = spawn(process.execPath, ['--import', '@swc-node/register/esm-register', 'src/server.ts'], {
259
+ cwd: apiDir,
260
+ env,
261
+ stdio: ['ignore', 'pipe', 'pipe'],
262
+ });
263
+
264
+ const logs = [];
265
+ const collectLogs = (chunk) => {
266
+ if (chunk) {
267
+ logs.push(String(chunk));
268
+ }
269
+ };
270
+
271
+ server.stdout.on('data', collectLogs);
272
+ server.stderr.on('data', collectLogs);
273
+
274
+ try {
275
+ await waitForServer(endpoint, server, logs);
276
+ let seededRecord = null;
277
+ if (options.seedSmoke) {
278
+ seededRecord = await createSmokeSeed(baseUrl, analysis);
279
+ }
280
+ const response = await fetchJson(endpoint);
281
+ return { port, endpoint, response, seededRecord };
282
+ } finally {
283
+ server.kill('SIGTERM');
284
+ await waitForExit(server, 2000);
285
+ }
286
+ }
287
+
288
+ function readStringArg(argv, flag) {
289
+ const index = argv.indexOf(flag);
290
+ if (index === -1) {
291
+ return '';
292
+ }
293
+ return argv[index + 1] || '';
294
+ }
295
+
296
+ async function waitForServer(endpoint, server, logs) {
297
+ const deadline = Date.now() + 15000;
298
+ while (Date.now() < deadline) {
299
+ if (server.exitCode !== null) {
300
+ throw new PipelineError('smoke', 'SMOKE_SERVER_START_FAILED', '冒烟服务启动失败', {
301
+ endpoint,
302
+ logs: logs.join(''),
303
+ });
304
+ }
305
+
306
+ try {
307
+ await fetchJson(endpoint);
308
+ return;
309
+ } catch (error) {
310
+ await sleep(300);
311
+ }
312
+ }
313
+
314
+ throw new PipelineError('smoke', 'SMOKE_TIMEOUT', '冒烟启动超时', {
315
+ endpoint,
316
+ logs: logs.join(''),
317
+ });
318
+ }
319
+
320
+ async function fetchJson(url) {
321
+ const response = await fetch(url);
322
+ if (!response.ok) {
323
+ throw new Error(`HTTP ${response.status} ${url}`);
324
+ }
325
+ return response.json();
326
+ }
327
+
328
+ async function postJson(url, body) {
329
+ const response = await fetch(url, {
330
+ method: 'POST',
331
+ headers: {
332
+ 'content-type': 'application/json',
333
+ },
334
+ body: JSON.stringify(body),
335
+ });
336
+
337
+ if (!response.ok) {
338
+ const text = await response.text();
339
+ throw new Error(`HTTP ${response.status} ${url}\n${text}`.trim());
340
+ }
341
+
342
+ return response.json();
343
+ }
344
+
345
+ function waitForExit(child, timeoutMs) {
346
+ return new Promise((resolve) => {
347
+ let settled = false;
348
+ const finish = () => {
349
+ if (!settled) {
350
+ settled = true;
351
+ resolve();
352
+ }
353
+ };
354
+ child.once('exit', finish);
355
+ setTimeout(finish, timeoutMs);
356
+ });
357
+ }
358
+
359
+ function sleep(ms) {
360
+ return new Promise((resolve) => setTimeout(resolve, ms));
361
+ }
362
+
363
+ function readNumberArg(argv, flag) {
364
+ const index = argv.indexOf(flag);
365
+ if (index < 0) {
366
+ return 0;
367
+ }
368
+ const next = argv[index + 1];
369
+ return next ? Number(next) : 0;
370
+ }
371
+
372
+ function renderRunMarkdown(result) {
373
+ const lines = [];
374
+ lines.push(`需求: ${result.requirement}`);
375
+ lines.push('');
376
+ lines.push('decision: extendable');
377
+ lines.push('nextAction: implement');
378
+ lines.push('');
379
+ lines.push('识别结果:');
380
+ lines.push(`- domain: ${result.inferred.domain}`);
381
+ lines.push(`- object: ${result.inferred.object}`);
382
+ lines.push(`- label: ${result.inferred.label}`);
383
+ lines.push(`- actions: ${(result.inferred.actions || []).join(', ') || '-'}`);
384
+ lines.push('');
385
+ lines.push('结果:');
386
+ lines.push(`- specPath: ${result.generatedSpecPath}`);
387
+ lines.push(`- generatedFiles: ${result.generatedFiles.length}`);
388
+ for (const file of result.generatedFiles) {
389
+ lines.push(`- ${file}`);
390
+ }
391
+
392
+ if (result.pipeline && result.pipeline.length > 0) {
393
+ lines.push('');
394
+ lines.push('流水线:');
395
+ for (const item of result.pipeline) {
396
+ lines.push(`- ${item.step}: ${item.status} (${item.summary})`);
397
+ }
398
+ }
399
+
400
+ if (result.smokeResult) {
401
+ const smokeSummary = summarizeSmokeResponse(result.smokeResult.response);
402
+ lines.push('');
403
+ lines.push('冒烟结果:');
404
+ lines.push(`- endpoint: ${result.smokeResult.endpoint}`);
405
+ if (result.smokeResult.seededRecord) {
406
+ lines.push(`- seeded: yes`);
407
+ }
408
+ lines.push(`- records: ${smokeSummary.records}`);
409
+ lines.push(`- total: ${smokeSummary.total}`);
410
+ }
411
+
412
+ if (result.warnings && result.warnings.length > 0) {
413
+ lines.push('');
414
+ lines.push('提示:');
415
+ for (const item of result.warnings) {
416
+ lines.push(`- ${item}`);
417
+ }
418
+ }
419
+
420
+ return `${lines.join('\n')}\n`;
421
+ }
422
+
423
+ function renderFallbackMarkdown(result) {
424
+ const lines = [];
425
+ lines.push(`需求: ${result.requirement}`);
426
+ lines.push('');
427
+ lines.push(`decision: ${result.decision}`);
428
+ lines.push(`nextAction: ${result.nextAction}`);
429
+ lines.push('');
430
+ lines.push('识别结果:');
431
+ lines.push(`- domain: ${result.inferred?.domain || '-'}`);
432
+ lines.push(`- object: ${result.inferred?.object || '-'}`);
433
+ lines.push(`- label: ${result.inferred?.label || '-'}`);
434
+ lines.push(`- pattern: ${result.inferred?.pattern || '-'}`);
435
+ lines.push(`- actions: ${(result.inferred?.actions || []).join(', ') || '-'}`);
436
+ lines.push('');
437
+ lines.push('结果:');
438
+ lines.push(`- specPath: ${result.generatedSpecPath}`);
439
+ lines.push(`- validation: ${result.validation?.valid ? 'passed' : 'failed'}`);
440
+ if (result.validation?.location) {
441
+ lines.push(`- scope: ${result.validation.location.scope}`);
442
+ }
443
+
444
+ if (result.reason && result.reason.length > 0) {
445
+ lines.push('');
446
+ lines.push('原因:');
447
+ for (const item of result.reason) {
448
+ lines.push(`- ${item}`);
449
+ }
450
+ }
451
+
452
+ if (result.missingInfo && result.missingInfo.length > 0) {
453
+ lines.push('');
454
+ lines.push('缺失信息:');
455
+ for (const item of result.missingInfo) {
456
+ lines.push(`- ${item}`);
457
+ }
458
+ }
459
+
460
+ const schemaErrors = result.validation?.schemaErrors || [];
461
+ const boundaryErrors = result.validation?.boundaryErrors || [];
462
+ const governanceErrors = result.validation?.governanceErrors || [];
463
+ if (schemaErrors.length > 0 || boundaryErrors.length > 0 || governanceErrors.length > 0) {
464
+ lines.push('');
465
+ lines.push('校验问题:');
466
+ for (const item of [...schemaErrors, ...boundaryErrors, ...governanceErrors]) {
467
+ lines.push(`- ${item}`);
468
+ }
469
+ }
470
+
471
+ const warnings = [
472
+ ...((result.warnings || [])),
473
+ ...((result.validation?.warnings || [])),
474
+ ];
475
+ if (warnings.length > 0) {
476
+ lines.push('');
477
+ lines.push('提示:');
478
+ for (const item of warnings) {
479
+ lines.push(`- ${item}`);
480
+ }
481
+ }
482
+
483
+ return `${lines.join('\n')}\n`;
484
+ }
485
+
486
+ function stripRunnerFlags(argv) {
487
+ const result = [];
488
+ for (let index = 0; index < argv.length; index += 1) {
489
+ const item = argv[index];
490
+ if (['--no-force', '--skip-codegen', '--skip-init-db', '--skip-smoke', '--seed-smoke'].includes(item)) {
491
+ continue;
492
+ }
493
+ if (item === '--smoke-port' || item === '--config') {
494
+ index += 1;
495
+ continue;
496
+ }
497
+ result.push(item);
498
+ }
499
+ return result;
500
+ }
501
+
502
+ function summarizeSmokeResponse(response) {
503
+ const payload = response && typeof response === 'object' && response.data && typeof response.data === 'object'
504
+ ? response.data
505
+ : response;
506
+
507
+ return {
508
+ records: Array.isArray(payload?.records) ? payload.records.length : '-',
509
+ total: payload?.total ?? '-',
510
+ };
511
+ }
512
+
513
+ function resolvePnpmStage(args) {
514
+ const joined = args.join(' ');
515
+ if (joined.includes('codegen')) {
516
+ return 'codegen';
517
+ }
518
+ if (joined.includes('init-db')) {
519
+ return 'init-db';
520
+ }
521
+ return 'pipeline';
522
+ }
523
+
524
+ function normalizeRunError(error) {
525
+ if (error instanceof PipelineError) {
526
+ return {
527
+ contractVersion: '1.0.0',
528
+ stage: 'run',
529
+ status: 'failed',
530
+ errorStage: error.stage,
531
+ code: error.code,
532
+ message: error.message,
533
+ details: error.details || {},
534
+ exitCode: error.exitCode ?? 1,
535
+ };
536
+ }
537
+
538
+ return {
539
+ contractVersion: '1.0.0',
540
+ stage: 'run',
541
+ status: 'failed',
542
+ errorStage: 'unknown',
543
+ code: 'UNEXPECTED_ERROR',
544
+ message: error?.message || '一键执行失败',
545
+ details: {},
546
+ exitCode: 1,
547
+ };
548
+ }
549
+
550
+ function renderRunFailureMarkdown(error) {
551
+ const lines = [];
552
+ lines.push(`❌ 一键执行失败: ${error.message}`);
553
+ lines.push('');
554
+ lines.push(`- stage: ${error.errorStage}`);
555
+ lines.push(`- code: ${error.code}`);
556
+ if (error.details?.specPath) {
557
+ lines.push(`- specPath: ${error.details.specPath}`);
558
+ }
559
+ const validation = error.details?.validation;
560
+ if (validation) {
561
+ const issues = [
562
+ ...(validation.schemaErrors || []),
563
+ ...(validation.boundaryErrors || []),
564
+ ...(validation.governanceErrors || []),
565
+ ];
566
+ if (issues.length > 0) {
567
+ lines.push('');
568
+ lines.push('校验问题:');
569
+ for (const item of issues) {
570
+ lines.push(`- ${item}`);
571
+ }
572
+ }
573
+ }
574
+ if (error.details?.command) {
575
+ lines.push(`- command: ${error.details.command}`);
576
+ }
577
+ return `${lines.join('\n')}\n`;
578
+ }
579
+
580
+ async function createSmokeSeed(baseUrl, analysis) {
581
+ const payload = buildSeedPayload(analysis);
582
+ const response = await postJson(baseUrl, payload);
583
+ return {
584
+ request: payload,
585
+ response,
586
+ };
587
+ }
588
+
589
+ function buildSeedPayload(analysis) {
590
+ const model = analysis.suggestedModel || {};
591
+ const fields = Array.isArray(model?.data?.fields) ? model.data.fields : [];
592
+ const children = Array.isArray(model?.data?.children) ? model.data.children : [];
593
+ const timestamp = formatSeedTimestamp(new Date());
594
+ const payload = {};
595
+
596
+ for (const field of fields) {
597
+ payload[field.name] = buildSeedValue(field, analysis, timestamp);
598
+ }
599
+
600
+ for (const child of children) {
601
+ payload[child.name] = [buildSeedChildRow(child, analysis, timestamp)];
602
+ }
603
+
604
+ return payload;
605
+ }
606
+
607
+ function buildSeedChildRow(child, analysis, timestamp) {
608
+ const row = {};
609
+ for (const field of child.fields || []) {
610
+ row[field.name] = buildSeedValue(field, analysis, timestamp, child.label || child.name);
611
+ }
612
+ return row;
613
+ }
614
+
615
+ function buildSeedValue(field, analysis, timestamp, parentLabel) {
616
+ const name = String(field.name || '');
617
+ const label = String(field.label || parentLabel || analysis?.inferred?.label || '对象');
618
+ const type = String(field.type || 'string');
619
+
620
+ if (type === 'enum') {
621
+ if (field.default !== undefined) {
622
+ return String(field.default);
623
+ }
624
+ return Array.isArray(field.options) && field.options.length > 0 ? String(field.options[0]) : '';
625
+ }
626
+
627
+ if (type === 'boolean') {
628
+ return field.default !== undefined ? Boolean(field.default) : false;
629
+ }
630
+
631
+ if (['integer', 'number', 'decimal'].includes(type)) {
632
+ return field.default !== undefined ? Number(field.default) : 1;
633
+ }
634
+
635
+ if (name.toLowerCase().includes('code')) {
636
+ return `${String(analysis.inferred.object || 'OBJ').toUpperCase()}-${timestamp}`;
637
+ }
638
+
639
+ if (name.toLowerCase().endsWith('name')) {
640
+ return `${label}示例${timestamp.slice(-4)}`;
641
+ }
642
+
643
+ if (name.toLowerCase().includes('phone') || name.toLowerCase().includes('mobile')) {
644
+ return `1380000${timestamp.slice(-4)}`;
645
+ }
646
+
647
+ if (name.toLowerCase().includes('email')) {
648
+ return `seed-${timestamp}@example.com`;
649
+ }
650
+
651
+ if (name.toLowerCase().includes('owner')) {
652
+ return '系统验证';
653
+ }
654
+
655
+ return field.required ? `${label}示例` : `${label}补充`;
656
+ }
657
+
658
+ function formatSeedTimestamp(date) {
659
+ const parts = [
660
+ date.getFullYear(),
661
+ padSeedPart(date.getMonth() + 1),
662
+ padSeedPart(date.getDate()),
663
+ padSeedPart(date.getHours()),
664
+ padSeedPart(date.getMinutes()),
665
+ padSeedPart(date.getSeconds()),
666
+ ];
667
+ return parts.join('');
668
+ }
669
+
670
+ function padSeedPart(value) {
671
+ return String(value).padStart(2, '0');
672
+ }
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+ const { loadDraft, saveDraft, validateDraft } = require('./lib/draft-toolkit.cjs');
3
+ const { loadRuntimeConfig } = require('./lib/model-toolkit.cjs');
4
+
5
+ const args = process.argv.slice(2);
6
+ const draftReference = args.find((item) => !item.startsWith('--'));
7
+ const configPath = readArgValue(args, '--config');
8
+
9
+ try {
10
+ const runtimeConfig = loadRuntimeConfig({ configPath });
11
+ if (!draftReference) {
12
+ throw new Error('请提供 draft 引用,例如: node ./tools/validate-draft.cjs <draft-path|draft-id>');
13
+ }
14
+ const draft = loadDraft(draftReference, { draftRoot: runtimeConfig.drafts.rootDir });
15
+ const nextDraft = validateDraft(draft, { configPath });
16
+ const savedDraft = saveDraft(nextDraft, {
17
+ draftRoot: draft.storage?.draftRoot || runtimeConfig.drafts.rootDir,
18
+ fileName: draft.storage?.fileName,
19
+ });
20
+ console.log(JSON.stringify(savedDraft, null, 2));
21
+ } catch (error) {
22
+ console.error(`\n❌ Draft 校验失败: ${error.message}`);
23
+ process.exit(1);
24
+ }
25
+
26
+ function readArgValue(argv, flag) {
27
+ const index = argv.indexOf(flag);
28
+ if (index === -1) {
29
+ return '';
30
+ }
31
+ return argv[index + 1] || '';
32
+ }