cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
48 lines (47 loc) • 1.29 kB
JavaScript
import fs from "node:fs";
import pc from "picocolors";
import { codeFrameColumns } from "@babel/code-frame";
const readFileSync = fs.readFileSync;
function getCodeFrame(frame) {
const locationStart = frame.indexOf("(") + 1;
const locationEnd = frame.indexOf(")");
const frameLocation = frame.slice(locationStart, locationEnd);
const frameLocationElements = frameLocation.split(":");
const [filename, line, column] = [
frameLocationElements[0],
parseInt(frameLocationElements[1], 10),
parseInt(frameLocationElements[2], 10)
];
let rawFileContents = "";
try {
rawFileContents = readFileSync(filename, "utf-8");
} catch (e) {
return "";
}
const codeFrame = codeFrameColumns(
rawFileContents,
{
start: { line, column }
},
{
highlightCode: false,
linesBelow: 0
}
);
return `${pc.dim(frameLocation)}
${codeFrame}
`;
}
function getUserCodeFrame() {
var _a;
if (!readFileSync || !codeFrameColumns) {
return "";
}
const err = new Error();
const firstClientCodeFrame = (_a = err.stack) == null ? void 0 : _a.split("\n").slice(1).find((frame) => !frame.includes("node_modules/"));
return getCodeFrame(firstClientCodeFrame);
}
export {
getUserCodeFrame
};
//# sourceMappingURL=get-user-code-frame.js.map