UNPKG

@stryke/fs

Version:

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

142 lines (140 loc) 5.34 kB
import { exists } from "./exists.mjs"; import { getParentPath } from "./get-parent-path.mjs"; import { getWorkspaceRoot } from "./get-workspace-root.mjs"; import { resolvePackage } from "./resolve.mjs"; import { readJsonFile } from "./json.mjs"; import { existsSync } from "node:fs"; import { joinPaths } from "@stryke/path/join-paths"; import { findFileName, findFilePath } from "@stryke/path/file-path-fns"; import { getPackageName } from "@stryke/string-format/package"; import { isString } from "@stryke/type-checks/is-string"; import { subset } from "semver"; //#region src/package-fns.ts /** * Get the package manager used in the project * * @param dir - The path to the project * @returns The package manager used in the project */ function getPackageManager(dir = getWorkspaceRoot()) { const lockFile = getParentPath([ "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock" ], dir, { includeNameInResults: true }); if (!lockFile) return "pnpm"; switch (findFileName(lockFile)) { case "yarn.lock": return "yarn"; case "pnpm-lock.yaml": return "pnpm"; case "bun.lock": return "bun"; default: return "npm"; } } async function searchPackageJson(dir) { let packageJsonPath; while (true) { if (!dir) return; const newDir = findFilePath(dir); if (newDir === dir) return; dir = newDir; packageJsonPath = joinPaths(dir, "package.json"); if (await exists(packageJsonPath)) break; } return packageJsonPath; } async function getPackageJsonPath(name, options = {}) { const entry = await resolvePackage(name, options); if (!entry) return; return searchPackageJson(entry); } /** * Get package info * * @param name - The name of the package * @param options - The options to use when resolving the package * @returns The package info */ async function getPackageInfo(name, options = {}) { const packageJsonPath = await getPackageJsonPath(name, options); if (!packageJsonPath) return; const packageJson = await readJsonFile(packageJsonPath); return { name, version: packageJson.version, rootPath: findFilePath(packageJsonPath), packageJsonPath, packageJson }; } /** * Get the package info from the package.json file * * @param cwd - The current working directory * @returns The package info */ async function loadPackageJson(cwd = getWorkspaceRoot()) { const path = getParentPath("package.json", cwd, { skipCwd: false, includeNameInResults: true }); if (!path || !existsSync(path)) return null; return readJsonFile(path); } /** * Check if a package is listed in the package.json file * * @param name - The name of the package * @param cwdOrOptions - The current working directory or options to use when checking if the package is listed * @returns An indicator specifying if the package is listed */ async function isPackageListed(name, cwdOrOptions) { const packageName = getPackageName(name); const packageJson = await loadPackageJson(isString(cwdOrOptions) ? cwdOrOptions : cwdOrOptions?.cwd); if (!packageJson) return false; return Boolean(packageJson.dependencies && packageName in packageJson.dependencies && packageJson.dependencies[packageName] || packageJson.devDependencies && packageName in packageJson.devDependencies && packageJson.devDependencies[packageName]); } /** * Return the package version and dependency type listed in the package.json file * * @param name - The name of the package * @param cwdOrOptions - The current working directory or options to use when checking if the package is listed * @returns The version and type of the package if listed, otherwise undefined */ async function getPackageListing(name, cwdOrOptions) { const packageName = getPackageName(name); const packageJson = await loadPackageJson(isString(cwdOrOptions) ? cwdOrOptions : cwdOrOptions?.cwd); if (!packageJson) return; const version = packageJson.dependencies && packageName in packageJson.dependencies ? packageJson.dependencies[packageName] : packageJson.devDependencies && packageName in packageJson.devDependencies ? packageJson.devDependencies[packageName] : void 0; const type = (packageJson.dependencies && packageName in packageJson.dependencies ? "dependencies" : packageJson.devDependencies && packageName in packageJson.devDependencies ? "devDependencies" : void 0) || void 0; return version && type ? { version, type } : void 0; } /** * Check if a package version matches a specific version range * * @param name - The name of the package * @param version - The version to check against * @param cwd - The current working directory * @returns An indicator specifying if the package version matches the range */ async function doesPackageMatch(name, version, cwd) { const pkg = await getPackageListing(name, { cwd }); if (!pkg) return false; return pkg.version.startsWith("catalog:") || pkg.version.startsWith("workspace:") || subset(pkg.version, version); } /** * Check if a package exists * * @param name - The name of the package * @param options - The options to use when resolving the package * @returns An indicator specifying if the package exists */ function isPackageExists(name, options = {}) { return Boolean(resolvePackage(name, options)); } //#endregion export { doesPackageMatch, getPackageInfo, getPackageListing, getPackageManager, isPackageExists, isPackageListed, loadPackageJson }; //# sourceMappingURL=package-fns.mjs.map