UNPKG

@stryke/fs

Version:

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

239 lines (237 loc) 9.31 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_workspace_root = require('./get-workspace-root.cjs'); let _stryke_path = require("@stryke/path"); let _stryke_path_join_paths = require("@stryke/path/join-paths"); let _stryke_path_correct_path = require("@stryke/path/correct-path"); let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns"); let mlly = require("mlly"); let _stryke_path_cwd = require("@stryke/path/cwd"); let _stryke_helpers_get_unique = require("@stryke/helpers/get-unique"); let _stryke_path_is_type = require("@stryke/path/is-type"); //#region src/resolve.ts const DEFAULT_EXTENSIONS = [ "js", "jsx", "mjs", "cjs", "ts", "tsx", "mts", "cts", "json", "jsonc", "json5", "node" ]; /** * Get the resolution paths based on the provided paths, current working directory, and workspace root. * * @param paths - An array of paths to include in the resolution. * @returns An array of unique, corrected resolution paths. */ function getResolutionPaths(paths = []) { let resolutionPaths = paths; if (!resolutionPaths.includes((0, _stryke_path_cwd.cwd)())) resolutionPaths.push((0, _stryke_path_cwd.cwd)()); const workspaceRoot = require_get_workspace_root.getWorkspaceRoot(); if (!resolutionPaths.includes(workspaceRoot)) resolutionPaths.push(workspaceRoot); resolutionPaths = (0, _stryke_helpers_get_unique.getUnique)(resolutionPaths.filter(Boolean).map((path) => (0, _stryke_path_correct_path.correctPath)(path)).reduce((ret, path, _, arr) => { ret.push(path); if (!(0, _stryke_path_is_type.isAbsolutePath)(path)) { ret.push((0, _stryke_path_correct_path.toAbsolutePath)(path, (0, _stryke_path_cwd.cwd)())); ret.push((0, _stryke_path_correct_path.toAbsolutePath)(path, workspaceRoot)); arr.forEach((existing) => { ret.push((0, _stryke_path_correct_path.toAbsolutePath)(path, existing)); }); } return ret; }, [])); return resolutionPaths; } /** * Get the node_modules resolution paths based on the provided paths. * * @param paths - An array of paths to include in the resolution. * @returns An array of unique, corrected node_modules resolution paths. */ function getNodeModulesPaths(paths = []) { return (0, _stryke_helpers_get_unique.getUnique)(paths.reduce((ret, path) => { if ((0, _stryke_path_file_path_fns.findFolderName)(path) === "node_modules") ret.push((0, _stryke_path_correct_path.correctPath)(path)); if (require_exists.existsSync((0, _stryke_path_join_paths.joinPaths)(path, "node_modules"))) ret.push((0, _stryke_path_correct_path.correctPath)((0, _stryke_path_join_paths.joinPaths)(path, "node_modules"))); return ret; }, [])); } /** * Get all combinations of resolution paths for a given path and options. * * @param path - The base path to combine with resolution paths. * @param options - The options containing resolution paths. * @returns An array of unique, corrected resolution paths. */ function getResolutionCombinations(path, options = {}) { let paths = options.useAdditionalPaths ? getResolutionPaths(options.paths) : options.paths ?? []; if ((0, _stryke_path_is_type.isNpmScopedPackage)(path)) paths = getNodeModulesPaths(paths); else paths.push(...getNodeModulesPaths(paths)); let combinations = paths.map((base) => (0, _stryke_path_join_paths.joinPaths)(base, path)); if ((0, _stryke_path_file_path_fns.findFileName)(path, { withExtension: false }) !== "index") combinations = combinations.reduce((ret, combination) => { ret.push(combination); ret.push((0, _stryke_path_join_paths.joinPaths)(combination, "index")); return ret; }, []); if (!(0, _stryke_path_file_path_fns.hasFileExtension)(path)) { const extensions = options.extensions ?? DEFAULT_EXTENSIONS; combinations = combinations.reduce((ret, combination) => { ret.push(combination); extensions.forEach((ext) => { ret.push((0, _stryke_path.appendExtension)(combination, ext)); }); return ret; }, []); combinations.push(...extensions.map((ext) => getResolutionCombinations((0, _stryke_path.appendExtension)(path, ext), options)).flat()); } paths.map((p) => { if ((0, _stryke_path_file_path_fns.hasFileExtension)(p)) combinations.push(...getResolutionCombinations(path, { ...options, useAdditionalPaths: false, paths: [(0, _stryke_path_file_path_fns.findFilePath)(p)] })); }); return (0, _stryke_helpers_get_unique.getUnique)(combinations.filter(Boolean)).map((p) => (0, _stryke_path_correct_path.correctPath)(p)); } /** * Resolve the path to a specified module * * @param path - The path to the module * @param options - The options to use when resolving the module * @returns A promise for the path to the module */ async function resolve(path, options = {}) { let paths = getResolutionPaths(options.paths); if ((0, _stryke_path_is_type.isNpmScopedPackage)(path)) paths = getNodeModulesPaths(paths); else paths.push(...getNodeModulesPaths(paths)); let result; let error; try { result = await (0, mlly.resolvePath)(path, { url: paths, extensions: options.extensions ?? DEFAULT_EXTENSIONS, conditions: options.conditions }); } catch (err) { error = err; } if (!result) throw new Error(`Unable to resolve module "${path}". The following import paths were tried: \n${paths.join("\n")}`, { cause: error }); return (0, _stryke_path_correct_path.correctPath)(result); } /** * Resolve the path to a specified module * * @param path - The path to the module * @param options - The options to use when resolving the module * @returns The path to the module or undefined */ function resolveSync(path, options = {}) { let paths = getResolutionPaths(options.paths); if ((0, _stryke_path_is_type.isNpmScopedPackage)(path)) paths = getNodeModulesPaths(paths); else paths.push(...getNodeModulesPaths(paths)); let result; let error; try { result = (0, mlly.resolvePathSync)(path, { url: paths, extensions: options.extensions ?? DEFAULT_EXTENSIONS, conditions: options.conditions }); } catch (err) { error = err; } if (!result) throw new Error(`Unable to resolve module "${path}". The following import paths were tried: \n${paths.join("\n")}`, { cause: error }); return (0, _stryke_path_correct_path.correctPath)(result); } /** * Resolve the path to a specified module with error handling * * @param name - The name of the module * @param options - The options to use when resolving the module * @returns A promise for the path to the module */ async function resolveSafe(name, options = {}) { try { return await resolve(name, options); } catch { return; } } /** * Resolve the path to a specified module with error handling * * @param name - The name of the module * @param options - The options to use when resolving the module * @returns The path to the module or undefined */ function resolveSafeSync(name, options = {}) { try { return resolveSync(name, options); } catch { return; } } /** * Import a module from a specified path * * @param path - The path to the module * @returns The module */ async function importModule(path) { const i = await import(path); if (i) return (0, mlly.interopDefault)(i); return i; } /** * Resolve the path to a specified package asynchronously * * @remarks * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself. * * @param name - The name of the module * @returns A promise for the module or undefined */ async function resolvePackage(name, options = {}) { let result = await resolveSafe((0, _stryke_path_join_paths.joinPaths)(name, "package.json"), options); if (!result) { result = await resolveSafe((0, _stryke_path_join_paths.joinPaths)(name, "index.js"), options); if (!result) result = await resolveSafe(name, options); } return result ? (0, _stryke_path_file_path_fns.findFilePath)(result) : void 0; } /** * Resolve the path to a specified package synchronously * * @remarks * This path points to the root of the package, which is usually the directory containing the `package.json` file. Please note: this path does not include the `package.json` file itself. * * @param name - The name of the module * @param options - The options to use when resolving the module * @returns The module or undefined */ function resolvePackageSync(name, options = {}) { let result = resolveSafeSync((0, _stryke_path_join_paths.joinPaths)(name, "package.json"), options); if (!result) { result = resolveSafeSync((0, _stryke_path_join_paths.joinPaths)(name, "index.js"), options); if (!result) result = resolveSafeSync(name, options); } return result ? (0, _stryke_path_file_path_fns.findFilePath)(result) : void 0; } //#endregion exports.DEFAULT_EXTENSIONS = DEFAULT_EXTENSIONS; exports.getNodeModulesPaths = getNodeModulesPaths; exports.getResolutionCombinations = getResolutionCombinations; exports.getResolutionPaths = getResolutionPaths; exports.importModule = importModule; exports.resolve = resolve; exports.resolvePackage = resolvePackage; exports.resolvePackageSync = resolvePackageSync; exports.resolveSafe = resolveSafe; exports.resolveSafeSync = resolveSafeSync; exports.resolveSync = resolveSync;