rop-swagger-fetch.js
1.56 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
#!/usr/bin/env node
/**
* 拉取 ROP Swagger 契约快照
*
* 示例:
* npm run rop:swagger:fetch
* npm run rop:swagger:fetch -- --url http://dj.dev.glsx.com.cn/rop-dj-smartcarlife/swagger-rop/api-docs
*/
const fs = require("fs");
const path = require("path");
const { loadSwagger, countRouterMethods } = require("../src/libs/rop/swaggerInspect");
const DEFAULT_URL = process.env.ROP_SWAGGER_URL
|| "http://dj.dev.glsx.com.cn/rop-dj-smartcarlife/swagger-rop/api-docs";
const DEFAULT_OUT = path.join(
__dirname,
"../src/libs/rop/__snapshots__/smartcarlife.swagger.json",
);
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;
}
async function main() {
const args = parseArgs(process.argv);
const url = args.url || DEFAULT_URL;
const out = path.resolve(args.out || DEFAULT_OUT);
const swagger = await loadSwagger(url);
const methodCount = countRouterMethods(swagger);
ensureDir(path.dirname(out));
fs.writeFileSync(out, `${JSON.stringify(swagger, null, 2)}\n`, "utf8");
console.log("Fetched:", url);
console.log("Wrote:", out);
console.log("Router methods:", methodCount);
}
function ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
}
main().catch((error) => {
console.error("Fetch failed:", error.message);
process.exit(1);
});