import{existsSync as z,copyFileSync as Q}from"node:fs";import{execSync as J}from"node:child_process";import{resolve as G,dirname as Y}from"node:path";import{fileURLToPath as Z}from"node:url";var $=Y(Z(import.meta.url)),M=G($,".."),j=["better-sqlite3"];function L(){for(let q of j){let H=G(M,"node_modules",q);if(!z(H))try{J(`npm install ${q} --no-package-lock --no-save --silent`,{cwd:M,stdio:"pipe",timeout:120000})}catch{}else if(!z(G(H,"build","Release"))&&!z(G(H,"prebuilds")))try{J(`npm rebuild ${q} --ignore-scripts=false`,{cwd:M,stdio:"pipe",timeout:120000})}catch{}}}function W(q){try{return J(`node -e "new (require('better-sqlite3'))(':memory:').close()"`,{cwd:q,stdio:"pipe",timeout:1e4}),!0}catch{return!1}}function U(q){try{let H=process.versions.modules,O=G(q,"node_modules","better-sqlite3","build","Release"),w=G(O,"better_sqlite3.node"),K=G(O,`better_sqlite3.abi${H}.node`);if(!z(O))return;if(z(K)){if(Q(K,w),X(w),W(q))return}if(!z(w))return;if(W(q))Q(w,K);else if(J("npm rebuild better-sqlite3",{cwd:q,stdio:"pipe",timeout:60000}),X(w),z(w))Q(w,K)}catch{}}function X(q){if(process.platform==="darwin")try{J(`codesign --sign - --force "${q}"`,{stdio:"pipe",timeout:1e4})}catch{}}L();U(M);export{U as ensureNativeCompat,L as ensureDeps,X as codesignBinary};
