anl 1.2.3 → 1.2.6

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/bin/an-cli.js CHANGED
File without changes
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="1.2.3",i="FE command line tool",s="bin/an-cli.js",l={dev:"rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript -w",build:"rm -rf lib && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",pub:"sh publish.sh",ts:"tsc ./src/int.ts --noEmit --watch"},p={anl:"bin/an-cli.js"},t="Gleason <bianliuzhu@gmail.com>",o={"@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.3","@types/shelljs":"^0.8.11","@typescript-eslint/eslint-plugin":"^5.52.0","@typescript-eslint/parser":"^5.52.0",eslint:"^8.7.0",husky:"^8.0.3",rollup:"^2.64.0","rollup-plugin-cleandir":"^2.0.0","rollup-plugin-terser":"^7.0.2",typescript:"^4.5.4","openapi-types":"^9.3.0"},r={"app-root-path":"^3.1.0",cac:"^6.7.12",chalk:"4.*","clear-console":"^1.1.0",commander:"^10.0.0",inquirer:"^9.1.4","log-symbols":"^5.1.0",ora:"5.*","progress-estimator":"^0.3.0",shelljs:"^0.8.5"},n=["typescript","cli","typescript 脚手架","ts 脚手架","ts-cli","脚手架"],c=["package.json","README.md","lib","template"],u={type:"git",url:"https://github.com/bianliuzhu/an-cli.git"},a={name:"anl",version:e,description:i,main:s,scripts:l,bin:p,author:t,license:"ISC",devDependencies:o,dependencies:r,keywords:n,files:c,repository:u};exports.author=t,exports.bin=p,exports.default=a,exports.dependencies=r,exports.description=i,exports.devDependencies=o,exports.files=c,exports.keywords=n,exports.license="ISC",exports.main=s,exports.name="anl",exports.repository=u,exports.scripts=l,exports.version=e;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e="1.2.5",i="FE command line tool",s="bin/an-cli.js",l={dev:"rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript -w",build:"rimraf lib && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",pub:"sh publish.sh",ts:"tsc ./src/int.ts --noEmit --watch"},t={anl:"bin/an-cli.js"},p="Gleason <bianliuzhu@gmail.com>",r={"@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.3","@types/shelljs":"^0.8.11","@typescript-eslint/eslint-plugin":"^5.52.0","@typescript-eslint/parser":"^5.52.0",eslint:"^8.7.0",husky:"^8.0.3","openapi-types":"^9.3.0",prettier:"^3.3.2",rimraf:"^5.0.7",rollup:"^2.64.0","rollup-plugin-cleandir":"^2.0.0","rollup-plugin-terser":"^7.0.2",typescript:"^4.5.4"},o={"app-root-path":"^3.1.0",cac:"^6.7.12",chalk:"4.*","clear-console":"^1.1.0",commander:"^10.0.0",inquirer:"^9.1.4","log-symbols":"^5.1.0",ora:"5.*","progress-estimator":"^0.3.0",shelljs:"^0.8.5"},n=["typescript","cli","typescript 脚手架","ts 脚手架","ts-cli","脚手架"],c=["package.json","README.md","lib","template"],u={type:"git",url:"https://github.com/bianliuzhu/an-cli.git"},a={name:"anl",version:e,description:i,main:s,scripts:l,bin:t,author:p,license:"ISC",devDependencies:r,dependencies:o,keywords:n,files:c,repository:u};exports.author=p,exports.bin=t,exports.default=a,exports.dependencies=o,exports.description=i,exports.devDependencies=r,exports.files=c,exports.keywords=n,exports.license="ISC",exports.main=s,exports.name="anl",exports.repository=u,exports.scripts=l,exports.version=e;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../utils/index.js");const r="\t";exports.default=class{defalutReturn={headerRef:"",renderStr:""};schemas={};schemasMap=new Map;config;constructor(e,r){this.schemas=e,this.config=r}typeNameToFileName(e){return e.replace(/([A-Z][a-z])/g,(e=>"-"+e.toLowerCase())).replace(/([A-Z]{2,})/g,"-$1").toLowerCase().slice(1)}nameTheHumpCenterStroke(e){try{const r=e.replace("#/components/schemas/","");return{typeName:r,fileName:this.typeNameToFileName(r)}}catch(e){throw console.error(e),JSON.stringify(e)}}parseRef(e){if(!e)return{headerRefStr:"",typeName:""};const{fileName:r,typeName:t}=this.nameTheHumpCenterStroke(e);return{headerRefStr:`import { ${t} } from './${r}';`,typeName:t}}parseObject(e,r){let t="",s="";if("object"===e.type){const n=e;switch(typeof n.additionalProperties){case"boolean":return{headerRef:"",renderStr:""};case"object":{const e=this.parseArray(n.additionalProperties,r)??this.defalutReturn,{headerRef:a,renderStr:i}=e;t=a,s=i}}}return{headerRef:t,renderStr:s}}parseArray(e,t){let s="",n="";const a=e.items.$ref;if(a){const{headerRefStr:i,typeName:o}=this.parseRef(a);s=i;n=`${r}${t}${e?.nullable?"?:":":"} Array<${o}>;`}else{const s=e,a=s?.items;n=`${r}${t}${e?.nullable?"?:":":"} Array<${"integer"===a?.type?"number":a?.type}>;`}return{headerRef:s,renderStr:n}}parseBoolean(e,t){if("boolean"!==e.type)return"";return`${r}${t}: boolean;`}parseEnum(e,t){if("integer"!==e.type)return"";if(Array.isArray(e.enum)){return[`export enum ${t} {`,...e.enum.map((e=>`${r}NUMBER_${e} = ${e},`)),"}"].join("\n")}return""}parseInteger(e,t){return"integer"!==e.type?"":Array.isArray(e.enum)?this.parseEnum(e,t):`${r}${t}: number;`}parseNumber(e,t){if("number"!==e.type)return"";return`${r}${t}: number;`}parseString(e,t){if("string"!==e.type)return null;return{headerRef:"",renderStr:`${r}${t}: string;`}}parseProperties(e,t){try{const s=[],n=[];for(const t in e){const a=e[t];if(a.$ref){const{headerRefStr:e,typeName:i}=this.parseRef(a.$ref);!n.includes(e)&&n.push(e),s.push(`${r}${t}: ${i};`)}else switch(a.type){case"array":{const e=this.parseArray(a,t)??this.defalutReturn,{headerRef:r,renderStr:i}=e;s.push(i),!n.includes(r)&&n.push(r)}break;case"boolean":{const e=this.parseBoolean(a,t)??this.defalutReturn;s.push(e)}break;case"integer":{const e=this.parseInteger(a,t)??this.defalutReturn;s.push(e)}break;case"number":{const e=this.parseNumber(a,t)??this.defalutReturn;s.push(e)}break;case"string":{const{renderStr:e}=this.parseString(a,t)??this.defalutReturn;s.push(e)}break;case"object":{const{headerRef:e,renderStr:r}=this.parseObject(a,t)??this.defalutReturn;s.push(r),!n.includes(e)&&n.push(e)}}}const a=`export interface ${t} {`;return[...n,"\n",a,...s,"}"].join("\n")}catch(e){throw console.error(e),JSON.stringify(e)}}parse(){return new Promise(((e,r)=>{try{for(const e in this.schemas){const r=this.schemas[e],t="type"in r?r:null;if(("$ref"in r?r:null)&&console.warn("ReferenceObject 未处理"),t){const t="items"in r?r:r,s=this.typeNameToFileName(e);if("items"in t)console.log("未处理---------------------------\x3e",t.items);else switch(t.type){case"boolean":{const r=this.parseBoolean(t,e)??this.defalutReturn;this.schemasMap.set(e,{fileName:s,content:r})}break;case"integer":{const r=this.parseInteger(t,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"number":{const r=this.parseNumber(t,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"object":{const r=this.parseProperties(t.properties,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"string":{const{renderStr:r}=this.parseString(t,e)??this.defalutReturn;this.schemasMap.set(e,{fileName:s,content:r})}}}}e(!0)}catch(e){console.error(e),r(e)}}))}writeFileHabdler(){const r=[],t=[],s=`${this.config.saveTypeFolderPath}/models/`,n=({fileName:r,content:n})=>new Promise(((a,i)=>{try{t.push(`export * from './${r}';`),e.writeFileRecursive(`${s}${r}.ts`,n).finally((()=>a(void 0)))}catch(e){console.error(e,!0),i()}}));for(const[,e]of this.schemasMap)r.push(n(e));Promise.all(r).then((()=>{e.writeFileRecursive(`${s}/index.ts`,t.join("\n")).finally((()=>console.log("components done!")))}))}handle(){this.parse().then((()=>this.writeFileHabdler()))}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../utils/index.js");const r="\t";exports.default=class{defalutReturn={headerRef:"",renderStr:""};schemas={};schemasMap=new Map;config;constructor(e,r){this.schemas=e,this.config=r}typeNameToFileName(e){return e.replace(/([A-Z][a-z])/g,(e=>"-"+e.toLowerCase())).replace(/([A-Z]{2,})/g,"-$1").toLowerCase().slice(1)}nameTheHumpCenterStroke(e){try{const r=e.replace("#/components/schemas/","");return{typeName:r,fileName:this.typeNameToFileName(r)}}catch(e){throw console.error(e),JSON.stringify(e)}}parseRef(e){if(!e)return{headerRefStr:"",typeName:""};const{fileName:r,typeName:t}=this.nameTheHumpCenterStroke(e);return{headerRefStr:`import { ${t} } from './${r}';`,typeName:t}}parseObject(e,r){let t="",s="";if("object"===e.type){const n=e;switch(typeof n.additionalProperties){case"boolean":return{headerRef:"",renderStr:""};case"object":{const e=this.parseArray(n.additionalProperties,r)??this.defalutReturn,{headerRef:a,renderStr:i}=e;t=a,s=i}}}return{headerRef:t,renderStr:s}}parseArray(e,t){let s="",n="";const a=e.items.$ref;if(a){const{headerRefStr:i,typeName:o}=this.parseRef(a);s=i;n=`${r}${t}${e?.nullable?"?:":":"} Array<${o}>;`}else{const s=e,a=s?.items;n=`${r}${t}${e?.nullable?"?:":":"} Array<${"integer"===a?.type?"number":a?.type}>;`}return{headerRef:s,renderStr:n}}parseBoolean(e,t){if("boolean"!==e.type)return"";return`${r}${t}: boolean;`}parseEnum(e,t){if("integer"!==e.type)return"";if(Array.isArray(e.enum)){return[`export enum ${t} {`,...e.enum.map((e=>`${r}NUMBER_${e} = ${e},`)),"}"].join("\n")}return""}parseInteger(e,t){return"integer"!==e.type?"":Array.isArray(e.enum)?this.parseEnum(e,t):`${r}${t}: number;`}parseNumber(e,t){if("number"!==e.type)return"";return`${r}${t}: number;`}parseString(e,t){if("string"!==e.type)return null;return{headerRef:"",renderStr:`${r}${t}: string;`}}parseProperties(e,t){try{const s=[],n=[];for(const t in e){const a=e[t];if(a.$ref){const{headerRefStr:e,typeName:i}=this.parseRef(a.$ref);!n.includes(e)&&n.push(e),s.push(`${r}${t}: ${i};`)}else switch(a.type){case"array":{const e=this.parseArray(a,t)??this.defalutReturn,{headerRef:r,renderStr:i}=e;s.push(i),!n.includes(r)&&n.push(r)}break;case"boolean":{const e=this.parseBoolean(a,t)??this.defalutReturn;s.push(e)}break;case"integer":{const e=this.parseInteger(a,t)??this.defalutReturn;s.push(e)}break;case"number":{const e=this.parseNumber(a,t)??this.defalutReturn;s.push(e)}break;case"string":{const{renderStr:e}=this.parseString(a,t)??this.defalutReturn;s.push(e)}break;case"object":{const{headerRef:e,renderStr:r}=this.parseObject(a,t)??this.defalutReturn;s.push(r),!n.includes(e)&&n.push(e)}}}const a=`export interface ${t} {`;return[...n,"\n",a,...s,"\t}"].join("\n")}catch(e){throw console.error(e),JSON.stringify(e)}}parse(){return new Promise(((e,r)=>{try{for(const e in this.schemas){const r=this.schemas[e],t="type"in r?r:null;if(("$ref"in r?r:null)&&console.warn("ReferenceObject 未处理"),t){const t="items"in r?r:r,s=this.typeNameToFileName(e);if("items"in t)console.log("未处理---------------------------\x3e",t.items);else switch(t.type){case"boolean":{const r=this.parseBoolean(t,e)??this.defalutReturn;this.schemasMap.set(e,{fileName:s,content:r})}break;case"integer":{const r=this.parseInteger(t,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"number":{const r=this.parseNumber(t,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"object":{const r=this.parseProperties(t.properties,e);this.schemasMap.set(e,{fileName:s,content:r})}break;case"string":{const{renderStr:r}=this.parseString(t,e)??this.defalutReturn;this.schemasMap.set(e,{fileName:s,content:r})}}}}e(!0)}catch(e){console.error(e),r(e)}}))}writeFileHabdler(){const r=[],t=[],s=`${this.config.saveTypeFolderPath}/models/`,n=({fileName:r,content:n})=>new Promise(((a,i)=>{try{t.push(`export * from './${r}';`),e.writeFileRecursive(`${s}${r}.ts`,n).finally((()=>a(void 0)))}catch(e){console.error(e,!0),i()}}));for(const[,e]of this.schemasMap)r.push(n(e));Promise.all(r).then((()=>{e.writeFileRecursive(`${s}/index.ts`,t.join("\n")).finally((()=>console.log("components done!")))}))}handle(){this.parse().then((()=>this.writeFileHabdler()))}};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../utils/index.js");const t="\t";var s;!function(e){e.GET="get",e.PUT="put",e.POST="post",e.DELETE="delete",e.OPTIONS="options",e.HEAD="head",e.PATCH="patch",e.TRACE="trace"}(s||(s={}));const n={namespace:"",payload:{path:[],query:[],body:[]},response:"",_response:"",fileName:"",method:"",path:""};class r{pathsObject={};nonArrayType=["boolean","object","number","string","integer"];pathKey="";contentBody=n;Map=new Map;config;constructor(e,t){this.pathsObject=e,this.config=t}pathTranslantionName(e,t){return{name:e.slice(1).replace(/\/\{\w+\}/g,(()=>"")).replace(/\/\w/g,(e=>{const[,t]=e.split("");return t.toUpperCase()})).replace("api",""),method:t}}pathTranslantionFileName(e){const t=e.replace("/api","").replace(/\{\w+\}/g,(e=>`$${e}`));return{name:e.slice(1).replace(/\//g,"-").replace("api-",""),path:t}}typeNameToFileName(e){return e.replace(/([A-Z][a-z])/g,(e=>"-"+e.toLowerCase())).replace(/([A-Z]{2,})/g,"-$1").toLowerCase().slice(1)}propertiesParse(e){const s=[];for(const n in e){const r=e[n],a=this.schemaParse(r),o=`${t}${t}${n}:${a}`;s.push(o)}return s.join("\n")}nonArraySchemaObjectParse(e){if(!e)return"";switch(e.type){case"boolean":return"boolean";case"integer":case"number":return"number";case"object":return this.propertiesParse(e.properties);case"string":return"string";default:return""}}arraySchemaObjectParse(e){if("array"!==e.type)return"";const{items:t}=e,s="$ref"in t?t:null,n=t;if(s){return`Array<${this.referenceObjectParse(s)}>`}if(n){return`Array<${this.schemaParse(t)}>`}return""}referenceObjectParse(e){if(!e)return"";const t=e.$ref.replace("#/components/schemas/","");return`import('../models/${this.typeNameToFileName(t)}').${t}`}schemaParse(e){if(!e)return"";const t=e?.type,s="$ref"in e?e:null,n="array"===t?e:null,r=t&&this.nonArrayType.includes(t)?e:null;return s?this.referenceObjectParse(s):n?this.arraySchemaObjectParse(n):r?this.nonArraySchemaObjectParse(r):""}responseObjectParse(e){const t=e.content;if(!t)return"";const{schema:s}=Object.values(t)[0];if(s){return this.schemaParse(s).split("\n")}}responseHandle(e){const t=e[200],s="content"in t?t:null,n="$ref"in t?t:null;if(null===s&&null===n&&(this.contentBody.response="type Response = void",this.contentBody._response="void"),n){const e=this.referenceObjectParse(n);this.contentBody.response=`type Response = ${e}['responseObject']`,this.contentBody._response=e}if(s){const e=this.responseObjectParse(s);this.contentBody.response=`type Response = ${e}['responseObject']`,this.contentBody._response=`${e}`}}requestBodyObjectParse(e){const{schema:s}=Object.values(e.content)[0];if(s){const e=s?.type,n="$ref"in s?s:null,r="array"===e?s:null,a=e&&this.nonArrayType.includes(e)?s:null;if(n){const e=this.referenceObjectParse(n);return`${t}type Body = ${e}`}if(r){const e=this.arraySchemaObjectParse(r);return`${t}type Body = ${e}`}if(a){const e=this.nonArraySchemaObjectParse(a);return[`${t}interface Body {`,e,"}"]}}}requestBodyParse(e){if(!e)return"";const s="$ref"in e?e:null,n="content"in e?e:null;if(s){const e=this.referenceObjectParse(s);return`${t}type Body = ${e}`}return n?this.requestBodyObjectParse(n):""}requestParametersParse(e){const s=[],n=[];e?.map((e=>{const r="$ref"in e?e:null,a="name"in e?e:null;if(r){const e=this.referenceObjectParse(r);return console.log(this.pathKey,e,"item 是 ReferenceObject 类型","----为处理---"),e}if(a){if("path"===a.in){const e=this.schemaParse(a.schema);s.push(`${t}${t}type ${a.name} = ${e};`),this.contentBody.payload._path?this.contentBody.payload._path[a.name]=e:this.contentBody.payload._path={[a.name]:e}}if("query"===a.in){const e=this.schemaParse(a.schema);n.push(`${t}${t}${a.name}: ${e};`),this.contentBody.payload._query?this.contentBody.payload._query[a.name]=e:this.contentBody.payload._query={[a.name]:e}}}})),0!==s.length&&(s.unshift(`${t}namespace Path {`),s.push(`${t}}`)),0!==n.length&&(n.unshift(`${t}interface Query {`),n.push(`${t}}`)),this.contentBody.payload.path=s,this.contentBody.payload.query=n}requestHandle(e){if(e.parameters&&this.requestParametersParse(e.parameters),e.requestBody){const t=this.requestBodyParse(e.requestBody);if(t&&Array.isArray(t))this.contentBody.payload.body=t;else{const e=t?.split("\n")||[];this.contentBody.payload.body=e}}}parsePathItemObject(e){for(const t in s){const s=e[t.toLowerCase()];if(s){const{name:e,method:n}=this.pathTranslantionName(this.pathKey,t);this.requestHandle(s),this.responseHandle(s.responses),this.contentBody.method=n,this.contentBody.namespace=e+n}}}initialize(){return new Promise(((e,t)=>{try{for(const e in this.pathsObject){const t=this.pathsObject[e];if(this.pathKey=e,t){const s=this.pathTranslantionFileName(e);this.contentBody.fileName=s.name,this.contentBody.path=s.path,this.parsePathItemObject(t),this.Map.set(e,this.contentBody),this.contentBody={namespace:"",payload:{path:[],query:[],body:[]},response:"",_response:"",fileName:"",method:"",path:""}}}e(this.Map)}catch(e){t(e)}}))}writeApiListFile(e){const{namespace:t,payload:s,method:n,path:r,_response:a}=e,{_path:o,_query:i,body:c}=s,p=t.replace(t[0],t[0].toLowerCase()),h=e=>{const s=[];for(const n in e)s.push(`${n}: ${t}.Path.${n}`);return s.length>1?s.join(","):s.join("")},l=o?h(o):"",y=i?`params: ${t}.Query`:"",u=c.length>0?`params: ${t}.Body`:"",d={};l&&(d.apiParamsPath=l),y&&(d.apiParamsQuery=y),u&&(d.apiParamsBody=u);const m=Object.keys(d).length>=2;return[`export const ${p} = `,"(",o?h(o):"",""+(m?",":""),i?`params: ${t}.Query`:"",c.length>0?`params: ${t}.Body`:"",")"," => ",`${n}${a?`<${t}.Response>`:""}`,"(`"+r+"`"+(y||u?", params":"")+")"].join("")}writeFileHabdler(){const s=[],n=[],r=this.config.saveTypeFolderPath,a=[],o=(s,o)=>new Promise(((s,i)=>{try{const{namespace:c,payload:p,response:h,fileName:l,method:y}=o;!a.includes(y)&&a.push(y);const u=[`declare namespace ${c} {`,...p.path,...p.query,...p.body,`${t}${h}`,"}"],d=this.writeApiListFile(o);n.push(d,"\n"),e.writeFileRecursive(`${r}/api/${l}.d.ts`,u.join("\n")).then((()=>s(1))).catch((e=>{i(e),console.error(e)}))}catch(e){console.error(e,!0),i()}}));for(const[e,t]of this.Map)s.push(o(0,t));Promise.all(s).then((()=>{n.unshift(`import { ${a.join(",")} } from './api';`,"\n"),e.clearDir(this.config.apiListFilePath+"/index.ts").finally((()=>{e.writeFileRecursive(this.config.apiListFilePath+"/index.ts",n.join("\n")).then((()=>{console.log("path parse done!"),this.Map=new Map})).catch((e=>{console.error("-----------\x3e",e)}))}))}))}handle(){this.initialize().then((()=>this.writeFileHabdler()))}}exports.PathParse=r,exports.default=r;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../../utils/index.js");const t="\t";var n;!function(e){e.GET="get",e.PUT="put",e.POST="post",e.DELETE="delete",e.OPTIONS="options",e.HEAD="head",e.PATCH="patch",e.TRACE="trace"}(n||(n={}));const s={namespace:"",payload:{path:[],query:[],body:[]},response:"",_response:"",fileName:"",method:"",path:""};class r{pathsObject={};nonArrayType=["boolean","object","number","string","integer"];pathKey="";contentBody=s;Map=new Map;config;constructor(e,t){this.pathsObject=e,this.config=t}pathTranslantionName(e,t){let n="";e.startsWith("/")||(n="/"+e);return{name:n.slice(1).replace(/\/\{\w+\}/g,(()=>"")).replace(/\/\w/g,(e=>{const[,t]=e.split("");return t.toUpperCase()})).replace(/(\/api)|(api)/g,""),method:t}}pathTranslantionFileName(e){let t="";e.startsWith("/")||(t="/"+e);const n=t.replace(/(\/api)|(api)/g,"").replace(/\{\w+\}/g,(e=>`$${e}`));return{name:t.slice(1).replace(/\//g,"-").replace("api-",""),path:n}}typeNameToFileName(e){return e.replace(/([A-Z][a-z])/g,(e=>"-"+e.toLowerCase())).replace(/([A-Z]{2,})/g,"-$1").toLowerCase().slice(1)}propertiesParse(e){const n=[];for(const s in e){const r=e[s],a=this.schemaParse(r);let o="";o=Array.isArray(a)?`${t}${t}${s}:{${a.join("\n")}};`:`${t}${t}${s}:${a};`,n.push(o)}return n}nonArraySchemaObjectParse(e){if(!e)return"unknown";switch(e.type){case"boolean":return"boolean";case"integer":case"number":return"number";case"object":return this.propertiesParse(e.properties);case"string":return"string";default:return"unknown"}}arraySchemaObjectParse(e){if("array"!==e.type)return"";const{items:t}=e,n="$ref"in t?t:null,s=t;if(n){return`Array<${this.referenceObjectParse(n)}>`}if(s){const e=this.schemaParse(t);if(Array.isArray(e)){return`Array<{${e.join("\n")}}>`}return`Array<${e}>`}return""}referenceObjectParse(e){if(!e)return"";const t=e.$ref.replace("#/components/schemas/","");return`import('../models/${this.typeNameToFileName(t)}').${t}`}schemaParse(e){if(!e)return"unknown";const t=e?.type,n="$ref"in e?e:null,s="array"===t?e:null,r=t&&this.nonArrayType.includes(t)?e:null;return n?this.referenceObjectParse(n):s?this.arraySchemaObjectParse(s):r?this.nonArraySchemaObjectParse(r):"unknown"}responseObjectParse(e){const t=e.content;if(!t)return"";const{schema:n}=Object.values(t)[0];if(n){return this.schemaParse(n)}}responseHandle(e){const t=e[200],n="content"in t?t:null,s="$ref"in t?t:null;if(null===n&&null===s&&(this.contentBody.response="type Response = void",this.contentBody._response="void"),s){const e=this.referenceObjectParse(s);this.contentBody.response=`type Response = ${e}`,this.contentBody._response=e}if(n){const e=this.responseObjectParse(n);Array.isArray(e)?(this.contentBody.response=`interface Response {${e.join("\n")}} `,this.contentBody._response=`${e.join("\n")}`):(this.contentBody.response=`type Response = ${e}`,this.contentBody._response=`${e}`)}}requestBodyObjectParse(e){const n=Object.values(e.content),{schema:s}=Array.isArray(n)&&n.length>0?n[0]:{schema:null};if(s){const e=s?.type,n="$ref"in s?s:null,r="array"===e?s:null,a=e&&this.nonArrayType.includes(e)?s:null;if(n){const e=this.referenceObjectParse(n);return`${t}type Body = ${e}`}if(r){const e=this.arraySchemaObjectParse(r);return`${t}type Body = ${e}`}if(a){const e=this.nonArraySchemaObjectParse(a);return Array.isArray(e)?0===e.length?[`${t}type Body = void`]:[`${t}interface Body {`,...e,"}"]:[`${t}type Body = ${e}`]}}}requestBodyParse(e){if(!e)return"{}";const n="$ref"in e?e:null,s="content"in e?e:null;if(n){const e=this.referenceObjectParse(n);return`${t}type Body = ${e}`}return s&&"[object Object]"===String(e)&&0!==Reflect.ownKeys(e).length?this.requestBodyObjectParse(s):"{}"}requestParametersParse(e){const n=[],s=[];e?.map((e=>{const r="$ref"in e?e:null,a="name"in e?e:null;if(r){const e=this.referenceObjectParse(r);return console.log(this.pathKey,e,"item 是 ReferenceObject 类型","----未处理---"),e}if(a){if("path"===a.in){const e=this.schemaParse(a.schema);n.push(`${t}${t}type ${a.name} = ${e};`),this.contentBody.payload._path?"string"==typeof e?this.contentBody.payload._path[a.name]=e:console.log(e):"string"==typeof e?this.contentBody.payload._path={[a.name]:e}:console.log(e)}if("query"===a.in){const e=this.schemaParse(a.schema);s.push(`${t}${t}${a.name}: ${e};`),this.contentBody.payload._query?"string"==typeof e?this.contentBody.payload._query[a.name]=e:console.log(e):"string"==typeof e?this.contentBody.payload._query={[a.name]:e}:console.log(e)}}})),0!==n.length&&(n.unshift(`${t}namespace Path {`),n.push(`${t}}`)),0!==s.length&&(s.unshift(`${t}interface Query {`),s.push(`${t}}`)),this.contentBody.payload.path=n,this.contentBody.payload.query=s}requestHandle(e){if(e.parameters&&this.requestParametersParse(e.parameters),e.requestBody){const t=this.requestBodyParse(e.requestBody);if(Array.isArray(t))this.contentBody.payload.body=t;else if(t){const e=t?.split("\n")||[];this.contentBody.payload.body=e}}}parsePathItemObject(e){for(const t in n){const n=e[t.toLowerCase()];if(n){this.pathKey;const{name:e,method:s}=this.pathTranslantionName(this.pathKey,t);this.requestHandle(n),this.responseHandle(n.responses),this.contentBody.method=s,this.contentBody.namespace=e+s}}}initialize(){return new Promise(((e,t)=>{try{for(const e in this.pathsObject){const t=this.pathsObject[e];if(this.pathKey=e,t){const n=this.pathTranslantionFileName(e);this.contentBody.fileName=n.name,this.contentBody.path=n.path,this.parsePathItemObject(t),this.Map.set(e,this.contentBody),this.contentBody={namespace:"",payload:{path:[],query:[],body:[]},response:"",_response:"",fileName:"",method:"",path:""}}}e(this.Map)}catch(e){t(e)}}))}writeApiListFile(e){const{namespace:t,payload:n,method:s,path:r,_response:a}=e,{_path:o,_query:i,body:c}=n,p=t.replace(t[0],t[0].toLowerCase()),h=e=>{const n=[];for(const s in e)n.push(`${s}: ${t}.Path.${s}`);return n.length>1?n.join(","):n.join("")},l=o?h(o):"",y=i?`params: ${t}.Query`:"",u=c.length>0?`params: ${t}.Body`:"",d={};l&&(d.apiParamsPath=l),y&&(d.apiParamsQuery=y),u&&(d.apiParamsBody=u);const m=Object.keys(d).length>=2;return[`export const ${p} = `,"(",o?h(o):"",""+(m?",":""),i?`params: ${t}.Query`:"",c.length>0?`params: ${t}.Body`:"",")"," => ",`${s}${a?`<${t}.Response>`:""}`,"(`"+r+"`"+(y||u?", params":"")+")"].join("")}writeFileHabdler(){const n=[],s=[],r=this.config.saveTypeFolderPath,a=[],o=(n,o)=>new Promise(((n,i)=>{try{const{namespace:c,payload:p,response:h,fileName:l,method:y}=o;!a.includes(y)&&a.push(y);const u=[`declare namespace ${c} {`,...p.path,...p.query,...p.body,`${t}${h}`,"}"],d=this.writeApiListFile(o);s.push(d,"\n"),e.writeFileRecursive(`${r}/api/${l}.d.ts`,u.join("\n")).then((()=>n(1))).catch((e=>{i(e),console.error(e)}))}catch(e){console.error(e,!0),i()}}));for(const[e,t]of this.Map)n.push(o(0,t));Promise.all(n).then((()=>{s.unshift(`import { ${a.join(",")} } from './api';`,"\n"),e.clearDir(this.config.apiListFilePath+"/index.ts").finally((()=>{e.writeFileRecursive(this.config.apiListFilePath+"/index.ts",s.join("\n")).then((()=>{console.log("path parse done!"),this.Map=new Map})).catch((e=>{console.error("-----------\x3e",e)}))}))}))}handle(){this.initialize().then((()=>this.writeFileHabdler()))}}exports.PathParse=r,exports.default=r;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),s=require("../utils/index.js"),t=require("./core/components.js"),r=require("./core/get-data.js"),a=require("./core/path.js");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=n(e);const o={saveTypeFolderPath:"apps/types",apiListFilePath:"apps/services",swaggerJsonUrl:"",indent:"\t",headers:{}};exports.Main=class{schemas={};paths={};handle(e){return new Promise((s=>{if(!e.swaggerJsonUrl)return s({});r.getSwaggerJson(e).then((r=>{const n=r;this.schemas=n.components?.schemas,this.paths=n.paths;new t.default(this.schemas,e).handle();new a.PathParse(this.paths,e).handle(),s({})})).catch((()=>{s({})}))}))}initialize(){const e=process.cwd()+"/an.config.json";s.isFileExisted(e).then((()=>{i.default.readFile(e,"utf8",((e,t)=>{if(e)return void console.error(e);const r=JSON.parse(t);console.log("config ----\x3e",r),s.clearDir(r.saveTypeFolderPath).then((()=>this.handle(r)))}))})).catch((t=>{s.clearDir(o.saveTypeFolderPath).then((()=>{s.writeFileRecursive(e,JSON.stringify(o,null,2)).catch((e=>{console.log(e)})).finally((()=>{this.handle(o)}))})),console.error(t)}))}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("fs"),s=require("../utils/index.js"),t=require("./core/components.js"),r=require("./core/get-data.js"),a=require("./core/path.js"),o=require("shelljs");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=i(e);const l={saveTypeFolderPath:"apps/types",apiListFilePath:"apps/services",swaggerJsonUrl:"",indent:"\t",headers:{}};exports.Main=class{schemas={};paths={};handle(e){return new Promise((s=>{if(!e.swaggerJsonUrl)return s({});r.getSwaggerJson(e).then((r=>{const o=r;this.schemas=o.components?.schemas,this.paths=o.paths;new t.default(this.schemas,e).handle();new a.PathParse(this.paths,e).handle(),s({})})).catch((()=>{s({})}))}))}initialize(){const e=process.cwd()+"/an.config.json";s.isFileExisted(e).then((()=>{n.default.readFile(e,"utf8",((e,t)=>{if(e)return void console.error(e);const r=JSON.parse(t);console.log("config ----\x3e",r),s.clearDir(r.saveTypeFolderPath).then((()=>{this.handle(r).then((e=>{console.log("format: ",`prettier --write "${r.saveTypeFolderPath}/**/*.{ts,d.ts}"`),o.exec(`prettier --write "${r.saveTypeFolderPath}/**/*.{ts,d.ts}"`)})).catch((e=>{console.log(e)}))}))}))})).catch((t=>{s.clearDir(l.saveTypeFolderPath).then((()=>{s.writeFileRecursive(e,JSON.stringify(l,null,2)).catch((e=>{console.log(e)})).finally((()=>{this.handle(l)}))})),console.error(t)}))}};
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "anl",
3
- "version": "1.2.3",
3
+ "version": "1.2.6",
4
4
  "description": "FE command line tool",
5
5
  "main": "bin/an-cli.js",
6
6
  "scripts": {
7
7
  "dev": "rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript -w",
8
- "build": "rm -rf lib && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
9
- "pub": "sh publish.sh",
8
+ "build": "rimraf lib && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
9
+ "pub": "bash publish.sh",
10
10
  "ts": "tsc ./src/int.ts --noEmit --watch"
11
11
  },
12
12
  "bin": {
@@ -27,11 +27,13 @@
27
27
  "@typescript-eslint/parser": "^5.52.0",
28
28
  "eslint": "^8.7.0",
29
29
  "husky": "^8.0.3",
30
+ "openapi-types": "^9.3.0",
31
+ "prettier": "^3.3.2",
32
+ "rimraf": "^5.0.7",
30
33
  "rollup": "^2.64.0",
31
34
  "rollup-plugin-cleandir": "^2.0.0",
32
35
  "rollup-plugin-terser": "^7.0.2",
33
- "typescript": "^4.5.4",
34
- "openapi-types": "^9.3.0"
36
+ "typescript": "^4.5.4"
35
37
  },
36
38
  "dependencies": {
37
39
  "app-root-path": "^3.1.0",