pi-lens
Version:
Real-time code feedback for pi — LSP, linters, formatters, type-checking, structural analysis & booboo
53 lines (52 loc) • 1.93 kB
JavaScript
import * as fs from "node:fs";
import * as path from "node:path";
import { normalizeMapKey } from "../path-utils.js";
function resolveAgainstAncestors(cwd, filePath) {
if (path.isAbsolute(filePath)) {
return path.resolve(filePath);
}
const normalizedRelative = filePath.replace(/\\/g, "/");
const cwdBaseName = path.basename(path.resolve(cwd)).replace(/\\/g, "/");
if (cwdBaseName &&
(normalizedRelative === cwdBaseName ||
normalizedRelative.startsWith(`${cwdBaseName}/`))) {
const trimmedRelative = normalizedRelative === cwdBaseName
? ""
: normalizedRelative.slice(cwdBaseName.length + 1);
const trimmedCandidate = trimmedRelative
? path.resolve(cwd, trimmedRelative)
: path.resolve(cwd);
if (fs.existsSync(trimmedCandidate)) {
return trimmedCandidate;
}
}
const directCandidate = path.resolve(cwd, filePath);
if (fs.existsSync(directCandidate)) {
return directCandidate;
}
let current = path.resolve(cwd);
const { root } = path.parse(current);
while (current !== root) {
const parent = path.dirname(current);
if (parent === current)
break;
const candidate = path.resolve(parent, filePath);
if (fs.existsSync(candidate)) {
return candidate;
}
current = parent;
}
return directCandidate;
}
export function resolveRunnerPath(cwd, filePath) {
return normalizeMapKey(resolveAgainstAncestors(cwd, filePath));
}
export function toRunnerDisplayPath(cwd, filePath) {
const cwdKey = normalizeMapKey(path.resolve(cwd));
const fileKey = resolveRunnerPath(cwd, filePath);
const relative = path.relative(cwdKey, fileKey).replace(/\\/g, "/");
if (relative && relative !== "." && !relative.startsWith("../")) {
return relative;
}
return fileKey;
}