@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
2 lines (1 loc) • 1.39 kB
JavaScript
import{exists as p,findFileName as g,findFilePath as o,joinPaths as k}from"@stryke/path";import{getParentPath as r}from"@stryke/path/get-parent-path";import{getWorkspaceRoot as s}from"@stryke/path/get-workspace-root";import{resolvePackage as i}from"@stryke/path/resolve";import{existsSync as l}from"node:fs";import{readJsonFile as c}from"./json";export function getPackageManager(a=s()){const n=r(["package-lock.json","yarn.lock","pnpm-lock.yaml","bun.lock"],a);if(!n)return"pnpm";switch(g(n)){case"yarn.lock":return"yarn";case"pnpm-lock.yaml":return"pnpm";case"bun.lock":return"bun";default:return"npm"}}async function f(a){let n;for(;;){if(!a)return;const e=o(a);if(e===a)return;if(a=e,n=k(a,"package.json"),await p(n))break}return n}async function u(a,n={}){const e=await i(a,n);if(e)return f(e)}export async function getPackageInfo(a,n={}){const e=await u(a,n);if(!e)return;const t=await c(e);return{name:a,version:t.version,rootPath:o(e),packageJsonPath:e,packageJson:t}}export async function loadPackageJson(a=s()){const n=r("package.json",a,{skipCwd:!1});return!n||!l(n)?null:c(n)}export async function isPackageListed(a,n){let e=a;e.includes("@")&&(e=!e.startsWith("@")||e.lastIndexOf("@")>0?e.slice(0,e.lastIndexOf("@")):e);const t=await loadPackageJson(n)??{};return e in(t.dependencies??{})||e in(t.devDependencies??{})}export function isPackageExists(a,n={}){return!!i(a,n)}