anl 26.109.0 → 26.109.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/package.json.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="26.109.
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="26.109.1",i="FE command line tool",t="bin/an-cli.js",s={registry:"https://registry.npmjs.org"},o={dev:"rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript -w",build:"rimraf lib && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",pub:"bash publish.sh",ts:"tsc ./src/int.ts --noEmit --watch",blink:"npm run build && npm link","sync-docs":"node scripts/sync-docs.js"},r={anl:"bin/an-cli.js"},n="Gleason <bianliuzhu@gmail.com>",l={"@commitlint/cli":"^17.4.3","@commitlint/config-conventional":"^17.4.3","@rollup/plugin-commonjs":"^21.0.1","@rollup/plugin-json":"^4.1.0","@rollup/plugin-node-resolve":"^13.1.3","@rollup/plugin-typescript":"^8.3.0","@types/inquirer":"^9.0.7","@types/shelljs":"^0.8.11","@typescript-eslint/eslint-plugin":"^5.52.0","@typescript-eslint/parser":"^5.52.0","cross-env":"^7.0.3",eslint:"^8.7.0",husky:"^8.0.3","openapi-types":"^12.1.3",prettier:"^3.3.2",rimraf:"^5.0.7",rollup:"^2.64.0","rollup-plugin-cleandir":"^2.0.0","rollup-plugin-copy":"^3.5.0","rollup-plugin-terser":"^7.0.2",typescript:"^4.5.4"},p={"app-root-path":"^3.1.0",cac:"^6.7.12",chalk:"4.*","clear-console":"^1.1.0",commander:"14.0.1",figures:"^6.1.0",inquirer:"^10.1.8","log-symbols":"^5.1.0",ora:"5.*","progress-estimator":"^0.3.0",shelljs:"^0.8.5"},c=["cli","command-line","frontend","typescript","type-generation","code-generator","swagger","openapi","api","rest-api","eslint","stylelint","prettier","commitlint","linter","formatter","react","vue","git","gitflow","developer-tools","automation","scaffolding"],a=["package.json","README.md","lib","template"],u={type:"git",url:"git+https://github.com/bianliuzhu/an-cli.git"},g="commonjs",m={name:"anl",version:e,description:i,main:t,publishConfig:s,scripts:o,bin:r,author:n,license:"ISC",devDependencies:l,dependencies:p,keywords:c,files:a,repository:u,type:g};exports.author=n,exports.bin=r,exports.default=m,exports.dependencies=p,exports.description=i,exports.devDependencies=l,exports.files=a,exports.keywords=c,exports.license="ISC",exports.main=t,exports.name="anl",exports.publishConfig=s,exports.repository=u,exports.scripts=o,exports.type=g,exports.version=e;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("../../utils/index.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e);exports.default=class{config;schemas={};enumsMap=new Map;schemasMap=new Map;defaultReturn={headerRef:"",renderStr:"",comment:"",typeName:""};requiredFieldS=[];appendMode;constructor(e,t,n){this.schemas=e,this.config=t,this.appendMode=n?.appendMode??!1,this.config.formatting&&(this.config.formatting.indentation=this.config.formatting.indentation??"\t")}handleEnum(e,n){const r=n.charAt(0).toUpperCase()+n.slice(1);if(t.isValidJSON(e.example)){const t=this.convertJsonToEnumString(e.example,r),i=this.getEnumTypeName(r);return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: ${i}${this.nullable(e.nullable)};`,comment:t}}const i=this.typeNameToFileName(r),s=this.parseEnum(e,r);return s&&!this.enumsMap.has(i)?(this.enumsMap.set(i,{fileName:i,content:s.renderStr}),{headerRef:"",renderStr:""}):null}nullable(e){return""+(e?" | null":"")}getEnumTypeName(e){return this.config.enmuConfig.erasableSyntaxOnly?`${e}Type`:e}fieldComment(e){const{description:t}=e;if(t){return["\t","/**","\n",`\t * ${t}`,"\n","\t */"].join("")}return""}typeNameToFileName(e){return e.replace(/_/g,"-").replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase().replace(/-+/g,"-")}nameTheHumpCenterStroke(e){const t=e.replace("#/components/schemas/",""),n={typeName:t,fileName:this.typeNameToFileName(t),dataType:""};if(this.schemas){const e=this.schemas[t];e.enum?n.dataType="enum":n.dataType=e.type}return n}parseRef(e){if(!e?.trim())return{headerRefStr:"",typeName:"",dataType:""};const{fileName:t,typeName:n,dataType:r=""}=this.nameTheHumpCenterStroke(e);let i;if("enum"===r){i=`import type { ${this.getEnumTypeName(n)} } from '${this.config.importEnumPath}';`}else i=`import type { ${n} } from './${t}';`;return{headerRefStr:i,typeName:n,dataType:r}}parseObject(e,t){let n="",r="";if("object"===e.type){const i=e;if("object"==typeof i.additionalProperties){const e=this.parseArray(i.additionalProperties,t)??this.defaultReturn;n=e?.headerRef??"",r=e?.renderStr??""}r="boolean"==typeof i.additionalProperties?`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: Record<string, unknown>${this.nullable(i.nullable)};`:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: ${e.type}${this.nullable(e.nullable)};`}return{headerRef:n,renderStr:r}}parseArray(e,n){const r=e,{items:i={},nullable:s,example:a}=r,o=i?.$ref;if(o){const{headerRefStr:e,typeName:r,dataType:i}=this.parseRef(o);if("enum"===i&&t.isValidJSON(a)){const e=this.typeNameToFileName(r),t=this.convertJsonToEnumString(a,r);this.enumsMap.set(e,{fileName:e,content:t})}const c="enum"===i?this.getEnumTypeName(r):r;return{headerRef:e,renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${c}>${this.nullable(s)};`,typeName:c}}const c=i?.type;let u="integer"===c?"number":c;return"object"===c&&(u="Record<string, unknown>"),{headerRef:"",renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${u}>${this.nullable(s)};`,typeName:u}}parseBoolean(e,t){return"boolean"===e.type?`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: boolean${this.nullable(e.nullable)};`:""}extractEnumMetadata(e){const t={},{enmuConfig:n}=this.config,r=e;if(n?.varnames){const e=r[n.varnames];Array.isArray(e)&&(t.customNames=e.map(e=>`${e}`))}if(n?.comment){const e=r[n.comment];if(e&&"object"==typeof e&&!Array.isArray(e)){const n={};Object.entries(e).forEach(([e,t])=>{"string"==typeof t&&(n[e]=t)}),Object.keys(n).length&&(t.descriptionMap=n)}}return t}resolveEnumMemberName(e,t,n){const{customNames:r,isNumericEnum:i,treatStringAsNumeric:s}=n,a=r?.[t];return"string"==typeof a&&a.trim()?a:i||"string"==typeof e&&s?`NUMBER_${e}`:"string"==typeof e&&e?e.toUpperCase():`ENUM_${t}`}parseEnum(e,t){if(!Array.isArray(e.enum))return null;const n=Boolean(e.type&&["integer","number"].includes(e.type)),r=e.enum,i="string"===e.type&&r.every(e=>"string"==typeof e&&!isNaN(Number(e))),{customNames:s,descriptionMap:a}=this.extractEnumMetadata(e),o=this.config.enmuConfig.erasableSyntaxOnly,c=r.map((e,t)=>{const r=this.resolveEnumMemberName(e,t,{customNames:s,isNumericEnum:n,treatStringAsNumeric:i}),c=n?`${e}`:`'${String(e)}'`,u=o?`${r}: ${c},`:`${r} = ${c},`,m=a?.[String(e)];return m?[`${this.config.formatting?.indentation}/** ${m} */`,`${this.config.formatting?.indentation}${u}`].join("\n"):`${this.config.formatting?.indentation}${u}`});return c.length?o?{headerRef:"",renderStr:[`export const ${t} = {`,...c,"} as const;","",`export type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`].join("\n")}:{headerRef:"",renderStr:[`export enum ${t} {`,...c,"}"].join("\n")}:null}parseInteger(e,t){if(Array.isArray(e.enum)){const n=t.charAt(0).toUpperCase()+t.slice(1);return this.parseEnum(e,n)?.renderStr??""}return`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: number${this.nullable(e.nullable)};`}parseNumber(e,t){if("number"!==e.type)return null;if(e.enum){const n=this.handleEnum(e,t);if(n)return n}return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: number${this.nullable(e.nullable)};`}}convertJsonToEnumString(e,t){try{const n=JSON.parse(e);if(this.config.enmuConfig.erasableSyntaxOnly){return`export const ${t} = {\n${Object.entries(n).map(([e,t])=>`${this.config.formatting?.indentation}${e}: '${t}'`).join(",\n")}\n} as const;\n\nexport type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`}return`export enum ${t} {\n${Object.entries(n).map(([e,t])=>`${this.config.formatting?.indentation}${e} = '${t}'`).join(",\n")}\n}`}catch(e){return console.error("JSON 解析失败:",e),""}}getStringTypeByFormat(e){if(!e)return{type:"string"};switch(e){case"date-time":return{type:"Date",comment:"ISO 8601 格式的日期时间字符串 (例如: 2024-01-09T12:00:00Z)"};case"date":return{type:"Date",comment:"ISO 8601 格式的日期字符串 (例如: 2024-01-09)"};case"time":return{type:"string",comment:"ISO 8601 格式的时间字符串 (例如: 12:00:00)"};case"email":case"idn-email":return{type:"string",comment:"邮箱地址"};case"uuid":return{type:"string",comment:"UUID 格式字符串"};case"uri":case"uri-reference":case"iri":case"iri-reference":return{type:"string",comment:"URI 格式字符串"};case"hostname":case"idn-hostname":return{type:"string",comment:"主机名"};case"ipv4":return{type:"string",comment:"IPv4 地址"};case"ipv6":return{type:"string",comment:"IPv6 地址"};case"binary":return{type:"File",comment:"二进制文件"};default:return{type:"string"}}}parseString(e,t){if("string"!==e.type)return null;if(e.enum){const n=this.handleEnum(e,t);if(n)return n}const{type:n,comment:r}=this.getStringTypeByFormat(e.format);return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: ${n}${this.nullable(e.nullable)};${r?` // ${r}`:""}`}}parseProperties(e,n){const r=[],i=[];for(const s in e){const a=e[s];if(a?.$ref){const{headerRefStr:e,typeName:o,dataType:c}=this.parseRef(a.$ref);i.includes(e)||o===n||i.push(e);const u=a,m=this.fieldComment(u);""!==m&&r.push(m);const l="enum"===c?this.getEnumTypeName(o):o;r.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${l};`);const p=a.example;if("enum"===c&&p&&t.isValidJSON(p)){const e=this.typeNameToFileName(o),t=this.convertJsonToEnumString(p,o);this.enumsMap.set(e,{fileName:e,content:t})}continue}if("allOf"in a){const e=a.allOf?.[0];if(e?.$ref){const{headerRefStr:t,typeName:o,dataType:c}=this.parseRef(e.$ref),u="enum"===c?this.getEnumTypeName(o):o;i.includes(t)||o===n||i.push(t);const m=a,l=this.fieldComment(m);""!==l&&r.push(l),r.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${u}${this.nullable(a.nullable)};`);continue}}const o=a,c=this.fieldComment(o);switch(""!==c&&r.push(c),o.type){case"array":{const e=this.parseArray(o,s)??this.defaultReturn;r.push(e?.renderStr??""),i.includes(e.headerRef)||n===e.typeName||i.push(e.headerRef)}break;case"boolean":r.push(this.parseBoolean(o,s));break;case"integer":r.push(this.parseInteger(o,s));break;case"number":{const e=this.parseNumber(o,s);e&&(e.headerRef&&!i.includes(e.headerRef)&&i.push(e.headerRef),r.push(e.renderStr))}break;case"string":if(o.enum){const e=s.charAt(0).toUpperCase()+s.slice(1);if(o.example&&t.isValidJSON(o.example)){const t=this.getEnumTypeName(e),n=`import type { ${t} } from '${this.config.importEnumPath}';`;i.includes(n)||i.push(n);const a=this.typeNameToFileName(e);if(!this.enumsMap.has(a)){const t=this.convertJsonToEnumString(o.example,e);this.enumsMap.set(a,{fileName:a,content:t})}r.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${t}${this.nullable(o.nullable)};`)}else{const t=this.parseEnum(o,e);if(t?.renderStr){const n=this.getEnumTypeName(e),a=`import type { ${n} } from '${this.config.importEnumPath}';`;i.includes(a)||i.push(a);const c=this.typeNameToFileName(e);this.enumsMap.has(c)||this.enumsMap.set(c,{fileName:c,content:t.renderStr}),r.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${n}${this.nullable(o.nullable)};`)}}}else r.push(this.parseString(o,s)?.renderStr??"");break;case"object":{const{headerRef:e,renderStr:t}=this.parseObject(o,s)??this.defaultReturn;r.push(t),i.includes(e)||i.push(e)}}}const s=[`export interface ${n} {`,...r,"}"];if(i.length>0){const e=i.filter(e=>""!==e);e.length>0&&e.push(""),s.unshift(...e)}const a=s.join("\n");return{headerRef:i.join("\n"),renderStr:a}}async generateContent(e,t){if("items"in e)return console.warn(`数组类型未处理: ${t}`,e.items),"";switch(e.type){case"boolean":return this.parseBoolean(e,t);case"integer":return this.parseInteger(e,t);case"number":{const n=this.parseNumber(e,t);return n?.renderStr??""}case"object":{const n=this.parseProperties(e.properties,t);return n?.renderStr??""}case"string":{const n=this.parseString(e,t);return n?.renderStr??""}default:return""}}async parseData(){try{if(!this.schemas)return void console.warn("schemas 为空");for(const[e,t]of Object.entries(this.schemas)){if("$ref"in t){console.warn(`跳过 ReferenceObject: ${e}`);continue}const n="type"in t?t:null;if(!n?.type){console.warn(`无效的 schema 对象: ${e}`);continue}this.requiredFieldS=t.required??[];const r=this.typeNameToFileName(e),i=await this.generateContent(n,e);if(i){(i.includes("export enum ")||i.includes("export const ")&&i.includes("as const"))&&!this.enumsMap.has(r)?this.enumsMap.set(r,{fileName:r,content:i}):this.schemasMap.set(e,{fileName:r,content:i})}}}catch(e){throw console.error("解析过程出错:",e),e}}async writeEnums(){const e=[],n=[];for(const[,r]of this.enumsMap){const i=async({fileName:e,content:r})=>{n.push(`export * from './${e}';`);const i=`${this.config.saveEnumFolderPath}/${e}.ts`;await t.writeFileRecursive(i,r),t.log.info(`${i.padEnd(80)} - Write done!`)};e.push(i(r))}await Promise.all(e),await this.writeIndexFile(`${this.config.saveEnumFolderPath}/index.ts`,n),t.log.success("Enums write done!")}async writeFile(){const e=[],n=[],r=`${this.config.saveTypeFolderPath}/models/`;for(const[,i]of this.schemasMap){const s=async({fileName:e,content:i})=>{n.push(`export * from './${e}';`);const s=`${r}${e}.ts`;await t.writeFileRecursive(s,i),t.log.info(`${s.padEnd(80)} - Write done!`)};e.push(s(i))}await Promise.all(e),await this.writeIndexFile(`${r}index.ts`,n),t.log.success("Component parse & write done!")}async handle(){await this.parseData(),await this.writeFile(),await this.writeEnums()}async writeIndexFile(e,n){const i=n.filter(Boolean);if(!i.length)return;let s=[];if(this.appendMode)try{s=(await r.default.promises.readFile(e,"utf8")).split("\n").filter(e=>""!==e.trim())}catch{s=[]}const a=new Set(s),o=[...s,...i.filter(e=>!a.has(e))].join("\n");await t.writeFileRecursive(e,o)}};
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),t=require("../../utils/index.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=n(e);exports.default=class{config;schemas={};enumsMap=new Map;schemasMap=new Map;defaultReturn={headerRef:"",renderStr:"",comment:"",typeName:""};requiredFieldS=[];appendMode;constructor(e,t,n){this.schemas=e,this.config=t,this.appendMode=n?.appendMode??!1,this.config.formatting&&(this.config.formatting.indentation=this.config.formatting.indentation??"\t")}handleEnum(e,n){const i=n.charAt(0).toUpperCase()+n.slice(1);if(t.isValidJSON(e.example)){const t=this.convertJsonToEnumString(e.example,i),r=this.getEnumTypeName(i);return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: ${r}${this.nullable(e.nullable)};`,comment:t}}const r=this.typeNameToFileName(i),s=this.parseEnum(e,i);return s&&!this.enumsMap.has(r)?(this.enumsMap.set(r,{fileName:r,content:s.renderStr}),{headerRef:"",renderStr:""}):null}nullable(e){return""+(e?" | null":"")}getEnumTypeName(e){return this.config.enmuConfig.erasableSyntaxOnly?`${e}Type`:e}fieldComment(e){const{description:t}=e;if(t){return["\t","/**","\n",`\t * ${t}`,"\n","\t */"].join("")}return""}typeNameToFileName(e){return e.replace(/_/g,"-").replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase().replace(/-+/g,"-")}nameTheHumpCenterStroke(e){const t=e.replace("#/components/schemas/",""),n={typeName:t,fileName:this.typeNameToFileName(t),dataType:""};if(this.schemas){const e=this.schemas[t];e.enum?n.dataType="enum":n.dataType=e.type}return n}parseRef(e){if(!e?.trim())return{headerRefStr:"",typeName:"",dataType:""};const{fileName:t,typeName:n,dataType:i=""}=this.nameTheHumpCenterStroke(e);let r;if("enum"===i){r=`import type { ${this.getEnumTypeName(n)} } from '${this.config.importEnumPath}';`}else r=`import type { ${n} } from './${t}';`;return{headerRefStr:r,typeName:n,dataType:i}}parseObject(e,t){let n="",i="";if("object"===e.type){const r=e;if("object"==typeof r.additionalProperties){const e=this.parseArray(r.additionalProperties,t)??this.defaultReturn;n=e?.headerRef??"",i=e?.renderStr??""}i="boolean"==typeof r.additionalProperties?`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: Record<string, unknown>${this.nullable(r.nullable)};`:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: ${e.type}${this.nullable(e.nullable)};`}return{headerRef:n,renderStr:i}}parseArray(e,n){const i=e,{items:r={},nullable:s,example:a}=i,o=r?.$ref;if(o){const{headerRefStr:e,typeName:i,dataType:r}=this.parseRef(o);if("enum"===r&&t.isValidJSON(a)){const e=this.typeNameToFileName(i),t=this.convertJsonToEnumString(a,i);this.enumsMap.set(e,{fileName:e,content:t})}const c="enum"===r?this.getEnumTypeName(i):i;return{headerRef:e,renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${c}>${this.nullable(s)};`,typeName:c}}const c=r?.type;let u="integer"===c?"number":c;return"object"===c&&(u="Record<string, unknown>"),{headerRef:"",renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${u}>${this.nullable(s)};`,typeName:u}}parseBoolean(e,t){return"boolean"===e.type?`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: boolean${this.nullable(e.nullable)};`:""}extractEnumMetadata(e){const t={},{enmuConfig:n}=this.config,i=e;if(n?.varnames){const e=i[n.varnames];Array.isArray(e)&&(t.customNames=e.map(e=>`${e}`))}if(n?.comment){const e=i[n.comment];if(e&&"object"==typeof e&&!Array.isArray(e)){const n={};Object.entries(e).forEach(([e,t])=>{"string"==typeof t&&(n[e]=t)}),Object.keys(n).length&&(t.descriptionMap=n)}}return t}resolveEnumMemberName(e,t,n){const{customNames:i,isNumericEnum:r,treatStringAsNumeric:s}=n,a=i?.[t];return"string"==typeof a&&a.trim()?a:r||"string"==typeof e&&s?`NUMBER_${e}`:"string"==typeof e&&e?e.toUpperCase():`ENUM_${t}`}parseEnum(e,t){if(!Array.isArray(e.enum))return null;const n=Boolean(e.type&&["integer","number"].includes(e.type)),i=e.enum,r="string"===e.type&&i.every(e=>"string"==typeof e&&!isNaN(Number(e))),{customNames:s,descriptionMap:a}=this.extractEnumMetadata(e),o=this.config.enmuConfig.erasableSyntaxOnly,c=i.map((e,t)=>{const i=this.resolveEnumMemberName(e,t,{customNames:s,isNumericEnum:n,treatStringAsNumeric:r}),c=n?`${e}`:`'${String(e)}'`,u=o?`${i}: ${c},`:`${i} = ${c},`,l=a?.[String(e)];return l?[`${this.config.formatting?.indentation}/** ${l} */`,`${this.config.formatting?.indentation}${u}`].join("\n"):`${this.config.formatting?.indentation}${u}`});return c.length?o?{headerRef:"",renderStr:[`export const ${t} = {`,...c,"} as const;","",`export type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`].join("\n")}:{headerRef:"",renderStr:[`export enum ${t} {`,...c,"}"].join("\n")}:null}parseInteger(e,t){if(Array.isArray(e.enum)){const n=t.charAt(0).toUpperCase()+t.slice(1);return this.parseEnum(e,n)?.renderStr??""}return`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: number${this.nullable(e.nullable)};`}parseNumber(e,t){if("number"!==e.type)return null;if(e.enum){const n=this.handleEnum(e,t);if(n)return n}return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: number${this.nullable(e.nullable)};`}}convertJsonToEnumString(e,t){try{const n=JSON.parse(e);if(this.config.enmuConfig.erasableSyntaxOnly){return`export const ${t} = {\n${Object.entries(n).map(([e,t])=>`${this.config.formatting?.indentation}${e}: '${t}'`).join(",\n")}\n} as const;\n\nexport type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`}return`export enum ${t} {\n${Object.entries(n).map(([e,t])=>`${this.config.formatting?.indentation}${e} = '${t}'`).join(",\n")}\n}`}catch(e){return console.error("JSON 解析失败:",e),""}}getStringTypeByFormat(e){if(!e)return"string";switch(e){case"date-time":case"date":return"Date";case"time":case"email":case"idn-email":case"uuid":case"uri":case"uri-reference":case"iri":case"iri-reference":case"hostname":case"idn-hostname":case"ipv4":case"ipv6":default:return"string";case"binary":return"File"}}parseString(e,t){if("string"!==e.type)return null;if(e.enum){const n=this.handleEnum(e,t);if(n)return n}const n=this.getStringTypeByFormat(e.format);return{headerRef:"",renderStr:`${this.config.formatting?.indentation}${t}${this.requiredFieldS.includes(t)?"":"?"}: ${n}${this.nullable(e.nullable)};`}}parseProperties(e,n){const i=[],r=[];for(const s in e){const a=e[s];if(a?.$ref){const{headerRefStr:e,typeName:o,dataType:c}=this.parseRef(a.$ref);r.includes(e)||o===n||r.push(e);const u=a,l=this.fieldComment(u);""!==l&&i.push(l);const m="enum"===c?this.getEnumTypeName(o):o;i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${m};`);const h=a.example;if("enum"===c&&h&&t.isValidJSON(h)){const e=this.typeNameToFileName(o),t=this.convertJsonToEnumString(h,o);this.enumsMap.set(e,{fileName:e,content:t})}continue}if("allOf"in a){const e=a.allOf?.[0];if(e?.$ref){const{headerRefStr:t,typeName:o,dataType:c}=this.parseRef(e.$ref),u="enum"===c?this.getEnumTypeName(o):o;r.includes(t)||o===n||r.push(t);const l=a,m=this.fieldComment(l);""!==m&&i.push(m),i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${u}${this.nullable(a.nullable)};`);continue}}const o=a,c=this.fieldComment(o);switch(""!==c&&i.push(c),o.type){case"array":{const e=this.parseArray(o,s)??this.defaultReturn;i.push(e?.renderStr??""),r.includes(e.headerRef)||n===e.typeName||r.push(e.headerRef)}break;case"boolean":i.push(this.parseBoolean(o,s));break;case"integer":i.push(this.parseInteger(o,s));break;case"number":{const e=this.parseNumber(o,s);e&&(e.headerRef&&!r.includes(e.headerRef)&&r.push(e.headerRef),i.push(e.renderStr))}break;case"string":if(o.enum){const e=s.charAt(0).toUpperCase()+s.slice(1);if(o.example&&t.isValidJSON(o.example)){const t=this.getEnumTypeName(e),n=`import type { ${t} } from '${this.config.importEnumPath}';`;r.includes(n)||r.push(n);const a=this.typeNameToFileName(e);if(!this.enumsMap.has(a)){const t=this.convertJsonToEnumString(o.example,e);this.enumsMap.set(a,{fileName:a,content:t})}i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${t}${this.nullable(o.nullable)};`)}else{const t=this.parseEnum(o,e);if(t?.renderStr){const n=this.getEnumTypeName(e),a=`import type { ${n} } from '${this.config.importEnumPath}';`;r.includes(a)||r.push(a);const c=this.typeNameToFileName(e);this.enumsMap.has(c)||this.enumsMap.set(c,{fileName:c,content:t.renderStr}),i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${n}${this.nullable(o.nullable)};`)}}}else i.push(this.parseString(o,s)?.renderStr??"");break;case"object":{const{headerRef:e,renderStr:t}=this.parseObject(o,s)??this.defaultReturn;i.push(t),r.includes(e)||r.push(e)}}}const s=[`export interface ${n} {`,...i,"}"];if(r.length>0){const e=r.filter(e=>""!==e);e.length>0&&e.push(""),s.unshift(...e)}const a=s.join("\n");return{headerRef:r.join("\n"),renderStr:a}}async generateContent(e,t){if("items"in e)return console.warn(`数组类型未处理: ${t}`,e.items),"";switch(e.type){case"boolean":return this.parseBoolean(e,t);case"integer":return this.parseInteger(e,t);case"number":{const n=this.parseNumber(e,t);return n?.renderStr??""}case"object":{const n=this.parseProperties(e.properties,t);return n?.renderStr??""}case"string":{const n=this.parseString(e,t);return n?.renderStr??""}default:return""}}async parseData(){try{if(!this.schemas)return void console.warn("schemas 为空");for(const[e,t]of Object.entries(this.schemas)){if("$ref"in t){console.warn(`跳过 ReferenceObject: ${e}`);continue}const n="type"in t?t:null;if(!n?.type){console.warn(`无效的 schema 对象: ${e}`);continue}this.requiredFieldS=t.required??[];const i=this.typeNameToFileName(e),r=await this.generateContent(n,e);if(r){(r.includes("export enum ")||r.includes("export const ")&&r.includes("as const"))&&!this.enumsMap.has(i)?this.enumsMap.set(i,{fileName:i,content:r}):this.schemasMap.set(e,{fileName:i,content:r})}}}catch(e){throw console.error("解析过程出错:",e),e}}async writeEnums(){const e=[],n=[];for(const[,i]of this.enumsMap){const r=async({fileName:e,content:i})=>{n.push(`export * from './${e}';`);const r=`${this.config.saveEnumFolderPath}/${e}.ts`;await t.writeFileRecursive(r,i),t.log.info(`${r.padEnd(80)} - Write done!`)};e.push(r(i))}await Promise.all(e),await this.writeIndexFile(`${this.config.saveEnumFolderPath}/index.ts`,n),t.log.success("Enums write done!")}async writeFile(){const e=[],n=[],i=`${this.config.saveTypeFolderPath}/models/`;for(const[,r]of this.schemasMap){const s=async({fileName:e,content:r})=>{n.push(`export * from './${e}';`);const s=`${i}${e}.ts`;await t.writeFileRecursive(s,r),t.log.info(`${s.padEnd(80)} - Write done!`)};e.push(s(r))}await Promise.all(e),await this.writeIndexFile(`${i}index.ts`,n),t.log.success("Component parse & write done!")}async handle(){await this.parseData(),await this.writeFile(),await this.writeEnums()}async writeIndexFile(e,n){const r=n.filter(Boolean);if(!r.length)return;let s=[];if(this.appendMode)try{s=(await i.default.promises.readFile(e,"utf8")).split("\n").filter(e=>""!==e.trim())}catch{s=[]}const a=new Set(s),o=[...s,...r.filter(e=>!a.has(e))].join("\n");await t.writeFileRecursive(e,o)}};
|