UNPKG

@stacksjs/dtsx

Version:

A modern, fast .d.ts generation tool, powered by Bun.

7 lines (6 loc) 3.11 kB
// @bun import{watch as m}from"fs";import{readdir as p,stat as f}from"fs/promises";import{join as y,relative as G}from"path";function g(k,J){let{root:K,include:O=["**/*.ts","**/*.tsx"],exclude:_=["**/node_modules/**","**/*.d.ts","**/dist/**"],debounce:V=100,initialBuild:L=!0,clearScreen:B=!1,onChange:C,onBuildStart:$,onBuildComplete:R,onError:j}=k,D=[],X=new Set,U=!1,Q=new Map,M=null,W=!1;function F(H){let z=G(K,H);for(let I of _)if(Y(z,I))return!1;for(let I of O)if(Y(z,I))return!0;return!1}function Y(H,z){let I=z.replace(/\./g,"\\.").replace(/\*\*/g,"{{DOUBLESTAR}}").replace(/\*/g,"[^/]*").replace(/\{\{DOUBLESTAR\}\}/g,".*").replace(/\?/g,".");return new RegExp(`^${I}$`).test(H.replace(/\\/g,"/"))}async function T(){if(W||Q.size===0)return;W=!0;let H=Array.from(Q.values());Q.clear();try{for(let q of H)await C?.(q);if(B)console.clear();await $?.();let z=H.filter((q)=>q.type!=="unlink").map((q)=>q.path),I=await J(z.length>0?z:void 0);await R?.(I)}catch(z){await j?.(z)}finally{if(W=!1,Q.size>0)S()}}function S(){if(M)clearTimeout(M);M=setTimeout(()=>{M=null,T()},V)}function w(H,z,I){if(!z||!U)return;let q=y(I,z);if(!F(q))return;let N={type:H==="rename"?"add":"change",path:q,relativePath:G(K,q)};f(q).then(()=>{X.add(q)}).catch(()=>{N.type="unlink",X.delete(q)}).finally(()=>{Q.set(q,N),S()})}async function x(H){let z=[H];try{let I=await p(H,{withFileTypes:!0});for(let q of I)if(q.isDirectory()){let N=y(H,q.name),A=G(K,N),E=!1;for(let Z of _)if(Y(A,Z)||Y(`${A}/`,Z)){E=!0;break}if(!E){let Z=await x(N);z.push(...Z)}}}catch{}return z}async function P(){if(U)return;U=!0;let H=await x(K);for(let z of H)try{let I=m(z,{persistent:!0},(q,N)=>{w(q,N,z)});I.on("error",(q)=>{j?.(q)}),D.push(I)}catch(I){await j?.(I)}if(L){await $?.();let z=await J();await R?.(z)}}function b(){if(U=!1,M)clearTimeout(M),M=null;for(let H of D)H.close();D.length=0,X.clear(),Q.clear()}async function v(){await $?.();let H=await J();return await R?.(H),H}return{start:P,stop:b,rebuild:v,isWatching:()=>U,getWatchedFiles:()=>Array.from(X)}}async function c(k,J){let K=async(_)=>{let V=Date.now();try{let L=await J(k);return{success:L.errors.length===0,duration:Date.now()-V,filesProcessed:L.filesProcessed,errors:L.errors}}catch(L){return{success:!1,duration:Date.now()-V,filesProcessed:0,errors:[L.message]}}},O=g({root:k.root||k.cwd||process.cwd(),include:k.include,exclude:k.exclude,debounce:k.debounce,initialBuild:k.initialBuild,clearScreen:k.clearScreen,onChange:k.onChange,onBuildStart:k.onBuildStart,onBuildComplete:k.onBuildComplete,onError:k.onError},K);return await O.start(),O}function h(k){let J=k.success?"\u2713":"\u2717",K=`${k.duration}ms`;if(k.success)return`${J} Built ${k.filesProcessed} file(s) in ${K}`;else return`${J} Build failed in ${K} ${k.errors.map((O)=>` - ${O}`).join(` `)}`}function a(){return{onChange:(k)=>{let J=k.type==="add"?"+":k.type==="unlink"?"-":"~";console.log(`[${J}] ${k.relativePath}`)},onBuildStart:()=>{console.log(` Rebuilding...`)},onBuildComplete:(k)=>{console.log(h(k))},onError:(k)=>{console.error(`Error: ${k.message}`)}}} export{g as ra,c as sa,h as ta,a as ua};