ts-proto-client 1.0.3 → 1.0.4
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/README.md +1 -1
- package/dist/index.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as q}from"commander";import
|
|
3
|
-
`))}let a=[...s].sort();e.push({[n]:['import { service } from "
|
|
2
|
+
import{Command as q}from"commander";import b from"fs";import R from"path";import y from"fs";import j from"path";import{Root as P,Service as x}from"protobufjs";import u from"path";import w from"fs";var g=["get","post","put","delete","patch"];function C(t){let o=[t,u.join(t,"/third_party/googleapis")],e=new P;return e.resolvePath=(n,p)=>{for(let s of o){let i=u.join(s,p);if(w.existsSync(i))return i}return p},e}function h(t,o=[]){if(!t.nested)return o;for(let e of Object.values(t.nested))e instanceof x&&o.push(e),h(e,o);return o}function A(t){let o=t.parsedOptions?.find(n=>n["(google.api.http)"])?.["(google.api.http)"];if(!o)return null;let e=g.find(n=>o[n]);return e?{method:e,url:o[e],summary:t.options?.["(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation).summary"]}:null}function v(t,o){let e=u.resolve(process.cwd(),t),n=C(e);n.loadSync(u.join(e,o));let p=h(n),s={};for(let i of p){let a=[];for(let r of Object.values(i.methods)){let m=A(r);m&&a.push({name:r.name,requestType:r.requestType,responseType:r.responseType,...m})}s[i.name]=a}return s}function O(t,o){let e=[];for(let[n,p]of Object.entries(t)){let s=new Set,i=[];for(let r of p){s.add(r.requestType),s.add(r.responseType);let m=r.method==="get"?"params: data":"data",d=[];r.summary&&d.push(`// ${r.summary}`),d.push(`export function ${r.name}(data: ${r.requestType}) {`,` return service<${r.responseType}>({`,` url: "${r.url.replace("/api","")}",`,` method: "${r.method}",`,` ${m}`," })","}"),i.push(d.join(`
|
|
3
|
+
`))}let a=[...s].sort();e.push({[n]:['import { service } from "../..";',"","import type {",` ${a.join(`,
|
|
4
4
|
`)}`,`} from "${o}";`,"",i.join(`
|
|
5
5
|
|
|
6
6
|
`)].join(`
|
|
7
|
-
`)})}return e}function T(t,o){let e=o.replace(/\\/g,"/").replace(/^\.?\//,"").replace(/\.proto$/,"");return u.join(t,e)}function _(t){let o=j.resolve(process.cwd(),t.apiOutput);y.existsSync(o)||y.mkdirSync(o,{recursive:!0});let e=
|
|
7
|
+
`)})}return e}function T(t,o){let e=o.replace(/\\/g,"/").replace(/^\.?\//,"").replace(/\.proto$/,"");return u.join(t,e)}function _(t){let o=j.resolve(process.cwd(),t.apiOutput);y.existsSync(o)||y.mkdirSync(o,{recursive:!0});let e=v(t.root,t.file),n=T(t.typeOutput,t.file),p=j.relative(t.apiOutput,n).replace(/\\/g,"/"),s=O(e,p);for(let i of s)for(let[a,r]of Object.entries(i))y.writeFileSync(`${o}/${a}.ts`,r,"utf8");console.log("\u2705 Generate Success")}import{execSync as H}from"child_process";import f from"fs";import c from"path";function $(t){let o=c.resolve(process.cwd(),t.typeOutput);f.existsSync(o)||f.mkdirSync(o,{recursive:!0});let e=c.resolve(process.cwd(),t.root),n=["protoc",`--ts_proto_out=${o}`,"--ts_proto_opt=onlyTypes=true","--ts_proto_opt=useOptionals=messages","--ts_proto_opt=esModuleInterop=true",`--proto_path=${e}`,`--proto_path=${c.join(e,"/third_party/googleapis")}`,c.join(e,t.file)],p={...process.env,PATH:`${c.join(process.cwd(),"node_modules",".bin")};${process.env.PATH}`};console.log(`${c.join(process.cwd(),"node_modules",".bin")};${process.env.PATH}`),H(n.join(" "),{stdio:"inherit",env:p}),f.rmSync(c.join(o,"google"),{recursive:!0,force:!0}),f.rmSync(c.join(o,"common.ts"),{force:!0})}async function S(t){if(!b.existsSync(R.resolve(process.cwd(),t.root))){console.log(`proto \u9879\u76EE\u4E0D\u5B58\u5728(${t.root})`);return}$(t),_(t)}var l=new q;l.name("proto-gen").description("proto code generator").version("1.0.0");l.command("generate").option("--file, --file <path>").option("--root, --root <path>").option("--type-output, --typeOutput <path>").option("--api-output, --apiOutput <path>").action(t=>{if(t.file===void 0){console.log("proto file \u4E0D\u80FD\u4E3A\u7A7A, \u8BF7\u8F93\u5165 --file ./account.proto");return}if(t.root===void 0){console.log("proto \u6587\u4EF6\u7684\u6839\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A, \u8BF7\u8F93\u5165 --root ../protos");return}t.typeOutput===void 0&&(t.typeOutput="src/protos/type"),t.apiOutput===void 0&&(t.apiOutput="src/protos/api"),S(t)});l.parse();
|