anl 26.107.2 → 26.108.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="26.107.2",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
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="26.108.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:"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);const s="\t";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}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:`${s}${n}${this.requiredFieldS.includes(n)?"":"?"}: ${i}${this.nullable(e.nullable)};`,comment:t}}const i=this.typeNameToFileName(r),a=this.parseEnum(e,r);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.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 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,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?`${s}${t}${this.requiredFieldS.includes(t)?"":"?"}: Record<string, unknown>${this.nullable(i.nullable)};`:`${s}${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:a,example:o}=r,u=i?.$ref;if(u){const{headerRefStr:e,typeName:r,dataType:i}=this.parseRef(u);if("enum"===i&&t.isValidJSON(o)){const e=this.typeNameToFileName(r),t=this.convertJsonToEnumString(o,r);this.enumsMap.set(e,{fileName:e,content:t})}const l="enum"===i?this.getEnumTypeName(r):r;return{headerRef:e,renderStr:`${s}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${l}>${this.nullable(a)};`,typeName:l}}const l=i?.type;let c="integer"===l?"number":l;return"object"===l&&(c="Record<string, unknown>"),{headerRef:"",renderStr:`${s}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${c}>${this.nullable(a)};`,typeName:c}}parseBoolean(e,t){return"boolean"===e.type?`${s}${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:s,treatStringAsNumeric:i}=n,a=r?.[t];return"string"==typeof a&&a.trim()?a:s||"string"==typeof e&&i?`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:a,descriptionMap:o}=this.extractEnumMetadata(e),u=this.config.enmuConfig.erasableSyntaxOnly,l=r.map((e,t)=>{const r=this.resolveEnumMemberName(e,t,{customNames:a,isNumericEnum:n,treatStringAsNumeric:i}),l=n?`${e}`:`'${String(e)}'`,c=u?`${r}: ${l},`:`${r} = ${l},`,p=o?.[String(e)];return p?[`${s}/** ${p} */`,`${s}${c}`].join("\n"):`${s}${c}`});return l.length?u?{headerRef:"",renderStr:[`export const ${t} = {`,...l,"} as const;","",`export type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`].join("\n")}:{headerRef:"",renderStr:[`export enum ${t} {`,...l,"}"].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`${s}${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:`${s}${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])=>`${s}${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])=>`${s}${e} = '${t}'`).join(",\n")}\n}`}catch(e){return console.error("JSON 解析失败:",e),""}}parseString(e,t){if("string"!==e.type)return null;if(e.enum){const n=this.handleEnum(e,t);if(n)return n}return{headerRef:"",renderStr:`${s}${t}${this.requiredFieldS.includes(t)?"":"?"}: string${this.nullable(e.nullable)};`}}parseProperties(e,n){const r=[],i=[];for(const a in e){const o=e[a];if(o?.$ref){const{headerRefStr:e,typeName:n,dataType:u}=this.parseRef(o.$ref);i.includes(e)||i.push(e);const l=o,c=this.fieldComment(l);""!==c&&r.push(c);const p="enum"===u?this.getEnumTypeName(n):n;r.push(`${s}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${p};`);const m=o.example;if("enum"===u&&m&&t.isValidJSON(m)){const e=this.typeNameToFileName(n),t=this.convertJsonToEnumString(m,n);this.enumsMap.set(e,{fileName:e,content:t})}continue}if("allOf"in o){const e=o.allOf?.[0];if(e?.$ref){const{headerRefStr:t,typeName:n,dataType:u}=this.parseRef(e.$ref),l="enum"===u?this.getEnumTypeName(n):n;i.includes(t)||i.push(t);const c=o,p=this.fieldComment(c);""!==p&&r.push(p),r.push(`${s}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${l}${this.nullable(o.nullable)};`);continue}}const u=o,l=this.fieldComment(u);switch(""!==l&&r.push(l),u.type){case"array":{const e=this.parseArray(u,a)??this.defaultReturn;r.push(e?.renderStr??""),i.includes(e.headerRef)||n===e.typeName||i.push(e.headerRef)}break;case"boolean":r.push(this.parseBoolean(u,a));break;case"integer":r.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),r.push(e.renderStr))}break;case"string":if(u.enum){const e=a.charAt(0).toUpperCase()+a.slice(1);if(u.example&&t.isValidJSON(u.example)){const t=this.getEnumTypeName(e),n=`import type { ${t} } from '${this.config.importEnumPath}';`;i.includes(n)||i.push(n);const o=this.typeNameToFileName(e);if(!this.enumsMap.has(o)){const t=this.convertJsonToEnumString(u.example,e);this.enumsMap.set(o,{fileName:o,content:t})}r.push(`${s}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${t}${this.nullable(u.nullable)};`)}else{const t=this.parseEnum(u,e);if(t?.renderStr){const n=this.getEnumTypeName(e),o=`import type { ${n} } from '${this.config.importEnumPath}';`;i.includes(o)||i.push(o);const l=this.typeNameToFileName(e);this.enumsMap.has(l)||this.enumsMap.set(l,{fileName:l,content:t.renderStr}),r.push(`${s}${a}${this.requiredFieldS.includes(a)?"":"?"}: ${n}${this.nullable(u.nullable)};`)}}}else u.format&&"binary"===u.format?r.push(`${s}${a}${this.requiredFieldS.includes(a)?"":"?"}: File${this.nullable(u.nullable)};`):r.push(this.parseString(u,a)?.renderStr??"");break;case"object":{const{headerRef:e,renderStr:t}=this.parseObject(u,a)??this.defaultReturn;r.push(t),i.includes(e)||i.push(e)}}}const a=[`export interface ${n} {`,...r,"}"];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 e=[],n=[];for(const[,r]of this.enumsMap){const s=async({fileName:e,content:r})=>{n.push(`export * from './${e}';`);const s=`${this.config.saveEnumFolderPath}/${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(`${this.config.saveEnumFolderPath}/index.ts`,n),t.log.success("Enums write done!")}async writeFile(){const e=[],n=[],r=`${this.config.saveTypeFolderPath}/models/`;for(const[,s]of this.schemasMap){const i=async({fileName:e,content:s})=>{n.push(`export * from './${e}';`);const i=`${r}${e}.ts`;await t.writeFileRecursive(i,s),t.log.info(`${i.padEnd(80)} - Write done!`)};e.push(i(s))}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 s=n.filter(Boolean);if(!s.length)return;let i=[];if(this.appendMode)try{i=(await r.default.promises.readFile(e,"utf8")).split("\n").filter(e=>""!==e.trim())}catch{i=[]}const a=new Set(i),o=[...i,...s.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 u="enum"===r?this.getEnumTypeName(i):i;return{headerRef:e,renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${u}>${this.nullable(s)};`,typeName:u}}const u=r?.type;let l="integer"===u?"number":u;return"object"===u&&(l="Record<string, unknown>"),{headerRef:"",renderStr:`${this.config.formatting?.indentation}${n}${this.requiredFieldS.includes(n)?"":"?"}: Array<${l}>${this.nullable(s)};`,typeName:l}}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,u=i.map((e,t)=>{const i=this.resolveEnumMemberName(e,t,{customNames:s,isNumericEnum:n,treatStringAsNumeric:r}),u=n?`${e}`:`'${String(e)}'`,l=o?`${i}: ${u},`:`${i} = ${u},`,c=a?.[String(e)];return c?[`${this.config.formatting?.indentation}/** ${c} */`,`${this.config.formatting?.indentation}${l}`].join("\n"):`${this.config.formatting?.indentation}${l}`});return u.length?o?{headerRef:"",renderStr:[`export const ${t} = {`,...u,"} as const;","",`export type ${this.getEnumTypeName(t)} = typeof ${t}[keyof typeof ${t}];`].join("\n")}:{headerRef:"",renderStr:[`export enum ${t} {`,...u,"}"].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),""}}parseString(e,t){if("string"!==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)?"":"?"}: string${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:u}=this.parseRef(a.$ref);r.includes(e)||o===n||r.push(e);const l=a,c=this.fieldComment(l);""!==c&&i.push(c);const m="enum"===u?this.getEnumTypeName(o):o;i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${m};`);const h=a.example;if("enum"===u&&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:u}=this.parseRef(e.$ref),l="enum"===u?this.getEnumTypeName(o):o;r.includes(t)||o===n||r.push(t);const c=a,m=this.fieldComment(c);""!==m&&i.push(m),i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${l}${this.nullable(a.nullable)};`);continue}}const o=a,u=this.fieldComment(o);switch(""!==u&&i.push(u),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 u=this.typeNameToFileName(e);this.enumsMap.has(u)||this.enumsMap.set(u,{fileName:u,content:t.renderStr}),i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: ${n}${this.nullable(o.nullable)};`)}}}else o.format&&"binary"===o.format?i.push(`${this.config.formatting?.indentation}${s}${this.requiredFieldS.includes(s)?"":"?"}: File${this.nullable(o.nullable)};`):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)}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anl",
3
- "version": "26.107.2",
3
+ "version": "26.108.0",
4
4
  "description": "FE command line tool",
5
5
  "main": "bin/an-cli.js",
6
6
  "publishConfig": {