@magicbe/api-generator
Version:
api 生成器
86 lines (85 loc) • 2.77 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var minimist_1 = __importDefault(require("minimist"));
var node_fs_1 = require("node:fs");
var node_path_1 = require("node:path");
var _1 = require(".");
var args = (0, minimist_1.default)(process.argv.slice(2));
// 处理 help 参数
if (args.help || args.h) {
var helpPath = (0, node_path_1.join)(__dirname, "help.txt");
(0, node_fs_1.readFile)(helpPath, function (err, data) {
if (err) {
console.error("无法读取帮助文件");
process.exit(1);
}
console.log(data.toString());
process.exit(0);
});
}
var _a = args.config, config = _a === void 0 ? "./api-generator.config.json" : _a;
var readConfig = function (path) {
return new Promise(function (resolve, reject) {
(0, node_fs_1.access)(path, function (err) {
if (err)
reject(err);
else
(0, node_fs_1.readFile)(path, function (err, data) {
if (err)
reject(err);
else
resolve(data.toString());
});
});
});
};
var input = [
"yapi",
"swagger",
];
var handle = function (_a) {
var service = _a.service, type = _a.type, target = _a.target, output = _a.output, headers = _a.headers, _function = _a.function, resources = _a.resources, token = _a.token, cats = _a.cats;
if (!service)
throw new Error("field \"service\" is required");
if (!input.includes(type)) {
throw new Error("field \"type\" is include enums ".concat(JSON.stringify(input)));
}
if (type === "yapi") {
(new _1.GeneratorYapi({
service: service,
target: target,
output: output,
function: _function,
token: token,
cats: cats,
})).gen();
}
if (type === "swagger") {
(new _1.GeneratorSwaggerApi({
service: service,
target: target,
output: output,
headers: headers,
function: _function,
resources: resources,
})).gen();
}
};
readConfig(config).then(function (content) {
try {
var args_1 = JSON.parse(content);
console.log(args_1);
console.log("\u6570\u636E\u6765\u6E90 ".concat(config, "\u3002"));
handle(args_1);
}
catch (error) {
console.error(error);
}
}).catch(function () {
console.log("\u6570\u636E\u6765\u6E90 \u547D\u4EE4\u884C\u53C2\u6570\u3002");
handle(args);
});
;