UNPKG

4.47 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.generate = void 0;
7const fs_1 = require("fs");
8const getJson_1 = require("./getJson");
9const javascript_1 = require("./javascript");
10const utils_1 = require("./utils");
11const swaggerToOpenApi_1 = require("./utilities/swaggerToOpenApi");
12const chalk_1 = __importDefault(require("chalk"));
13const updateJson_1 = require("./updateJson");
14/** @param config If isn't defined will be use swagger.config.json instead */
15async function generate(config, cli) {
16 config = config !== null && config !== void 0 ? config : getSwaggerConfig();
17 const configs = Array.isArray(config) ? config : [config];
18 configs.forEach((con) => {
19 generateService(con, cli);
20 });
21}
22exports.generate = generate;
23const generateService = async (config, cli) => {
24 var _a, _b, _c;
25 config = Object.assign(Object.assign({}, config), { tag: (_a = cli === null || cli === void 0 ? void 0 : cli.tag) !== null && _a !== void 0 ? _a : config.tag, local: (_b = cli === null || cli === void 0 ? void 0 : cli.local) !== null && _b !== void 0 ? _b : config.local, branch: (_c = cli === null || cli === void 0 ? void 0 : cli.branch) !== null && _c !== void 0 ? _c : config.branch });
26 const { url, dir, tag, keepJson, local } = config;
27 if (!(0, fs_1.existsSync)(dir)) {
28 (0, fs_1.mkdirSync)(dir);
29 }
30 try {
31 const swaggerJsonPath = `${dir}/swagger.json`;
32 let input;
33 if (local) {
34 input = getLocalJson(dir);
35 }
36 else {
37 if (!url) {
38 throw new Error("Add url in swagger.config.json ");
39 }
40 if (typeof url === "string") {
41 input = await (0, getJson_1.getJson)(url);
42 }
43 else {
44 input = await (0, getJson_1.getJson)(await (0, utils_1.getCurrentUrl)(config));
45 }
46 if (input.swagger) {
47 (0, utils_1.majorVersionsCheck)("2.0.0", input.swagger);
48 // convert swagger v2 to openApi v3
49 config._isSwagger2 = true;
50 input = await (0, swaggerToOpenApi_1.swaggerToOpenApi)(input);
51 }
52 else {
53 (0, utils_1.majorVersionsCheck)("3.0.0", input.openapi);
54 }
55 }
56 if (keepJson) {
57 try {
58 if (!(tag === null || tag === void 0 ? void 0 : tag.length)) {
59 (0, fs_1.writeFileSync)(swaggerJsonPath, JSON.stringify(input));
60 }
61 else {
62 const oldJson = getLocalJson(dir);
63 input = (0, updateJson_1.partialUpdateJson)(oldJson, input, tag);
64 (0, fs_1.writeFileSync)(swaggerJsonPath, JSON.stringify(input));
65 }
66 }
67 catch (error) {
68 chalk_1.default.red(error);
69 chalk_1.default.red("keepJson failed");
70 }
71 }
72 switch (config.language) {
73 // case "CSharp": {
74 // await generateCSharpService(config, input);
75 // break;
76 // }
77 default:
78 await (0, javascript_1.generateJavascriptService)(config, input);
79 break;
80 }
81 }
82 catch (error) {
83 console.log(chalk_1.default.redBright(error));
84 console.log(chalk_1.default.redBright("failed"));
85 }
86};
87function getSwaggerConfig() {
88 try {
89 const config = JSON.parse((0, fs_1.readFileSync)("swagger.config.json").toString());
90 if (!config) {
91 throw "";
92 }
93 return config;
94 }
95 catch (error) {
96 try {
97 return JSON.parse((0, fs_1.readFileSync)("./swaggerConfig.json").toString()); // backward compatible for v1
98 }
99 catch (_a) {
100 throw new Error("Please define swagger.config.json");
101 }
102 }
103}
104function getLocalJson(dir) {
105 const swaggerJsonPath = `${dir}/swagger.json`;
106 try {
107 const old = (0, fs_1.readFileSync)(swaggerJsonPath).toString();
108 return JSON.parse(old);
109 }
110 catch (error) {
111 chalk_1.default.red("swagger.json file not found. You should set keepJson true to save json then run swag-ts without tag to save that");
112 throw error;
113 }
114}
115//# sourceMappingURL=index.js.map
\No newline at end of file