Skip to content

Commit

Permalink
feat: 增加文件抬头
Browse files Browse the repository at this point in the history
  • Loading branch information
wangj committed Jun 2, 2022
1 parent d9049d2 commit c3a8f78
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
1 change: 0 additions & 1 deletion dist/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { convertRefKey, isValidName, nodeType, parseSingleSimpleValue, tsUnionOf
const COMMENT_RE = /\*\//g;
export const LB_RE = /\r?\n/g;
export function transformSchemaObjMap(objMap, root = true) {
const ouputArr = [];
let output = "";
Object.entries(objMap).forEach(([k, v]) => {
const comments = createComment(v);
Expand Down
19 changes: 18 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { removeRef } from "./removeRef.js";
const { writeFileSync } = fs;
export async function run(input, output, options) {
const { tag, mock } = options;
console.log(input, output, tag, mock);
setMock(mock);
const res = await load(input);
if (res.swagger !== "2.0") {
throw new Error("请使用2.0版本的swagger");
}
if (tag) {
const { tags } = res;
const choices = (tags || []).map(({ name, description }) => {
Expand Down Expand Up @@ -45,6 +47,7 @@ export async function run(input, output, options) {
});
}
data = defs + data;
data = createHeader(res) + "\n\n" + data;
writeFileSync(output, data);
}
function pre(swagger) {
Expand Down Expand Up @@ -96,3 +99,17 @@ function pre(swagger) {
}
});
}
function createHeader(swagger) {
var _a, _b, _c;
const comments = ["powered by wyyt-fe"];
if ((_a = swagger.info) === null || _a === void 0 ? void 0 : _a.title) {
comments.push(swagger.info.title);
}
if ((_b = swagger.info) === null || _b === void 0 ? void 0 : _b.description) {
comments.push(`@description: ${swagger.info.description}`);
}
if ((_c = swagger.info) === null || _c === void 0 ? void 0 : _c.version) {
comments.push(`@version: ${swagger.info.version}`);
}
return ["/**", ...comments.map((c) => ` * ${c}`), " */"].join("\n");
}
1 change: 0 additions & 1 deletion dist/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function convertRequest(swagger, request, method, url) {
}
else {
returnType = transformSchemaObj(rep.schema);
console.log(url, returnType);
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "tsc && node ./bin/cli ./examples/swagger2.json ./examples/swagger.ts -t",
"dev": "tsc && node ./bin/cli ./examples/swagger2.json ./examples/swagger.ts -t -n",
"remote": "tsc && node ./bin/cli http://gateway.dev.wyyt.cc/bigplatform/v2/api-docs ./examples/swagger.ts -t"
},
"bin": {
Expand Down
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export async function run(
}
) {
const { tag, mock } = options;
console.log(input, output, tag, mock);
setMock(mock);
const res = await load(input);
if (res.swagger !== "2.0") {
throw new Error("请使用2.0版本的swagger");
}
if (tag) {
const { tags } = res;
const choices = (tags || []).map(({ name, description }) => {
Expand Down Expand Up @@ -60,6 +62,7 @@ export async function run(
});
}
data = defs + data;
data = createHeader(res) + "\n\n" + data;
writeFileSync(output, data);
}

Expand Down Expand Up @@ -117,3 +120,17 @@ function pre(swagger: Swagger) {
// }
// });
}

function createHeader(swagger: Swagger) {
const comments: string[] = ["powered by wyyt-fe"];
if (swagger.info?.title) {
comments.push(swagger.info.title);
}
if (swagger.info?.description) {
comments.push(`@description: ${swagger.info.description}`);
}
if (swagger.info?.version) {
comments.push(`@version: ${swagger.info.version}`);
}
return ["/**", ...comments.map((c) => ` * ${c}`), " */"].join("\n");
}
1 change: 0 additions & 1 deletion src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function convertRequest(
returnType = `${itemType}[]`;
} else {
returnType = transformSchemaObj(rep.schema);
console.log(url, returnType);
}
} else {
returnType = transformSchemaObj(rep.schema || rep);
Expand Down

0 comments on commit c3a8f78

Please sign in to comment.