UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

71 lines (70 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackageInfo = getPackageInfo; exports.getPackageManager = getPackageManager; exports.isPackageExists = isPackageExists; exports.isPackageListed = isPackageListed; exports.loadPackageJson = loadPackageJson; var _path = require("@stryke/path"); var _getParentPath = require("@stryke/path/get-parent-path"); var _getWorkspaceRoot = require("@stryke/path/get-workspace-root"); var _resolve = require("@stryke/path/resolve"); var _nodeFs = require("node:fs"); var _json = require("./json.cjs"); function getPackageManager(a = (0, _getWorkspaceRoot.getWorkspaceRoot)()) { const n = (0, _getParentPath.getParentPath)(["package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock"], a); if (!n) return "pnpm"; switch ((0, _path.findFileName)(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 = (0, _path.findFilePath)(a); if (e === a) return; if (a = e, n = (0, _path.joinPaths)(a, "package.json"), await (0, _path.exists)(n)) break; } return n; } async function u(a, n = {}) { const e = await (0, _resolve.resolvePackage)(a, n); if (e) return f(e); } async function getPackageInfo(a, n = {}) { const e = await u(a, n); if (!e) return; const t = await (0, _json.readJsonFile)(e); return { name: a, version: t.version, rootPath: (0, _path.findFilePath)(e), packageJsonPath: e, packageJson: t }; } async function loadPackageJson(a = (0, _getWorkspaceRoot.getWorkspaceRoot)()) { const n = (0, _getParentPath.getParentPath)("package.json", a, { skipCwd: !1 }); return !n || !(0, _nodeFs.existsSync)(n) ? null : (0, _json.readJsonFile)(n); } 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 ?? {}); } function isPackageExists(a, n = {}) { return !!(0, _resolve.resolvePackage)(a, n); }