anl 25.1202.0 → 25.1203.0

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="25.1202.0",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:"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
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="25.1203.0",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:"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("../../utils/index.js");const t="\t";exports.default=class{config;schemas={};enumsMap=new Map;schemasMap=new Map;defaultReturn={headerRef:"",renderStr:"",comment:"",typeName:""};requiredFieldS=[];constructor(e,t){this.schemas=e,this.config=t}nullable(e){return""+(e?" | null":"")}getEnumTypeName(e){return this.config.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 s;if("enum"===r){s=`import type { ${this.getEnumTypeName(n)} } from '${this.config.importEnumPath}';`}else s=`import type { ${n} } from './${t}';`;return{headerRefStr:s,typeName:n,dataType:r}}parseObject(e,n){let r="",s="";if("object"===e.type){const i=e;if("object"==typeof i.additionalProperties){const e=this.parseArray(i.additionalProperties,n)??this.defaultReturn;r=e?.headerRef??"",s=e?.renderStr??""}s="boolean"==typeof i.additionalProperties?`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: Record<string, unknown>${this.nullable(i.nullable)};`:`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: ${e.type}${this.nullable(e.nullable)};`}return{headerRef:r,renderStr:s}}parseArray(n,r){const s=n,{items:i={},nullable:a,example:o}=s,u=i?.$ref;if(u){const{headerRefStr:n,typeName:s,dataType:i}=this.parseRef(u);if("enum"===i&&e.isValidJSON(o)){const e=this.typeNameToFileName(s),t=this.convertJsonToEnumString(o,s);this.enumsMap.set(e,{fileName:e,content:t})}const l="enum"===i?this.getEnumTypeName(s):s;return{headerRef:n,renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: Array<${l}>${this.nullable(a)};`,typeName:l}}const l=i?.type;let p="integer"===l?"number":l;return"object"===l&&(p="Record<string, unknown>"),{headerRef:"",renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: Array<${p}>${this.nullable(a)};`,typeName:p}}parseBoolean(e,n){return"boolean"===e.type?`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: boolean${this.nullable(e.nullable)};`:""}parseEnum(e,n){if(!Array.isArray(e.enum))return null;if(this.config.erasableSyntaxOnly){if("integer"===e.type){return{headerRef:"",renderStr:[`export const ${n} = {`,...e.enum.map(e=>`${t}NUMBER_${e}: ${e},`),"} as const;","",`export type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`].join("\n")}}if("string"===e.type){const r=e.enum.every(e=>!isNaN(Number(e)));return{headerRef:"",renderStr:[`export const ${n} = {`,...e.enum.map(e=>r?`${t}NUMBER_${e}: '${e}',`:`${t}${e.toUpperCase()}: '${e}',`),"} as const;","",`export type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`].join("\n")}}}else{if("integer"===e.type){return{headerRef:"",renderStr:[`export enum ${n} {`,...e.enum.map(e=>`${t}NUMBER_${e} = ${e},`),"}"].join("\n")}}if("string"===e.type){const r=e.enum.every(e=>!isNaN(Number(e)));return{headerRef:"",renderStr:[`export enum ${n} {`,...e.enum.map(e=>r?`${t}NUMBER_${e} = '${e}',`:`${t}${e.toUpperCase()} = '${e}',`),"}"].join("\n")}}}return null}parseInteger(e,n){if(Array.isArray(e.enum)){const t=n.charAt(0).toUpperCase()+n.slice(1);return this.parseEnum(e,t)?.renderStr??""}return`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: number${this.nullable(e.nullable)};`}parseNumber(e,n){return"number"===e.type?`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: number${this.nullable(e.nullable)};`:""}convertJsonToEnumString(e,n){try{const r=JSON.parse(e);if(this.config.erasableSyntaxOnly){return`export const ${n} = {\n${Object.entries(r).map(([e,n])=>`${t}${e}: '${n}'`).join(",\n")}\n} as const;\n\nexport type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`}return`export enum ${n} {\n${Object.entries(r).map(([e,n])=>`${t}${e} = '${n}'`).join(",\n")}\n}`}catch(e){return console.error("JSON 解析失败:",e),""}}parseString(n,r){if("string"===n.type){if(n.enum){const s=r.charAt(0).toUpperCase()+r.slice(1);if(e.isValidJSON(n.example)){const e=this.convertJsonToEnumString(n.example,s),i=this.getEnumTypeName(s);return{headerRef:"",renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: ${i}${this.nullable(n.nullable)};`,comment:e}}{const e=this.typeNameToFileName(s),t=this.parseEnum(n,s);if(t&&!this.enumsMap.has(e))return this.enumsMap.set(e,{fileName:e,content:t.renderStr}),{headerRef:"",renderStr:""}}}return{headerRef:"",renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: string${this.nullable(n.nullable)};`}}return null}parseProperties(n,r){const s=[],i=[];for(const a in n){const o=n[a];if(o?.$ref){const{headerRefStr:n,typeName:r,dataType:u}=this.parseRef(o.$ref);i.includes(n)||i.push(n);const l=o,p=this.fieldComment(l);""!==p&&s.push(p);const c="enum"===u?this.getEnumTypeName(r):r;s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${c};`);const h=o.example;if("enum"===u&&h&&e.isValidJSON(h)){const e=this.typeNameToFileName(r),t=this.convertJsonToEnumString(h,r);this.enumsMap.set(e,{fileName:e,content:t})}continue}if("allOf"in o){const e=o.allOf?.[0];if(e?.$ref){const{headerRefStr:n,typeName:r,dataType:u}=this.parseRef(e.$ref),l="enum"===u?this.getEnumTypeName(r):r;i.includes(n)||i.push(n);const p=o,c=this.fieldComment(p);""!==c&&s.push(c),s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${l}${this.nullable(o.nullable)};`);continue}}const u=o,l=this.fieldComment(u);switch(""!==l&&s.push(l),u.type){case"array":{const e=this.parseArray(u,a)??this.defaultReturn;s.push(e?.renderStr??""),i.includes(e.headerRef)||r===e.typeName||i.push(e.headerRef)}break;case"boolean":s.push(this.parseBoolean(u,a));break;case"integer":s.push(this.parseInteger(u,a));break;case"number":s.push(this.parseNumber(u,a));break;case"string":if(u.enum){const n=a.charAt(0).toUpperCase()+a.slice(1);if(u.example&&e.isValidJSON(u.example)){const e=this.getEnumTypeName(n),r=`import type { ${e} } from '${this.config.importEnumPath}';`;i.includes(r)||i.push(r);const o=this.typeNameToFileName(n);if(!this.enumsMap.has(o)){const e=this.convertJsonToEnumString(u.example,n);this.enumsMap.set(o,{fileName:o,content:e})}s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${e}${this.nullable(u.nullable)};`)}else{const e=this.parseEnum(u,n);if(e?.renderStr){const r=this.getEnumTypeName(n),o=`import type { ${r} } from '${this.config.importEnumPath}';`;i.includes(o)||i.push(o);const l=this.typeNameToFileName(n);this.enumsMap.has(l)||this.enumsMap.set(l,{fileName:l,content:e.renderStr}),s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${r}${this.nullable(u.nullable)};`)}}}else u.format&&"binary"===u.format?s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: File${this.nullable(u.nullable)};`):s.push(this.parseString(u,a)?.renderStr??"");break;case"object":{const{headerRef:e,renderStr:t}=this.parseObject(u,a)??this.defaultReturn;s.push(t),i.includes(e)||i.push(e)}}}const a=[`export interface ${r} {`,...s,"}"];if(i.length>0){const e=i.filter(e=>""!==e);e.length>0&&e.push(""),a.unshift(...e)}const o=a.join("\n");return{headerRef:i.join("\n"),renderStr:o}}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":return this.parseNumber(e,t);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),s=await this.generateContent(n,e);if(s){(s.includes("export enum ")||s.includes("export const ")&&s.includes("as const"))&&!this.enumsMap.has(r)?this.enumsMap.set(r,{fileName:r,content:s}):this.schemasMap.set(e,{fileName:r,content:s})}}}catch(e){throw console.error("解析过程出错:",e),e}}async writeEnums(){const t=[],n=[];for(const[,r]of this.enumsMap){const s=async({fileName:t,content:r})=>{n.push(`export * from './${t}';`);const s=`${this.config.saveEnumFolderPath}/${t}.ts`;await e.writeFileRecursive(s,r),e.log.info(`${s.padEnd(80)} - Write done!`)};t.push(s(r))}await Promise.all(t),await e.writeFileRecursive(`${this.config.saveEnumFolderPath}/index.ts`,n.join("\n")),e.log.success("Enums write done!")}async writeFile(){const t=[],n=[],r=`${this.config.saveTypeFolderPath}/models/`;for(const[,s]of this.schemasMap){const i=async({fileName:t,content:s})=>{n.push(`export * from './${t}';`);const i=`${r}${t}.ts`;await e.writeFileRecursive(i,s),e.log.info(`${i.padEnd(80)} - Write done!`)};t.push(i(s))}await Promise.all(t),await e.writeFileRecursive(`${r}index.ts`,n.join("\n")),e.log.success("Component parse & write done!")}async handle(){await this.parseData(),await this.writeFile(),await this.writeEnums()}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../utils/index.js");const t="\t";exports.default=class{config;schemas={};enumsMap=new Map;schemasMap=new Map;defaultReturn={headerRef:"",renderStr:"",comment:"",typeName:""};requiredFieldS=[];constructor(e,t){this.schemas=e,this.config=t}handleEnum(n,r){const s=r.charAt(0).toUpperCase()+r.slice(1);if(e.isValidJSON(n.example)){const e=this.convertJsonToEnumString(n.example,s),i=this.getEnumTypeName(s);return{headerRef:"",renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: ${i}${this.nullable(n.nullable)};`,comment:e}}const i=this.typeNameToFileName(s),a=this.parseEnum(n,s);return a&&!this.enumsMap.has(i)?(this.enumsMap.set(i,{fileName:i,content:a.renderStr}),{headerRef:"",renderStr:""}):null}nullable(e){return""+(e?" | null":"")}getEnumTypeName(e){return this.config.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 s;if("enum"===r){s=`import type { ${this.getEnumTypeName(n)} } from '${this.config.importEnumPath}';`}else s=`import type { ${n} } from './${t}';`;return{headerRefStr:s,typeName:n,dataType:r}}parseObject(e,n){let r="",s="";if("object"===e.type){const i=e;if("object"==typeof i.additionalProperties){const e=this.parseArray(i.additionalProperties,n)??this.defaultReturn;r=e?.headerRef??"",s=e?.renderStr??""}s="boolean"==typeof i.additionalProperties?`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: Record<string, unknown>${this.nullable(i.nullable)};`:`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: ${e.type}${this.nullable(e.nullable)};`}return{headerRef:r,renderStr:s}}parseArray(n,r){const s=n,{items:i={},nullable:a,example:o}=s,u=i?.$ref;if(u){const{headerRefStr:n,typeName:s,dataType:i}=this.parseRef(u);if("enum"===i&&e.isValidJSON(o)){const e=this.typeNameToFileName(s),t=this.convertJsonToEnumString(o,s);this.enumsMap.set(e,{fileName:e,content:t})}const l="enum"===i?this.getEnumTypeName(s):s;return{headerRef:n,renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: Array<${l}>${this.nullable(a)};`,typeName:l}}const l=i?.type;let p="integer"===l?"number":l;return"object"===l&&(p="Record<string, unknown>"),{headerRef:"",renderStr:`${t}${r}${this.requiredFieldS.includes(r)?"":"?"}: Array<${p}>${this.nullable(a)};`,typeName:p}}parseBoolean(e,n){return"boolean"===e.type?`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: boolean${this.nullable(e.nullable)};`:""}parseEnum(e,n){if(!Array.isArray(e.enum))return null;const r=["integer","number"];if(this.config.erasableSyntaxOnly){if(e.type&&r.includes(e.type)){return{headerRef:"",renderStr:[`export const ${n} = {`,...e.enum.map(e=>`${t}NUMBER_${e}: ${e},`),"} as const;","",`export type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`].join("\n")}}if("string"===e.type){const r=e.enum.every(e=>!isNaN(Number(e)));return{headerRef:"",renderStr:[`export const ${n} = {`,...e.enum.map(e=>r?`${t}NUMBER_${e}: '${e}',`:`${t}${e.toUpperCase()}: '${e}',`),"} as const;","",`export type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`].join("\n")}}}else{if(e.type&&r.includes(e.type)){return{headerRef:"",renderStr:[`export enum ${n} {`,...e.enum.map(e=>`${t}NUMBER_${e} = ${e},`),"}"].join("\n")}}if("string"===e.type){const r=e.enum.every(e=>!isNaN(Number(e)));return{headerRef:"",renderStr:[`export enum ${n} {`,...e.enum.map(e=>r?`${t}NUMBER_${e} = '${e}',`:`${t}${e.toUpperCase()} = '${e}',`),"}"].join("\n")}}}return null}parseInteger(e,n){if(Array.isArray(e.enum)){const t=n.charAt(0).toUpperCase()+n.slice(1);return this.parseEnum(e,t)?.renderStr??""}return`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: number${this.nullable(e.nullable)};`}parseNumber(e,n){if("number"!==e.type)return null;if(e.enum){const t=this.handleEnum(e,n);if(t)return t}return{headerRef:"",renderStr:`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: number${this.nullable(e.nullable)};`}}convertJsonToEnumString(e,n){try{const r=JSON.parse(e);if(this.config.erasableSyntaxOnly){return`export const ${n} = {\n${Object.entries(r).map(([e,n])=>`${t}${e}: '${n}'`).join(",\n")}\n} as const;\n\nexport type ${this.getEnumTypeName(n)} = typeof ${n}[keyof typeof ${n}];`}return`export enum ${n} {\n${Object.entries(r).map(([e,n])=>`${t}${e} = '${n}'`).join(",\n")}\n}`}catch(e){return console.error("JSON 解析失败:",e),""}}parseString(e,n){if("string"!==e.type)return null;if(e.enum){const t=this.handleEnum(e,n);if(t)return t}return{headerRef:"",renderStr:`${t}${n}${this.requiredFieldS.includes(n)?"":"?"}: string${this.nullable(e.nullable)};`}}parseProperties(n,r){const s=[],i=[];for(const a in n){const o=n[a];if(o?.$ref){const{headerRefStr:n,typeName:r,dataType:u}=this.parseRef(o.$ref);i.includes(n)||i.push(n);const l=o,p=this.fieldComment(l);""!==p&&s.push(p);const c="enum"===u?this.getEnumTypeName(r):r;s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${c};`);const h=o.example;if("enum"===u&&h&&e.isValidJSON(h)){const e=this.typeNameToFileName(r),t=this.convertJsonToEnumString(h,r);this.enumsMap.set(e,{fileName:e,content:t})}continue}if("allOf"in o){const e=o.allOf?.[0];if(e?.$ref){const{headerRefStr:n,typeName:r,dataType:u}=this.parseRef(e.$ref),l="enum"===u?this.getEnumTypeName(r):r;i.includes(n)||i.push(n);const p=o,c=this.fieldComment(p);""!==c&&s.push(c),s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${l}${this.nullable(o.nullable)};`);continue}}const u=o,l=this.fieldComment(u);switch(""!==l&&s.push(l),u.type){case"array":{const e=this.parseArray(u,a)??this.defaultReturn;s.push(e?.renderStr??""),i.includes(e.headerRef)||r===e.typeName||i.push(e.headerRef)}break;case"boolean":s.push(this.parseBoolean(u,a));break;case"integer":s.push(this.parseInteger(u,a));break;case"number":{const e=this.parseNumber(u,a);e&&(e.headerRef&&!i.includes(e.headerRef)&&i.push(e.headerRef),s.push(e.renderStr))}break;case"string":if(u.enum){const n=a.charAt(0).toUpperCase()+a.slice(1);if(u.example&&e.isValidJSON(u.example)){const e=this.getEnumTypeName(n),r=`import type { ${e} } from '${this.config.importEnumPath}';`;i.includes(r)||i.push(r);const o=this.typeNameToFileName(n);if(!this.enumsMap.has(o)){const e=this.convertJsonToEnumString(u.example,n);this.enumsMap.set(o,{fileName:o,content:e})}s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${e}${this.nullable(u.nullable)};`)}else{const e=this.parseEnum(u,n);if(e?.renderStr){const r=this.getEnumTypeName(n),o=`import type { ${r} } from '${this.config.importEnumPath}';`;i.includes(o)||i.push(o);const l=this.typeNameToFileName(n);this.enumsMap.has(l)||this.enumsMap.set(l,{fileName:l,content:e.renderStr}),s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${r}${this.nullable(u.nullable)};`)}}}else u.format&&"binary"===u.format?s.push(`${t}${a}${this.requiredFieldS.includes(a)?"":"?"}: File${this.nullable(u.nullable)};`):s.push(this.parseString(u,a)?.renderStr??"");break;case"object":{const{headerRef:e,renderStr:t}=this.parseObject(u,a)??this.defaultReturn;s.push(t),i.includes(e)||i.push(e)}}}const a=[`export interface ${r} {`,...s,"}"];if(i.length>0){const e=i.filter(e=>""!==e);e.length>0&&e.push(""),a.unshift(...e)}const o=a.join("\n");return{headerRef:i.join("\n"),renderStr:o}}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),s=await this.generateContent(n,e);if(s){(s.includes("export enum ")||s.includes("export const ")&&s.includes("as const"))&&!this.enumsMap.has(r)?this.enumsMap.set(r,{fileName:r,content:s}):this.schemasMap.set(e,{fileName:r,content:s})}}}catch(e){throw console.error("解析过程出错:",e),e}}async writeEnums(){const t=[],n=[];for(const[,r]of this.enumsMap){const s=async({fileName:t,content:r})=>{n.push(`export * from './${t}';`);const s=`${this.config.saveEnumFolderPath}/${t}.ts`;await e.writeFileRecursive(s,r),e.log.info(`${s.padEnd(80)} - Write done!`)};t.push(s(r))}await Promise.all(t),await e.writeFileRecursive(`${this.config.saveEnumFolderPath}/index.ts`,n.join("\n")),e.log.success("Enums write done!")}async writeFile(){const t=[],n=[],r=`${this.config.saveTypeFolderPath}/models/`;for(const[,s]of this.schemasMap){const i=async({fileName:t,content:s})=>{n.push(`export * from './${t}';`);const i=`${r}${t}.ts`;await e.writeFileRecursive(i,s),e.log.info(`${i.padEnd(80)} - Write done!`)};t.push(i(s))}await Promise.all(t),await e.writeFileRecursive(`${r}index.ts`,n.join("\n")),e.log.success("Component parse & write done!")}async handle(){await this.parseData(),await this.writeFile(),await this.writeEnums()}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anl",
3
- "version": "25.1202.0",
3
+ "version": "25.1203.0",
4
4
  "description": "FE command line tool",
5
5
  "main": "bin/an-cli.js",
6
6
  "publishConfig": {