UNPKG

@stryke/fs

Version:

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

149 lines (147 loc) 6.15 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_exists = require('./exists.cjs'); const require_get_parent_path = require('./get-parent-path.cjs'); const require_get_workspace_root = require('./get-workspace-root.cjs'); const require_resolve = require('./resolve.cjs'); const require_json = require('./json.cjs'); let node_fs = require("node:fs"); let _stryke_path_join_paths = require("@stryke/path/join-paths"); let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns"); let _stryke_string_format_package = require("@stryke/string-format/package"); let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string"); let semver = require("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 = require_get_workspace_root.getWorkspaceRoot()) { const lockFile = require_get_parent_path.getParentPath([ "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock" ], dir, { includeNameInResults: true }); if (!lockFile) return "pnpm"; switch ((0, _stryke_path_file_path_fns.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 = (0, _stryke_path_file_path_fns.findFilePath)(dir); if (newDir === dir) return; dir = newDir; packageJsonPath = (0, _stryke_path_join_paths.joinPaths)(dir, "package.json"); if (await require_exists.exists(packageJsonPath)) break; } return packageJsonPath; } async function getPackageJsonPath(name, options = {}) { const entry = await require_resolve.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 require_json.readJsonFile(packageJsonPath); return { name, version: packageJson.version, rootPath: (0, _stryke_path_file_path_fns.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 = require_get_workspace_root.getWorkspaceRoot()) { const path = require_get_parent_path.getParentPath("package.json", cwd, { skipCwd: false, includeNameInResults: true }); if (!path || !(0, node_fs.existsSync)(path)) return null; return require_json.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 = (0, _stryke_string_format_package.getPackageName)(name); const packageJson = await loadPackageJson((0, _stryke_type_checks_is_string.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 = (0, _stryke_string_format_package.getPackageName)(name); const packageJson = await loadPackageJson((0, _stryke_type_checks_is_string.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:") || (0, semver.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(require_resolve.resolvePackage(name, options)); } //#endregion exports.doesPackageMatch = doesPackageMatch; exports.getPackageInfo = getPackageInfo; exports.getPackageListing = getPackageListing; exports.getPackageManager = getPackageManager; exports.isPackageExists = isPackageExists; exports.isPackageListed = isPackageListed; exports.loadPackageJson = loadPackageJson;