@stryke/path
Version:
A package containing various utilities that expand the functionality of NodeJs's built-in `path` module
59 lines (58 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.importModule = importModule;
exports.resolve = resolve;
exports.resolvePackage = resolvePackage;
exports.resolvePackageSync = resolvePackageSync;
exports.resolveSafe = resolveSafe;
exports.resolveSafeSync = resolveSafeSync;
exports.resolveSync = resolveSync;
var _mlly = require("mlly");
var _correctPath = require("./correct-path.cjs");
var _filePathFns = require("./file-path-fns.cjs");
var _getWorkspaceRoot = require("./get-workspace-root.cjs");
var _joinPaths = require("./join-paths.cjs");
async function resolve(r, e = {}) {
const t = e.paths ?? [];
t.length === 0 && t.push(process.cwd());
const n = (0, _getWorkspaceRoot.getWorkspaceRoot)();
return t.includes(n) || t.push(n), (0, _correctPath.correctPath)(await (0, _mlly.resolvePath)(r, {
url: t
}));
}
function resolveSync(r, e = {}) {
const t = e.paths ?? [];
t.length === 0 && t.push(process.cwd());
const n = (0, _getWorkspaceRoot.getWorkspaceRoot)();
return t.includes(n) || t.push(n), (0, _correctPath.correctPath)((0, _mlly.resolvePathSync)(r, {
url: e.paths
}));
}
async function resolveSafe(r, e = {}) {
try {
return await resolve(r, e);
} catch {
return;
}
}
function resolveSafeSync(r, e = {}) {
try {
return resolveSync(r, e);
} catch {
return;
}
}
async function importModule(r) {
const e = await Promise.resolve(`${r}`).then(s => require(s));
return e && (0, _mlly.interopDefault)(e);
}
async function resolvePackage(r, e = {}) {
let t = await resolveSafe((0, _joinPaths.joinPaths)(r, "package.json"), e);
return t || (t = await resolveSafe((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = await resolveSafe(r, e))), t ? (0, _filePathFns.findFilePath)(t) : void 0;
}
async function resolvePackageSync(r, e = {}) {
let t = resolveSafeSync((0, _joinPaths.joinPaths)(r, "package.json"), e);
return t || (t = resolveSafeSync((0, _joinPaths.joinPaths)(r, "index.js"), e), t || (t = resolveSafeSync(r, e))), t ? (0, _filePathFns.findFilePath)(t) : void 0;
}