UNPKG

@plugjs/plug

Version:
133 lines (132 loc) 4.06 kB
// paths.ts import { statSync } from "node:fs"; import { createRequire } from "node:module"; import { dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { assert } from "./asserts.mjs"; function resolveAbsolutePath(directory, ...paths) { assertAbsolutePath(directory); return resolve(directory, ...paths); } function resolveRelativeChildPath(directory, ...paths) { const abs = resolveAbsolutePath(directory, ...paths); const rel = relative(directory, abs); return isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`) ? void 0 : rel; } function assertRelativeChildPath(directory, ...paths) { const relative2 = resolveRelativeChildPath(directory, ...paths); assert(relative2, `Path "${join(...paths)}" not relative to "${directory}"`); return relative2; } function isAbsolutePath(path) { return isAbsolute(path); } function assertAbsolutePath(p) { assert(isAbsolute(p), `Path "${p}" not absolute`); } function getAbsoluteParent(path) { assertAbsolutePath(path); return dirname(path); } function getCurrentWorkingDirectory() { const cwd = process.cwd(); assertAbsolutePath(cwd); return cwd; } function commonPath(path, ...paths) { assertAbsolutePath(path); const components = normalize(path).split(sep); let length = components.length; for (const current of paths) { const absolute = resolveAbsolutePath(path, current); const parts = absolute.split(sep); for (let i = 0; i < length; i++) { if (components[i] !== parts[i]) { length = i; break; } } assert(length, "No common ancestors amongst paths"); } const common = components.slice(0, length).join(sep); assertAbsolutePath(common); return common; } function resolveFilename(__fileurl) { const file = __fileurl.startsWith("file:") ? fileURLToPath(__fileurl) : __fileurl; assertAbsolutePath(file); return file; } function filenameFromUrl(__fileurl) { const file = resolveFilename(__fileurl); assert(resolveFile(file), `Unable to resolve "${__fileurl}" as a file`); return file; } function dirnameFromUrl(__fileurl) { const dir = getAbsoluteParent(resolveFilename(__fileurl)); assert(resolveDirectory(dir), `Unable to resolve "${__fileurl}" as a directory`); return dir; } function requireFilename(__fileurl, ...paths) { const file = resolveFilename(__fileurl); assertAbsolutePath(file); if (!paths.length) return file; const directory = getAbsoluteParent(file); return resolveAbsolutePath(directory, ...paths); } function requireResolve(__fileurl, module) { const file = resolveFilename(__fileurl); if (module.match(/^\.\.?\//)) { const url = pathToFileURL(file); const ext = extname(file); const checks = [`${module}`, `${module}${ext}`, `${module}/index${ext}`]; for (const check of checks) { const resolved = fileURLToPath(new URL(check, url)); if (resolveFile(resolved)) { module = check; break; } } } const require2 = createRequire(file); const required = require2.resolve(module); assertAbsolutePath(required); return required; } function resolveFile(path, ...paths) { const file = resolveAbsolutePath(path, ...paths); try { const stat = statSync(file); if (stat.isFile()) return file; } catch (error) { if (error.code !== "ENOENT") throw error; } return void 0; } function resolveDirectory(path, ...paths) { const directory = resolveAbsolutePath(path, ...paths); try { const stat = statSync(directory); if (stat.isDirectory()) return directory; } catch (error) { if (error.code !== "ENOENT") throw error; } return void 0; } export { assertAbsolutePath, assertRelativeChildPath, commonPath, dirnameFromUrl, filenameFromUrl, getAbsoluteParent, getCurrentWorkingDirectory, isAbsolutePath, requireFilename, requireResolve, resolveAbsolutePath, resolveDirectory, resolveFile, resolveRelativeChildPath }; //# sourceMappingURL=paths.mjs.map