@supernovaio/cli
Version:
Supernova.io Command Line Interface
109 lines (107 loc) • 5.51 kB
JavaScript
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="468dd210-6d28-58f6-b544-64da77a253bc")}catch(e){}}();
import path from "node:path";
import { declarationPreferredIndexEntryFileNames, exportConditionPriority, exportDeclarationConditionPriority, indexEntryFileNames, packageDeclarationEntryFieldPriority, packageEntryFieldPriority, readPackageJson, resolveExistingModuleFile, rootExportValue, targetCandidates, } from "../../../utils/module-resolution.js";
function getNodeModulesRoots(rootDir) {
const roots = [];
let current = path.resolve(rootDir);
while (true) {
roots.push(path.join(current, "node_modules"));
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
return roots;
}
function packageJsonEntryPaths(packageDir, preferDeclarations = false) {
const packageJson = readPackageJson(packageDir);
if (!packageJson) {
return [];
}
const exportEntries = targetCandidates(rootExportValue(packageJson.exports), preferDeclarations ? exportDeclarationConditionPriority : exportConditionPriority);
const fieldEntries = (preferDeclarations ? packageDeclarationEntryFieldPriority : packageEntryFieldPriority)
.map(field => packageJson[field])
.filter((entry) => typeof entry === "string" && entry.trim().length > 0);
return [...new Set([...exportEntries, ...fieldEntries])]
.filter(entry => entry.startsWith(".") || !path.isAbsolute(entry))
.map(entry => path.join(packageDir, entry));
}
export async function getModuleEntryPath(rootDir, modulePath) {
let entryFile = null;
const sourceFirstPossibleEntries = [
...indexEntryFileNames.map(entry => path.join(modulePath, entry)),
path.join(modulePath, "dist/index.ts"),
path.join(modulePath, "dist/index.tsx"),
path.join(modulePath, "dist/index.mts"),
path.join(modulePath, "dist/index.cts"),
path.join(modulePath, "dist/index.mjs"),
path.join(modulePath, "dist/index.js"),
path.join(modulePath, "dist/index.jsx"),
path.join(modulePath, "dist/index.cjs"),
path.join(modulePath, "dist/index.d.ts"),
path.join(modulePath, "dist/index.d.mts"),
path.join(modulePath, "dist/index.d.cts"),
path.join(modulePath, "lib/index.ts"),
path.join(modulePath, "lib/index.tsx"),
path.join(modulePath, "lib/index.mts"),
path.join(modulePath, "lib/index.cts"),
path.join(modulePath, "lib/index.mjs"),
path.join(modulePath, "lib/index.js"),
path.join(modulePath, "lib/index.jsx"),
path.join(modulePath, "lib/index.cjs"),
path.join(modulePath, "lib/index.d.ts"),
path.join(modulePath, "lib/index.d.mts"),
path.join(modulePath, "lib/index.d.cts"),
];
const declarationFirstPossibleEntries = [
...declarationPreferredIndexEntryFileNames.map(entry => path.join(modulePath, entry)),
path.join(modulePath, "dist/index.d.ts"),
path.join(modulePath, "dist/index.d.mts"),
path.join(modulePath, "dist/index.d.cts"),
path.join(modulePath, "dist/index.ts"),
path.join(modulePath, "dist/index.tsx"),
path.join(modulePath, "dist/index.mts"),
path.join(modulePath, "dist/index.cts"),
path.join(modulePath, "dist/index.mjs"),
path.join(modulePath, "dist/index.js"),
path.join(modulePath, "dist/index.jsx"),
path.join(modulePath, "dist/index.cjs"),
path.join(modulePath, "lib/index.d.ts"),
path.join(modulePath, "lib/index.d.mts"),
path.join(modulePath, "lib/index.d.cts"),
path.join(modulePath, "lib/index.ts"),
path.join(modulePath, "lib/index.tsx"),
path.join(modulePath, "lib/index.mts"),
path.join(modulePath, "lib/index.cts"),
path.join(modulePath, "lib/index.mjs"),
path.join(modulePath, "lib/index.js"),
path.join(modulePath, "lib/index.jsx"),
path.join(modulePath, "lib/index.cjs"),
];
const nodeModulesRoots = getNodeModulesRoots(rootDir);
const nodeModulesPackageDirs = nodeModulesRoots.map(nodeModulesRoot => path.join(nodeModulesRoot, modulePath));
const localPackageDir = path.join(rootDir, modulePath);
const allPossibleEntriesPaths = [
...nodeModulesRoots.flatMap(nodeModulesRoot => declarationFirstPossibleEntries.map(entry => path.join(nodeModulesRoot, entry))),
path.join(rootDir, modulePath),
...sourceFirstPossibleEntries.map(entry => path.join(rootDir, entry)),
...nodeModulesPackageDirs.flatMap(packageDir => packageJsonEntryPaths(packageDir, true)),
...packageJsonEntryPaths(localPackageDir),
];
for (const entryPath of allPossibleEntriesPaths) {
const resolvedEntryPath = resolveExistingModuleFile(entryPath, {
preferDeclarations: entryPath.includes("/node_modules/"),
});
if (resolvedEntryPath) {
entryFile = resolvedEntryPath;
break;
}
}
if (!entryFile) {
throw new Error(`Could not find entry file for "${modulePath}"`);
}
return entryFile;
}
//# sourceMappingURL=get-module-exports-path.js.map
//# debugId=468dd210-6d28-58f6-b544-64da77a253bc