rop-contract-diff.js
2.73 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
#!/usr/bin/env node
/**
* 校验已跟踪 contract 与 Swagger 快照是否一致
*
* 默认离线模式(读本地快照):
* npm run rop:contract:check
*
* 对比线上与快照差异后校验:
* npm run rop:contract:check -- --online
*/
const {
loadSwagger,
DEFAULT_SWAGGER_SNAPSHOT,
} = require("../src/libs/rop/swaggerInspect");
const {
findTrackedContracts,
diffSnapshotWithTracked,
diffOnlineSnapshot,
} = require("../src/libs/rop/contractDiff");
const DEFAULT_ONLINE_URL = process.env.ROP_SWAGGER_URL
|| "http://dj.dev.glsx.com.cn/rop-dj-smartcarlife/swagger-rop/api-docs";
function parseArgs(argv) {
const args = {};
for (let i = 2; i < argv.length; i += 1) {
const key = argv[i];
if (!key.startsWith("--")) {
continue;
}
const name = key.slice(2);
const value = argv[i + 1];
if (value && !value.startsWith("--")) {
args[name] = value;
i += 1;
} else {
args[name] = true;
}
}
return args;
}
function printReport(title, report) {
console.log(`\n=== ${title} ===`);
if (report.checked != null) {
console.log(`Checked: ${report.checked}`);
}
if (report.added?.length) {
console.log("Added methods:", report.added.slice(0, 5).join(", "), report.added.length > 5 ? "..." : "");
}
if (report.removed?.length) {
console.log("Removed methods:", report.removed.slice(0, 5).join(", "), report.removed.length > 5 ? "..." : "");
}
report.warnings?.forEach((message) => console.warn("WARN:", message));
report.errors?.forEach((message) => console.error("ERROR:", message));
}
async function main() {
const args = parseArgs(process.argv);
const snapshotPath = args.snapshot || DEFAULT_SWAGGER_SNAPSHOT;
const tracked = findTrackedContracts();
if (!tracked.length) {
console.warn("No tracked contracts found under src/api/adapters/**/__contracts__");
process.exit(0);
}
const snapshot = await loadSwagger(snapshotPath);
let errors = [];
let warnings = [];
if (args.online) {
const onlineUrl = args.url || DEFAULT_ONLINE_URL;
const online = await loadSwagger(onlineUrl);
const onlineReport = diffOnlineSnapshot(snapshot, online);
printReport("Online vs Snapshot", onlineReport);
warnings = warnings.concat(onlineReport.warnings || []);
}
const trackedReport = diffSnapshotWithTracked(snapshot, tracked);
printReport("Tracked Contracts vs Snapshot", trackedReport);
errors = errors.concat(trackedReport.errors || []);
warnings = warnings.concat(trackedReport.warnings || []);
if (errors.length) {
console.error(`\nContract check failed with ${errors.length} error(s).`);
process.exit(1);
}
console.log(`\nContract check passed (${tracked.length} tracked, ${warnings.length} warning(s)).`);
}
main().catch((error) => {
console.error("Contract check failed:", error.message);
process.exit(1);
});