UNPKG

anl

Version:
2 lines (1 loc) 3.25 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("../utils/index.js"),s=require("./core/components.js"),a=require("./core/get-data.js"),r=require("./core/path.js"),o=require("shelljs"),i=require("chalk"),n=require("path");function c(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var l=c(e),p=c(i),h=c(n);let u;const d="development"===process.env.NODE_ENV,f={saveTypeFolderPath:d?"apps/types":"src/api/types",saveApiListFolderPath:d?"apps/types":"src/api",saveEnumFolderPath:d?"apps/types/enums":"src/enums",importEnumPath:"../../../enums",requestMethodsImportPath:"./fetch",dataLevel:"serve",swaggerJsonUrl:"https://generator3.swagger.io/openapi.json",headers:{},formatting:{indentation:"\t",lineEnding:"\n"}};class g{schemas={};paths={};async handle(e){try{let t;if(t="development"===process.env.NODE_ENV?(await Promise.resolve().then((function(){return require("../../data/umf.json.js")}))).default:await a.getSwaggerJson(e),!t)throw new Error("无法获取 Swagger 数据");this.schemas=t.components?.schemas||{},this.paths=t.paths||{};const o=new s.default(this.schemas,e),i=new r.PathParse(this.paths,t.components?.parameters,e);return await Promise.all([o.handle(),i.handle()]),!0}catch(e){if(e instanceof Error)throw new Error(`处理 Swagger 数据失败: ${e.message}`);throw new Error("处理 Swagger 数据失败: 未知错误")}}async formatGeneratedFiles(e){const s=`npx prettier --write "${e.saveTypeFolderPath}/**/*.{ts,d.ts}"`;try{await l.default.promises.access(e.saveTypeFolderPath);const{stderr:a}=await new Promise(((e,t)=>{o.exec(s,((s,a,r)=>{s?t(s):e({stdout:a,stderr:r})}))}));if(a)throw new Error(a);t.log.success("文件格式化成功")}catch(e){console.log(""),t.log.error("格式化失败,请手动执行以下命令:"),console.log("$",p.default.yellow(s)),console.log("")}}async copyAjaxConfigFiles(e){try{const s=["config.ts","error-message.ts","fetch.ts","api-type.d.ts"],a=h.default.join(__dirname,"..","..","ajax-config"),r=e;for(const e of s){const s=h.default.join(a,e),o=h.default.join(r,e);try{await l.default.promises.access(s);try{await l.default.promises.access(o),t.log.info(`${e} 已存在,跳过生成.`)}catch{await l.default.promises.copyFile(s,o),t.log.success(`${e} create done.`)}}catch(e){t.log.error(`源文件 ${s} 不存在`);continue}}}catch(e){return e}}async initialize(){const e=process.cwd()+"/an.config.json";try{const s=await this.getConfig(e);if(!u)return;await l.default.promises.mkdir(s.saveApiListFolderPath,{recursive:!0}),await this.copyAjaxConfigFiles(s.saveApiListFolderPath),await t.clearDir(s.saveTypeFolderPath),await t.clearDir(s.saveEnumFolderPath),await this.handle(s),await this.formatGeneratedFiles(s)}catch(e){const s=e instanceof Error?e.message:"未知错误";t.log.error(`初始化失败: ${s}`)}}async getConfig(e){try{const t=await l.default.promises.readFile(e,"utf8");return u=!0,JSON.parse(t)}catch(s){return u=!1,t.log.warning("配置文件不存在,将自动创建配置文件。"),await t.writeFileRecursive(e,JSON.stringify(f,null,2)),t.log.success("请查看项目根目录下的 an.config.json 文件"),f}}}if("development"===process.env.NODE_ENV){(new g).initialize()}exports.Main=g;