47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
"use strict";
|
|
|
|
const stackTrace = require(`stack-trace`);
|
|
|
|
const {
|
|
codeFrameColumns
|
|
} = require(`@babel/code-frame`);
|
|
|
|
const fs = require(`fs-extra`);
|
|
|
|
const path = require(`path`);
|
|
|
|
const chalk = require(`chalk`);
|
|
|
|
const gatsbyLocation = path.dirname(require.resolve(`gatsby/package.json`));
|
|
|
|
const getNonGatsbyCallSite = () => stackTrace.get().find(callSite => callSite && callSite.getFileName() && !callSite.getFileName().includes(gatsbyLocation));
|
|
|
|
const getNonGatsbyCodeFrame = ({
|
|
highlightCode = true
|
|
} = {}) => {
|
|
const callSite = getNonGatsbyCallSite();
|
|
|
|
if (!callSite) {
|
|
return null;
|
|
}
|
|
|
|
const fileName = callSite.getFileName();
|
|
const line = callSite.getLineNumber();
|
|
const column = callSite.getColumnNumber();
|
|
const code = fs.readFileSync(fileName, {
|
|
encoding: `utf-8`
|
|
});
|
|
return [`File ${chalk.bold(`${fileName}:${line}:${column}`)}`, codeFrameColumns(code, {
|
|
start: {
|
|
line,
|
|
column
|
|
}
|
|
}, {
|
|
highlightCode
|
|
})].join(`\n`);
|
|
};
|
|
|
|
module.exports = {
|
|
getNonGatsbyCodeFrame
|
|
};
|
|
//# sourceMappingURL=stack-trace-utils.js.map
|