UNPKG

@stacksjs/dtsx

Version:

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

8 lines (6 loc) 3.92 kB
// @bun import{xf as Z}from"./chunk-pwwfmfhh.js";import{existsSync as T,mkdirSync as O,readFileSync as $,renameSync as V,rmSync as L,statSync as j,writeFileSync as k}from"fs";import{join as N,relative as W,resolve as D}from"path";var Y=3,X=".dtsx-cache",z="manifest.json";class R{cacheDir;manifestPath;manifest=null;configHash;constructor(b){this.cacheDir=D(b.cwd,X),this.manifestPath=N(this.cacheDir,z),this.configHash=this.hashConfig(b)}hashConfig(b){let q={keepComments:b.keepComments,importOrder:b.importOrder,outputStructure:b.outputStructure,isolatedDeclarations:b.isolatedDeclarations,declarationMap:b.declarationMap,indentStyle:b.indentStyle,indentSize:b.indentSize,prettier:b.prettier,typeMappings:b.typeMappings?{includeDefaults:b.typeMappings.includeDefaults,presets:b.typeMappings.presets,cacheKey:b.typeMappings.cacheKey,rules:b.typeMappings.rules.map((B)=>({pattern:typeof B.pattern==="string"?{kind:"string",value:B.pattern}:{kind:"regex",source:B.pattern.source,flags:B.pattern.flags},replacement:B.replacement,global:B.global,priority:B.priority,hasCondition:Boolean(B.condition)}))}:void 0,lineEnding:b.lineEnding,normalizeOutput:b.normalizeOutput,declarationOrder:b.declarationOrder,plugins:b.plugins?.map((B)=>({name:B.name,version:B.version,cacheKey:B.cacheKey}))};return String(Z(JSON.stringify(q)))}hashString(b){return String(Z(b))}load(){try{if(!T(this.manifestPath))return!1;let b=$(this.manifestPath,"utf-8"),q=JSON.parse(b);if(q.version!==Y)return!1;if(q.configHash!==this.configHash)return!1;if(!q.entries||typeof q.entries!=="object"||Array.isArray(q.entries))return!1;return this.manifest=q,!0}catch{return!1}}save(){if(!this.manifest)this.manifest={version:Y,configHash:this.configHash,entries:{},createdAt:Date.now(),updatedAt:Date.now()};if(this.manifest.updatedAt=Date.now(),!T(this.cacheDir))O(this.cacheDir,{recursive:!0});let b=`${this.manifestPath}.${process.pid}.tmp`;try{k(b,JSON.stringify(this.manifest,null,2)),V(b,this.manifestPath)}finally{if(T(b))L(b,{force:!0})}}needsRegeneration(b,q){return this.getCachedIfValid(b,q)===null}getCached(b,q){return this.getCachedIfValid(b,q)}getCachedIfValid(b,q){if(!this.manifest)return null;let B=W(q,b),G=this.manifest.entries[B];if(!G)return null;if(typeof G.dtsContent!=="string"||typeof G.dtsHash!=="string"||G.configHash!==this.configHash||this.hashString(G.dtsContent)!==G.dtsHash)return null;try{let J=j(b),K=J.mtimeMs,M=J.ctimeMs;if(J.size!==G.sourceSize)return null;if(K!==G.sourceMtime||M!==G.sourceCtime){let Q=$(b,"utf-8");if(this.hashString(Q)!==G.sourceHash)return null;G.sourceMtime=K,G.sourceCtime=M}return G.dtsContent}catch{return null}}update(b,q,B,G){if(!this.manifest)this.manifest={version:Y,configHash:this.configHash,entries:{},createdAt:Date.now(),updatedAt:Date.now()};let J=W(G,b),K,M,Q;try{let U=j(b);K=U.mtimeMs,M=U.ctimeMs,Q=U.size}catch{K=Date.now(),M=K,Q=Buffer.byteLength(q)}this.manifest.entries[J]={sourcePath:J,sourceHash:this.hashString(q),sourceMtime:K,sourceCtime:M,sourceSize:Q,dtsContent:B,dtsHash:this.hashString(B),generatedAt:Date.now(),configHash:this.configHash}}remove(b,q){if(!this.manifest)return;let B=W(q,b);delete this.manifest.entries[B]}clear(){this.manifest=null;try{if(T(this.manifestPath))L(this.cacheDir,{recursive:!0,force:!0})}catch{}}getStats(){if(!this.manifest)return{entries:0,size:0};let b=Object.keys(this.manifest.entries).length,q=0;for(let B of Object.values(this.manifest.entries))q+=B.dtsContent.length;return{entries:b,size:q}}prune(b,q){if(!this.manifest)return 0;let B=0,G=new Set(Array.from(b).map((J)=>W(q,J)));for(let J of Object.keys(this.manifest.entries))if(!G.has(J))delete this.manifest.entries[J],B++;return B}}function A(b){let q=N(b,".gitignore");try{let B="";if(T(q))B=$(q,"utf-8");if(!B.split(/\r?\n/).some((J)=>J.trim()===X||J.trim()===`${X}/`)){let J=B.trimEnd(),K=`${J}${J?` `:""}# dtsx cache ${X}/ `;k(q,K)}}catch{}} export{R as ec,A as fc};