UNPKG

@stacksjs/launchpad

Version:
33 lines (32 loc) 10.5 kB
// @bun import"./chunk-8pxdwzvm.js";import T from"fs";import E from"os";import V from"path";import x from"process";function I(){return V.join(E.homedir(),".local","share","launchpad")}function H(Q){let Z=["B","KB","MB","GB"],X=Q,U=0;while(X>=1024&&U<Z.length-1)X/=1024,U++;return`${X.toFixed(1)} ${Z[U]}`}function j(Q){if(!T.existsSync(Q))return 0;let Z=0;try{let X=T.readdirSync(Q,{withFileTypes:!0});for(let U of X){let K=V.join(Q,U.name);try{if(U.isDirectory())Z+=j(K);else{let L=T.statSync(K);Z+=L.size}}catch{}}}catch{}return Z}function W(Q){if(!T.existsSync(Q))return 0;try{return T.readdirSync(Q).length}catch{return 0}}function B(Q){let Z=Q.split("_");if(Z.length>=2){let X=Z.pop()||"";return{projectName:Z.join("_"),shortHash:X}}return{projectName:Q,shortHash:""}}function C(){let Q=I();if(!T.existsSync(Q))return[];let Z=[];try{let X=T.readdirSync(Q,{withFileTypes:!0});for(let U of X){if(!U.isDirectory())continue;if(!U.name.includes("_"))continue;let K=V.join(Q,U.name),{projectName:L}=B(U.name);try{let O=T.statSync(K),$=j(K),Y=W(V.join(K,"pkgs")),J=W(V.join(K,"bin"));Z.push({hash:U.name,projectName:L,path:K,size:H($),sizeBytes:$,packages:Y,binaries:J,created:O.birthtime,modified:O.mtime})}catch{}}}catch{}return Z.sort((X,U)=>U.modified.getTime()-X.modified.getTime())}async function P(Q){let Z=C();if(Z.length===0){console.log("\uD83D\uDCED No development environments found");return}switch(Q.format){case"json":console.log(JSON.stringify(Z.map((X)=>({hash:X.hash,projectName:X.projectName,packages:X.packages,binaries:X.binaries,size:X.size,created:X.created.toISOString()})),null,2));break;case"simple":Z.forEach((X)=>{console.log(`${X.projectName} (${X.hash})`)});break;default:{console.log(`\uD83D\uDCE6 Development Environments: `);let X=[["Project","Packages","Binaries","Size","Created"]];if(Q.verbose)X[0].push("Hash");Z.forEach(($)=>{let Y=[$.projectName,$.packages.toString(),$.binaries.toString(),$.size,$.created.toLocaleDateString()];if(Q.verbose)Y.push($.hash);X.push(Y)});let U=X[0].map(($,Y)=>Math.max(...X.map((J)=>J[Y]?.length||0))),K=X[0].map(($,Y)=>$.padEnd(U[Y])).join(" \u2502 ");console.log(`\u2502 ${K} \u2502`);let L=U.map(($)=>"\u2500".repeat($)).join("\u2500\u253C\u2500");console.log(`\u251C\u2500${L}\u2500\u2524`),X.slice(1).forEach(($)=>{let Y=$.map((J,G)=>J.padEnd(U[G])).join(" \u2502 ");console.log(`\u2502 ${Y} \u2502`)});let O="\u2500".repeat(K.length+4);console.log(`\u2514${O}\u2518`),console.log(` Total: ${Z.length} environment(s)`);break}}}async function b(Q,Z){let X=I(),U=V.join(X,Q);if(!T.existsSync(U)){console.error(`\u274C Environment not found: ${Q}`),console.log(` Available environments:`);let M=C();if(M.length>0)M.forEach((q)=>{console.log(` \u2022 ${q.hash} (${q.projectName})`)});else console.log(" (none)");x.exit(1)}let{projectName:K}=B(Q),L=T.statSync(U),O=j(U),$=W(V.join(U,"pkgs")),Y=W(V.join(U,"bin"));if(console.log(`\uD83D\uDD0D Inspecting environment: ${Q} `),console.log("\uD83D\uDCCB Basic Information:"),console.log(` Project Name: ${K}`),console.log(` Hash: ${Q}`),console.log(` Path: ${U}`),console.log(` Size: ${H(O)}`),console.log(` Created: ${L.birthtime.toLocaleString()}`),console.log(` Modified: ${L.mtime.toLocaleString()}`),Z.verbose||Z.showStubs)console.log(` \uD83D\uDCC1 Directory Structure:`),["bin","pkgs","lib","share","sbin","include"].forEach((q)=>{let F=V.join(U,q),_=W(F);if(_>0)console.log(` ${q}/: ${_} item(s)`)});let J=V.join(U,"pkgs");if(T.existsSync(J)){console.log(` \uD83D\uDCE6 Installed Packages:`);try{let M=T.readdirSync(J,{withFileTypes:!0}).filter((q)=>q.isDirectory()).map((q)=>q.name);if(M.length>0)M.forEach((q)=>{console.log(` ${q}`)});else console.log(" (none)")}catch{console.log(" (unable to read packages)")}}let G=V.join(U,"bin");if(T.existsSync(G)){console.log(` \uD83D\uDD27 BIN Binaries:`);try{let M=T.readdirSync(G);if(M.length>0){if(M.slice(0,10).forEach((q)=>{let F=V.join(G,q);try{let _=T.statSync(F),w=_.isFile()?"file":"directory",k=_.isFile()&&_.mode&73?", executable":"";console.log(` ${q} (${w}${k})`)}catch{console.log(` ${q} (unknown)`)}}),M.length>10)console.log(` ... and ${M.length-10} more`)}else console.log(" (none)")}catch{console.log(" (unable to read binaries)")}}if(Z.showStubs&&T.existsSync(G)){console.log(` \uD83D\uDCC4 Binary Stub Contents:`);try{let M=T.readdirSync(G).slice(0,3);for(let q of M){let F=V.join(G,q);try{let _=T.readFileSync(F,"utf8");if(console.log(` ${q}:`),console.log(_.slice(0,200).split(` `).map((w)=>` ${w}`).join(` `)),_.length>200)console.log(" ... (truncated)")}catch{console.log(` ${q}: (unable to read)`)}}}catch{console.log(" (unable to read stub files)")}}console.log(` \uD83C\uDFE5 Health Check:`);let R=$>0,A=Y>0,N=T.existsSync(V.join(U,"bin"));console.log(` ${A?"\u2705":"\u274C"} Binaries present`),console.log(` ${R?"\u2705":"\u274C"} ${$} package(s) installed`),console.log(` ${N?"\u2705":"\u274C"} Directory structure`),console.log(` Overall Status: ${R&&A&&N?"\u2705 Healthy":"\u26A0\uFE0F Issues detected"}`)}async function d(Q){let Z=C();if(Z.length===0){console.log("\uD83D\uDCED No development environments found");return}let X=new Date,U=Q.olderThanDays*24*60*60*1000,K=Z.filter((J)=>{let R=X.getTime()-J.modified.getTime()>U,A=J.packages===0&&J.binaries===0,N=!T.existsSync(V.join(J.path,"bin"));return R||A||N});if(K.length===0){console.log("\uD83D\uDCED No environments need cleaning"),console.log(`Checked ${Z.length} environment(s), all are recent and healthy`);return}let L=K.reduce((J,G)=>J+G.sizeBytes,0);if(Q.dryRun)console.log(`\uD83D\uDD0D DRY RUN MODE - Nothing will actually be cleaned `);if(console.log(`${Q.dryRun?"Would clean":"Cleaning"} ${K.length} environment(s)`),console.log(`Total size to ${Q.dryRun?"be freed":"free"}: ${H(L)} `),console.log("**Cleanup Criteria:**"),console.log(`- Environments older than ${Q.olderThanDays} days`),console.log("- Environments with no binaries (failed installations)"),console.log(`- Empty or corrupted environment directories `),console.log("**Environments to clean:**"),K.forEach((J)=>{let G=Math.floor((X.getTime()-J.modified.getTime())/86400000),R=G>Q.olderThanDays?`${G} days old`:J.packages===0&&J.binaries===0?"empty":"corrupted";if(console.log(` \u2022 ${J.hash} (${J.projectName}) - ${J.size} - ${R}`),Q.verbose)console.log(` Path: ${J.path}`),console.log(` Last modified: ${J.modified.toLocaleString()}`)}),Q.dryRun)return;if(!Q.force)console.log(` \u26A0\uFE0F This will permanently delete the environments listed above!`),console.log("Use --force to skip this confirmation or --dry-run to preview"),x.exit(0);let O=0,$=0,Y=0;console.log(` \uD83D\uDDD1\uFE0F Removing environments...`);for(let J of K)try{if(Q.verbose)console.log(` Removing ${J.hash}...`);if(Y+=J.sizeBytes,T.rmSync(J.path,{recursive:!0,force:!0}),O++,Q.verbose)console.log(` \u2705 Removed ${J.hash} (${J.size})`)}catch(G){$++,console.error(` \u274C Failed to remove ${J.hash}:`,G instanceof Error?G.message:String(G))}if(console.log(` \u2705 Environment cleanup completed!`),console.log(` \u2022 Removed: ${O}/${K.length} environment(s)`),$>0)console.log(` \u2022 Failed: ${$} environment(s)`);console.log(` \u2022 Freed: ${H(Y)} of disk space`)}async function f(Q,Z){let X=I(),U=V.join(X,Q);if(!T.existsSync(U)){console.error(`\u274C Environment not found: ${Q}`),console.log(` Available environments:`);let $=C();if($.length>0)$.forEach((Y)=>{console.log(` \u2022 ${Y.hash} (${Y.projectName})`)});else console.log(" (none)");x.exit(1)}let{projectName:K}=B(Q),L=j(U),O=H(L);if(console.log(`\uD83D\uDDD1\uFE0F Removing environment: ${Q}`),console.log(` Project: ${K}`),console.log(` Size: ${O}`),console.log(` Path: ${U}`),Z.verbose){let $=W(V.join(U,"pkgs")),Y=W(V.join(U,"bin"));console.log(` Packages: ${$}`),console.log(` Binaries: ${Y}`)}if(!Z.force)console.log(` \u26A0\uFE0F This will permanently delete this environment!`),console.log("Use --force to skip this confirmation"),x.exit(0);try{if(console.log(` \uD83D\uDDD1\uFE0F Removing...`),T.rmSync(U,{recursive:!0,force:!0}),console.log(` \u2705 Environment removed successfully!`),console.log(` \u2022 Freed: ${O} of disk space`),Z.verbose)console.log(` \u2022 Path: ${U}`),console.log(` \u2022 Project: ${K}`)}catch($){console.error(` \u274C Failed to remove environment:`,$ instanceof Error?$.message:String($)),x.exit(1)}}async function m(Q){let Z=C();if(Z.length===0){console.log("\uD83D\uDCED No development environments found");return}let X=Z.reduce((O,$)=>O+$.sizeBytes,0);if(console.log(`\uD83D\uDDD1\uFE0F Removing all ${Z.length} development environments`),console.log(` Total size: ${H(X)}`),Q.verbose)console.log(` Environments to remove:`),Z.forEach((O)=>{console.log(` \u2022 ${O.hash} (${O.projectName}) - ${O.size}`),console.log(` Path: ${O.path}`),console.log(` Packages: ${O.packages}, Binaries: ${O.binaries}`)});if(!Q.force)console.log(` \u26A0\uFE0F This will permanently delete ALL development environments!`),console.log("Use --force to skip this confirmation"),x.exit(0);let U=0,K=0,L=0;console.log(` \uD83D\uDDD1\uFE0F Removing environments...`);for(let O of Z)try{if(Q.verbose)console.log(` Removing ${O.hash} (${O.projectName})...`);if(L+=O.sizeBytes,T.rmSync(O.path,{recursive:!0,force:!0}),U++,Q.verbose)console.log(` \u2705 Removed ${O.hash} (${O.size})`)}catch($){K++,console.error(` \u274C Failed to remove ${O.hash}:`,$ instanceof Error?$.message:String($))}if(console.log(` \u2705 All environments removal completed!`),console.log(` \u2022 Removed: ${U}/${Z.length} environment(s)`),K>0)console.log(` \u2022 Failed: ${K} environment(s)`);console.log(` \u2022 Freed: ${H(L)} of disk space`)}var S=["dependencies.yaml","dependencies.yml","deps.yaml","deps.yml","pkgx.yaml","pkgx.yml","launchpad.yaml","launchpad.yml","package.json","pyproject.toml","requirements.txt","setup.py","Pipfile","Pipfile.lock","Cargo.toml","go.mod","go.sum","Gemfile","deno.json","deno.jsonc","action.yml","action.yaml","skaffold.yaml","skaffold.yml",".nvmrc",".node-version",".ruby-version",".python-version",".terraform-version","yarn.lock","bun.lockb",".yarnrc"];function l(Q,Z=!1){let X=Q;do{for(let U of S){let K=V.join(X,U);if(T.existsSync(K))return K}if(!Z)break;X=V.dirname(X)}while(X!==V.dirname(X));return null}export{f as removeEnvironment,m as removeAllEnvironments,P as listEnvironments,b as inspectEnvironment,l as findDependencyFile,d as cleanEnvironments,S as DEPENDENCY_FILE_NAMES}; export{l as B};