var k=Object.defineProperty;var m=(o,r)=>k(o,"name",{value:r,configurable:!0});import{createRequire as v}from"node:module";import{p as n,m as P}from"./bin.js";import{M as p,l as S}from"../packem_shared/readFileSync-DseCu8sg-DEq4Fn3a.js";import{cleanWorkspace as M}from"#native";import{l as D}from"../packem_shared/missing-package-json-DKAwoiWK.js";import{f as y}from"../packem_shared/utils-DrNg0XTR.js";const h=v(import.meta.url),u=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,_=m(o=>{if(typeof u<"u"&&u.versions&&u.versions.node){const[r,t]=u.versions.node.split(".").map(Number);if(r>22||r===22&&t>=3||r===20&&t>=16)return u.getBuiltinModule(o)}return h(o)},"__cjs_getBuiltinModule"),{readdirSync:$,lstatSync:b,unlinkSync:R,rmSync:j}=_("node:fs");var w=Object.defineProperty,d=m((o,r)=>w(o,"name",{value:r,configurable:!0}),"f");const x=/node_modules/,E=/\.git/,N=d(o=>{const r=[],t=[o];for(;t.length>0;){const s=t.pop();let c;try{c=$(s)}catch{continue}for(const i of c){const e=p(s,i);try{const f=b(e);if(f.isSymbolicLink()||!f.isDirectory())continue}catch{continue}i==="node_modules"?r.push(e):i!==".git"&&i!==".hg"&&t.push(e)}}return r},"findNodeModulesDirectories"),q=["pnpm-lock.yaml","package-lock.json","npm-shrinkwrap.json","yarn.lock","bun.lock","bun.lockb"],C=d((o,r,t)=>{let s=0,c=!1;for(const i of q){const e=p(o,i);if(S(e)){if(r){t.info(`  ${e}`),s++;continue}try{R(e),n.success(`Removed ${e}`),s++}catch(f){n.error(`${e}: ${y(f)}`),c=!0}}}return{hadError:c,removed:s}},"removeLockfiles"),L=d(o=>{for(const r of P(o,{includeDirs:!1,includeSymlinks:!1,skip:[x,E]}))if(r.name==="package.json")return!0;return!1},"containsPackageJson"),g=d((o,r,t)=>{let s=0,c=!1;for(const{packageDir:i}of D(o)){const e=p(o,i);if(!L(e)){if(r){t.info(`  ${e}`),s++;continue}try{j(e,{force:!0,recursive:!0}),n.success(`Removed empty package ${e}`),s++}catch(f){n.error(`${e}: ${y(f)}`),c=!0}}}return{hadError:c,removed:s}},"removeEmptyPackages"),F=d(async({logger:o,options:r,workspaceRoot:t})=>{const s=t??process.cwd(),c=r.lockfile||!1,i=r.emptyPackages||!1;if(r.dryRun){const l=N(s);if(l.length>0){n.info("Would remove:");for(const a of l)o.info(`  ${a}`)}else n.info("No node_modules directories found.");c&&C(s,!0,o),i&&(n.info("Would remove empty packages:"),g(s,!0,o));return}const e=M(s,c);for(const l of e.removed)n.success(`Removed ${l}`);for(const l of e.lockfilesRemoved)n.success(`Removed ${l}`);for(const l of e.errors)n.error(l);e.removed.length===0&&e.lockfilesRemoved.length===0?n.info("No node_modules directories found."):n.info(`Cleaned ${e.removed.length} node_modules director${e.removed.length===1?"y":"ies"}`);let f=!1;if(i){const{hadError:l,removed:a}=g(s,!1,o);f=l,a>0&&n.info(`Cleaned ${a} empty package director${a===1?"y":"ies"}`)}(e.errors.length>0||f)&&(process.exitCode=1)},"execute");export{F as default};
