rpc4next-cli 0.4.0 → 0.5.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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import ce from"node:path";import{parseArgs as pe}from"node:util";import bt from"node:path";var D=["page.tsx","route.ts"];import G from"node:path";var it=(t,e)=>{let r=P(G.relative(G.dirname(t),e)).replace(/\.tsx?$/,"");return r.startsWith("../")||(r=`./${r}`),r},P=t=>t.replace(/\\/g,"/"),x=t=>G.relative(process.cwd(),t);import tt from"node:fs";import oe from"node:path";var at=["Query"];var k="Endpoint",K="QueryKey",X="ParamsKey",ct=[k,X,K],pt="rpc4next/client";import yt from"node:fs";import _ from"node:path";import{CATCH_ALL_PREFIX as Vt,DYNAMIC_PREFIX as zt,OPTIONAL_CATCH_ALL_PREFIX as Jt}from"rpc4next-shared";import Q from"node:path";var S=new Map,F=new Map,mt=(t,e)=>{let r=Q.resolve(e);[...t.keys()].forEach(n=>{let o=Q.resolve(n);(o===r||r.startsWith(o+Q.sep))&&t.delete(n)})},ut=t=>{mt(S,t)},lt=t=>{mt(F,t)};import Xt from"node:fs";import{HTTP_METHODS_EXCLUDE_OPTIONS as Qt}from"rpc4next-shared";import Kt from"node:crypto";var ft=(t,e)=>{let r=Kt.createHash("md5").update(`${t}::${e}`).digest("hex").slice(0,16);return`${e}_${r}`};var v=(t,e)=>!t||!e?"":`Record<${t}, ${e}>`,L=t=>t.length===0||t.some(({name:e,type:r})=>!e||!r)?"":`{ ${t.map(({name:e,type:r})=>`"${e}": ${r}`).join(`${";"} `)}${t.length>1?";":""} }`,O=(t,e,r)=>!t||!e?"":r?`import type { ${t} as ${r} } from "${e}"${";"}`:`import type { ${t} } from "${e}"${";"}`;var gt=(t,e,r,n)=>{let o=it(t,e),i=ft(o,r);return{importName:i,importPath:o,importStatement:O(r,o,i),type:n(r,i),exportName:r}},qt=t=>at.find(e=>new RegExp(`export (interface ${e} ?{|type ${e} ?=)`).test(t)),Bt=(t,e)=>new RegExp(`export (async )?(function ${e} ?\\(|const ${e} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} ?=|const \\{[^}]*\\b${e}\\b[^}]*\\} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} from)`).test(t),dt=(t,e)=>{let r=Xt.readFileSync(e,"utf8"),n=qt(r),o=n?gt(t,e,n,(s,a)=>v(K,a)):void 0,i=Qt.filter(s=>Bt(r,s)).map(s=>gt(t,e,s,(a,c)=>L([{name:`$${a.toLowerCase()}`,type:`typeof ${c}`}])));return{...o?{query:o}:{},routes:i}};var Et=new Set(D),Tt=["(..)(..)","(...)","(..)","(.)"],Zt=t=>Tt.some(e=>t.startsWith(e)),te=t=>{let e=t;for(;;){let r=Tt.find(n=>e.startsWith(n));if(!r)return e;e=e.slice(r.length)}},ee=t=>{try{let e=decodeURIComponent(t);return e.startsWith("_")?t:e}catch{return t}},q=t=>{let e=Zt(t),r=e?te(t):t,n=!e&&r.startsWith("(")&&r.endsWith(")"),o=!e&&r.startsWith("@"),i=r.startsWith("[[...")&&r.endsWith("]]"),s=!i&&r.startsWith("[...")&&r.endsWith("]"),a=r.startsWith("[")&&r.endsWith("]"),c=!e&&r.startsWith("_"),p=ee(r);return{isCatchAll:s,isDynamic:a,isGroup:n,isIntercept:e,isOptionalCatchAll:i,isParallel:o,isPrivate:c,segmentName:r,staticKeyName:p}},St=t=>{let e=S.get(t);if(e!==void 0)return e;let r=_.basename(t),n=q(r);if(r==="node_modules"||n.isPrivate)return S.set(t,!1),!1;let o=yt.readdirSync(t,{withFileTypes:!0});for(let i of o){let{name:s}=i,a=_.join(t,s),c=q(s);if(!(s==="node_modules"||c.isPrivate)){if(i.isFile()&&Et.has(s))return S.set(t,!0),!0;if(i.isDirectory()&&St(a))return S.set(t,!0),!0}}return S.set(t,!1),!1},re=(t,{isDynamic:e,isCatchAll:r,isOptionalCatchAll:n})=>{let o=t;return e&&(o=o.replace(/^\[+|\]+$/g,"")),(r||n)&&(o=o.replace(/^\.{3}/,"")),{paramName:o,keyName:`${n?Jt:r?Vt:e?zt:""}${o}`}},V=(t,e,r="",n=[])=>{let o=F.get(e);if(o!==void 0)return o;let i=r,s=r+" ",a=[],c=[],p=[],m=[],u=[...n],y=yt.readdirSync(e,{withFileTypes:!0}).filter(h=>{if(h.isDirectory()){let E=_.join(e,h.name);return St(E)}return Et.has(h.name)}).sort((h,E)=>h.name.localeCompare(E.name));for(let h of y){let E=P(_.join(e,h.name));if(h.isDirectory()){let b=h.name,{isGroup:R,isParallel:g,isOptionalCatchAll:l,isCatchAll:f,isDynamic:d,isPrivate:H,isIntercept:Ot,segmentName:jt,staticKeyName:Wt}=q(b);if(H)continue;let{paramName:Yt,keyName:Mt}=re(jt,{isDynamic:d,isCatchAll:f,isOptionalCatchAll:l}),Ut=d||f||l?[...u,{paramName:Yt,routeType:{isDynamic:d,isCatchAll:f,isOptionalCatchAll:l,isGroup:R,isParallel:g}}]:u,ot=R,Ht=Ot||g,{pathStructure:C,imports:Gt,paramsTypes:kt}=V(t,E,ot||g?i:s,Ut);if(m.push(...kt),Ht)continue;if(c.push(...Gt),ot){let w=C.match(/^\s*\{([\s\S]*)\}\s*$/),st=C.trim();if(!C)continue;if(w)a.push(`${s}${w[1].trim()}`);else if(st)p.push(st);else throw new Error(`Invalid empty child path structure in grouped/parallel route: ${E}`)}else{if(!C.trim())continue;let w=d||f||l?Mt:Wt;a.push(`${s}"${w}": ${C}`)}}else{let{query:b,routes:R}=dt(t,E);if(b){let{importStatement:g,importPath:l,type:f}=b;c.push({statement:g,path:l}),p.push(f)}if(R.forEach(g=>{let{importStatement:l,importPath:f,type:d}=g;c.push({statement:l,path:f}),p.push(d)}),p.push(k),u.length>0){let g=u.map(({paramName:f,routeType:d})=>{let H=d.isCatchAll?"string[]":d.isOptionalCatchAll?"string[] | undefined":"string";return{name:f,type:H}}),l=L(g);m.push({paramsType:l,dirPath:_.dirname(E)}),p.push(v(X,l))}}}let M=p.join(" & "),U=a.length>0?`{${`
2
+ import ce from"node:path";import{parseArgs as pe}from"node:util";import bt from"node:path";var D=["page.tsx","route.ts"];import G from"node:path";var it=(t,e)=>{let r=P(G.relative(G.dirname(t),e)).replace(/\.tsx?$/,"");return r.startsWith("../")||(r=`./${r}`),r},P=t=>t.replace(/\\/g,"/"),x=t=>G.relative(process.cwd(),t);import tt from"node:fs";import oe from"node:path";var at=["Query"];var k="Endpoint",K="QueryKey",X="ParamsKey",ct=[k,X,K],pt="rpc4next/client";import yt from"node:fs";import _ from"node:path";import{CATCH_ALL_PREFIX as Vt,DYNAMIC_PREFIX as zt,OPTIONAL_CATCH_ALL_PREFIX as Jt}from"rpc4next-shared";import Q from"node:path";var S=new Map,F=new Map,mt=(t,e)=>{let r=Q.resolve(e);[...t.keys()].forEach(n=>{let o=Q.resolve(n);(o===r||r.startsWith(o+Q.sep))&&t.delete(n)})},ut=t=>{mt(S,t)},lt=t=>{mt(F,t)};import Xt from"node:fs";import{HTTP_METHODS_EXCLUDE_OPTIONS as Qt}from"rpc4next-shared";import Kt from"node:crypto";var ft=(t,e)=>{let r=Kt.createHash("md5").update(`${t}::${e}`).digest("hex").slice(0,16);return`${e}_${r}`};var v=(t,e)=>!t||!e?"":`Record<${t}, ${e}>`,L=t=>t.length===0||t.some(({name:e,type:r})=>!e||!r)?"":`{ ${t.map(({name:e,type:r})=>`"${e}": ${r}`).join(`${";"} `)}${t.length>1?";":""} }`,O=(t,e,r)=>!t||!e?"":r?`import type { ${t} as ${r} } from "${e}"${";"}`:`import type { ${t} } from "${e}"${";"}`;var gt=(t,e,r,n)=>{let o=it(t,e),i=ft(o,r);return{importName:i,importPath:o,importStatement:O(r,o,i),type:n(r,i),exportName:r}},qt=t=>at.find(e=>new RegExp(`export (interface ${e} ?{|type ${e} ?=)`).test(t)),Bt=(t,e)=>new RegExp(`export (async )?(function ${e} ?\\(|const ${e} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} ?=|const \\{[^}]*\\b${e}\\b[^}]*\\} ?=|\\{[^}]*\\b${e}\\b[^}]*\\} from)`).test(t),dt=(t,e)=>{let r=Xt.readFileSync(e,"utf8"),n=qt(r),o=n?gt(t,e,n,(s,a)=>v(K,a)):void 0,i=Qt.filter(s=>Bt(r,s)).map(s=>gt(t,e,s,(a,c)=>L([{name:`$${a.toLowerCase()}`,type:`typeof ${c}`}])));return{...o?{query:o}:{},routes:i}};var Et=new Set(D),Tt=["(..)(..)","(...)","(..)","(.)"],Zt=t=>Tt.some(e=>t.startsWith(e)),te=t=>{let e=t;for(;;){let r=Tt.find(n=>e.startsWith(n));if(!r)return e;e=e.slice(r.length)}},ee=t=>{try{let e=decodeURIComponent(t);return e.startsWith("_")?t:e}catch{return t}},q=t=>{let e=Zt(t),r=e?te(t):t,n=!e&&r.startsWith("(")&&r.endsWith(")"),o=!e&&r.startsWith("@"),i=r.startsWith("[[...")&&r.endsWith("]]"),s=!i&&r.startsWith("[...")&&r.endsWith("]"),a=r.startsWith("[")&&r.endsWith("]"),c=!e&&r.startsWith("_"),p=ee(r);return{isCatchAll:s,isDynamic:a,isGroup:n,isIntercept:e,isOptionalCatchAll:i,isParallel:o,isPrivate:c,segmentName:r,staticKeyName:p}},St=t=>{let e=S.get(t);if(e!==void 0)return e;let r=_.basename(t),n=q(r);if(r==="node_modules"||n.isPrivate)return S.set(t,!1),!1;let o=yt.readdirSync(t,{withFileTypes:!0});for(let i of o){let{name:s}=i,a=_.join(t,s),c=q(s);if(!(s==="node_modules"||c.isPrivate)){if(i.isFile()&&Et.has(s))return S.set(t,!0),!0;if(i.isDirectory()&&St(a))return S.set(t,!0),!0}}return S.set(t,!1),!1},re=(t,{isDynamic:e,isCatchAll:r,isOptionalCatchAll:n})=>{let o=t;return e&&(o=o.replace(/^\[+|\]+$/g,"")),(r||n)&&(o=o.replace(/^\.{3}/,"")),{paramName:o,keyName:`${n?Jt:r?Vt:e?zt:""}${o}`}},V=(t,e,r="",n=[])=>{let o=F.get(e);if(o!==void 0)return o;let i=r,s=r+" ",a=[],c=[],p=[],m=[],u=[...n],y=yt.readdirSync(e,{withFileTypes:!0}).filter(h=>{if(h.isDirectory()){let E=_.join(e,h.name);return St(E)}return Et.has(h.name)}).sort((h,E)=>h.name.localeCompare(E.name));for(let h of y){let E=P(_.join(e,h.name));if(h.isDirectory()){let b=h.name,{isGroup:R,isParallel:g,isOptionalCatchAll:l,isCatchAll:f,isDynamic:d,isPrivate:H,isIntercept:Ot,segmentName:jt,staticKeyName:Wt}=q(b);if(H)continue;let{paramName:Yt,keyName:Mt}=re(jt,{isDynamic:d,isCatchAll:f,isOptionalCatchAll:l}),Ut=d||f||l?[...u,{paramName:Yt,routeType:{isDynamic:d,isCatchAll:f,isOptionalCatchAll:l,isGroup:R,isParallel:g}}]:u,ot=R||g,Ht=Ot,{pathStructure:C,imports:Gt,paramsTypes:kt}=V(t,E,ot||g?i:s,Ut);if(m.push(...kt),Ht)continue;if(c.push(...Gt),ot){let w=C.match(/^\s*\{([\s\S]*)\}\s*$/),st=C.trim();if(!C)continue;if(w)a.push(`${s}${w[1].trim()}`);else if(st)p.push(st);else throw new Error(`Invalid empty child path structure in grouped/parallel route: ${E}`)}else{if(!C.trim())continue;let w=d||f||l?Mt:Wt;a.push(`${s}"${w}": ${C}`)}}else{let{query:b,routes:R}=dt(t,E);if(b){let{importStatement:g,importPath:l,type:f}=b;c.push({statement:g,path:l}),p.push(f)}if(R.forEach(g=>{let{importStatement:l,importPath:f,type:d}=g;c.push({statement:l,path:f}),p.push(d)}),p.push(k),u.length>0){let g=u.map(({paramName:f,routeType:d})=>{let H=d.isCatchAll?"string[]":d.isOptionalCatchAll?"string[] | undefined":"string";return{name:f,type:H}}),l=L(g);m.push({paramsType:l,dirPath:_.dirname(E)}),p.push(v(X,l))}}}let M=p.join(" & "),U=a.length>0?`{${`
3
3
  `}${a.join(`,${`
4
4
  `}`)}${`
5
5
  `}${i}}`:"",nt={pathStructure:M&&U?`${M} & ${U}`:M||U,imports:c,paramsTypes:m};return F.set(e,nt),nt};var Pt=(t,e)=>{let{pathStructure:r,imports:n,paramsTypes:o}=V(t,e),i=`export type PathStructure = ${r}${";"}`,s=n.length?`${n.sort((m,u)=>m.path.localeCompare(u.path,void 0,{numeric:!0})).map(m=>m.statement).join(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rpc4next-cli",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Command line interface for rpc4next. Generates RPC client type definitions from Next.js routes.",
5
5
  "homepage": "https://github.com/watanabe-1/rpc4next#readme",
6
6
  "repository": {