33 lines
840 B
JavaScript
33 lines
840 B
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 导出 Swagger 文档为 swagger.json,便于导入 Apifox / Postman 给前端调试。
|
||
* 用法:npm run swagger:export
|
||
*/
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const mm = require('egg-mock');
|
||
|
||
const output = process.env.SWAGGER_OUTPUT
|
||
? path.resolve(process.env.SWAGGER_OUTPUT)
|
||
: path.join(__dirname, '../swagger.json');
|
||
|
||
(async () => {
|
||
const app = mm.app();
|
||
await app.ready();
|
||
|
||
const res = await app.httpRequest().get('/swagger-doc');
|
||
if (res.status !== 200) {
|
||
throw new Error(`获取 swagger 文档失败,HTTP ${res.status}`);
|
||
}
|
||
|
||
fs.writeFileSync(output, JSON.stringify(res.body, null, 2));
|
||
console.log(`swagger 文档已导出:${output}`);
|
||
|
||
await app.close();
|
||
process.exit(0);
|
||
})().catch(err => {
|
||
console.error('导出失败:', err);
|
||
process.exit(1);
|
||
});
|