UNPKG

@vite-pwa/create-pwa

Version:
2 lines (1 loc) 1.95 kB
import u from"node:fs";import s from"node:path";import k from"node:process";const g=["npm","yarn","yarn@berry","pnpm","pnpm@6","bun","deno"],y={"bun.lock":"bun","bun.lockb":"bun","deno.lock":"deno","pnpm-lock.yaml":"pnpm","yarn.lock":"yarn","package-lock.json":"npm","npm-shrinkwrap.json":"npm"},f={"node_modules/.deno/":"deno","node_modules/.pnpm/":"pnpm","node_modules/.yarn-state.yml":"yarn","node_modules/.yarn_integrity":"yarn","node_modules/.package-lock.json":"npm",".pnp.cjs":"yarn",".pnp.js":"yarn","bun.lock":"bun","bun.lockb":"bun"};function p(a,e){try{const t=u.statSync(a);return e==="file"?t.isFile():t.isDirectory()}catch{return!1}}function*b(a=k.cwd()){let e=s.resolve(a);const{root:t}=s.parse(e);for(;e&&e!==t;)yield e,e=s.dirname(e)}async function m(a,e){return!a||!p(a,"file")?null:await w(a,e)}async function j(a={}){const{cwd:e,strategies:t=["lockfile","packageManager-field"],onUnknown:o}=a;for(const r of b(e))for(const i of t)switch(i){case"lockfile":{for(const n of Object.keys(y))if(await p(s.join(r,n),"file")){const l=y[n],c=await m(s.join(r,"package.json"),o);return c||{name:l,agent:l}}break}case"packageManager-field":{const n=await m(s.join(r,"package.json"),o);if(n)return n;break}case"install-metadata":{for(const n of Object.keys(f)){const l=n.endsWith("/")?"dir":"file";if(await p(s.join(r,n),l)){const c=f[n],d=c==="yarn"?M(n)?"yarn":"yarn@berry":c;return{name:c,agent:d}}}break}}return null}async function w(a,e){try{const t=JSON.parse(await u.promises.readFile(a,"utf8"));let o;if(typeof t.packageManager=="string"){const[r,i]=t.packageManager.replace(/^\^/,"").split("@");let n=i;return r==="yarn"&&Number.parseInt(i)>1?(o="yarn@berry",n="berry",{name:r,agent:o,version:n}):r==="pnpm"&&Number.parseInt(i)<7?(o="pnpm@6",{name:r,agent:o,version:n}):g.includes(r)?(o=r,{name:r,agent:o,version:n}):e?.(t.packageManager)??null}}catch{}return null}function M(a){return a.endsWith(".yarn_integrity")}export{j as d};