@stacksjs/dtsx
Version:
A modern, fast .d.ts generation tool, powered by Bun.
16 lines (15 loc) • 3.92 kB
JavaScript
// @bun
import{Vc as G}from"./chunk-nkp89e28.js";import{ed as I}from"./chunk-rr7k2pw0.js";import{Xd as E}from"./chunk-05vktpfb.js";import{statSync as F}from"fs";import{readFile as w,readdir as L}from"fs/promises";import{createRequire as P}from"module";import{dirname as v,isAbsolute as A,join as z,resolve as D}from"path";import h from"process";function C(){return`${String.fromCharCode(47,47,35)} sourceMappingURL=`}function d(k,H){throw Error(H||`Unexpected value: ${k}`)}async function a(k,H,B="lf"){let N=H.replace(/\r\n/g,`
`);if(!N.endsWith(`
`))N+=`
`;if(B==="crlf")N=N.replace(/\n/g,`\r
`);await G(k,N)}async function g(k){let H=k??I.root,B=await L(H,{withFileTypes:!0});return(await Promise.all(B.map((W)=>{let K=z(H,W.name);return W.isDirectory()?g(K):K}))).flat(1/0).filter((W)=>{if(typeof W!=="string")return!1;return/\.(?:ts|tsx|mts|cts)$/.test(W)&&!/\.d\.(?:ts|mts|cts)$/.test(W)})}function m(k){let H="",B=0,N=0;while(B<k.length){let W=k.charCodeAt(B);if(N){if(H+=k[B],W===92&&B+1<k.length){H+=k[B+1],B+=2;continue}if(W===N)N=0;B++;continue}if(W===34){N=W,H+=k[B++];continue}if(W===47&&k.charCodeAt(B+1)===47){B+=2;while(B<k.length&&k.charCodeAt(B)!==10)B++;continue}if(W===47&&k.charCodeAt(B+1)===42){B+=2;while(B+1<k.length&&!(k.charCodeAt(B)===42&&k.charCodeAt(B+1)===47)){if(k.charCodeAt(B)===10)H+=`
`;B++}B=Math.min(B+2,k.length);continue}if(W===44){let K=B+1;while(K<k.length){while(K<k.length&&k.charCodeAt(K)<=32)K++;if(k.charCodeAt(K)===47&&k.charCodeAt(K+1)===47){K+=2;while(K<k.length&&k.charCodeAt(K)!==10)K++;continue}if(k.charCodeAt(K)===47&&k.charCodeAt(K+1)===42){K+=2;while(K+1<k.length&&!(k.charCodeAt(K)===42&&k.charCodeAt(K+1)===47))K++;K=Math.min(K+2,k.length);continue}break}let Y=k.charCodeAt(K);if(Y===93||Y===125){B++;continue}}H+=k[B++]}return H}function R(k){let H=[k];if(!k.endsWith(".json"))H.push(`${k}.json`);H.push(z(k,"tsconfig.json"));for(let B of H)try{if(F(B).isFile())return B}catch{}return null}function f(k,H){if(A(k)||k.startsWith("."))return R(D(v(H),k));let B=P(H);for(let N of[k,`${k}/tsconfig.json`])try{let W=R(B.resolve(N));if(W)return W}catch{}return null}async function V(k,H){let B=R(k);if(!B||H.has(B))return;H.add(B);let N=await w(B,"utf8"),W=JSON.parse(m(N)),K=typeof W.extends==="string"?[W.extends]:W.extends??[],Y;for(let J of K){let M=f(J,B);if(!M)continue;let U=await V(M,H);if(U!==void 0)Y=U}return W.compilerOptions?.isolatedDeclarations??Y}async function r(k){try{let H=k?.cwd||h.cwd(),B=k?.tsconfigPath||"tsconfig.json",N=A(B)?B:D(H,B);return await V(N,new Set)??!1}catch{return!1}}function s(k,H){let B={isValid:!0,errors:[]};for(let N of E(k))B.errors.push({line:N.line,column:N.column,message:N.message,code:N.code});return B.isValid=B.errors.length===0,B}function i(k,H,B){if(k===H)return"";let N=k.split(`
`),W=H.split(`
`),K=new Map,Y=new Map;for(let X=0;X<N.length;X++)K.set(N[X],(K.get(N[X])??0)+1);for(let X=0;X<W.length;X++)Y.set(W[X],(Y.get(W[X])??0)+1);let J=[],M=[];for(let[X,Z]of K){let q=Z-(Y.get(X)??0);for(let $=0;$<q;$++)J.push(X)}for(let[X,Z]of Y){let q=Z-(K.get(X)??0);for(let $=0;$<q;$++)M.push(X)}if(J.length===0&&M.length===0)return"";let U=[`--- ${B}`,`+++ ${B}`];for(let X=0;X<J.length;X++)U.push(`- ${J[X]}`);for(let X=0;X<M.length;X++)U.push(`+ ${M[X]}`);return U.join(`
`)}function o(k,H,B,N){let W=k.split(`
`),K=[];function Y(Z){let $="",b=Z<0?-Z<<1|1:Z<<1;do{let O=b&31;if(b>>>=5,b>0)O|=32;$+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[O]}while(b>0);return $}let J=0,M=0,U=0,X=N.split(`
`).length;for(let Z=0;Z<W.length;Z++){if(W[Z].trim()===""){K.push("");continue}J=0;let $=Math.min(Z,X-1),b=[],O=0-J,Q=0,S=$-M,y=0-U;b.push(Y(O)+Y(Q)+Y(S)+Y(y)),J=0,M=$,U=0,K.push(b.join(","))}return{version:3,file:H,sourceRoot:"",sources:[B],sourcesContent:[N],mappings:K.join(";")}}function t(k,H){return`${k}
${C()}${H}
`}
export{C as Ic,d as Jc,a as Kc,g as Lc,r as Mc,s as Nc,i as Oc,o as Pc,t as Qc};