Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make athenna compatible with bun #78

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.13.1",
"version": "4.13.2",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
48 changes: 47 additions & 1 deletion src/helpers/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as changeCase from 'change-case'
import Youch from 'youch'
import YouchTerminal from 'youch-terminal'

import { readFile } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { Color } from '#src/helpers/Color'
import { Options } from '#src/helpers/Options'
import type { ExceptionJson } from '#src/types'
Expand Down Expand Up @@ -104,7 +106,51 @@ export class Exception extends Error {
this.message = this.message.concat(`\n\n${separator}`)
}

const pretty = await new Youch(this, {}).toJSON()
const youch = new Youch(this, {})

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
youch._getFrameSource = function (frame) {
let path = frame.file
.replace(/dist\/webpack:\//g, '') // unix
.replace(/dist\\webpack:\\/g, '') // windows

/**
* We ignore the error when "fileURLToPath" is unable to parse
* the path, since returning the frame source is an optional
* thing
*/
try {
path = path.startsWith('file:') ? fileURLToPath(path) : path
} catch {}

return new Promise(resolve => {
if (!path) {
resolve(null)
return
}
readFile(path, 'utf-8', (error, contents) => {
if (error) {
resolve(null)
return
}

const lines = contents.split(/\r?\n/)
const lineNumber = frame.line

resolve({
pre: lines.slice(
Math.max(0, lineNumber - (this.options.preLines + 1)),
lineNumber - 1
),
line: lines[lineNumber - 1],
post: lines.slice(lineNumber, lineNumber + this.options.postLines)
})
})
})
}

const pretty = await youch.toJSON()

pretty.error.frames = pretty.error.frames.map(frame => {
frame.isApp = true
Expand Down
8 changes: 7 additions & 1 deletion src/helpers/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,13 @@ export class Path {
*/
public static this(subPath = sep, stackIndex = 1): string {
const stack = callSite()
const requester = dirname(fileURLToPath(stack[stackIndex].getFileName()))
let fileName = stack[stackIndex].getFileName()

if (fileName.startsWith('file:')) {
fileName = fileURLToPath(fileName)
}

const requester = dirname(fileName)
const execDir = normalize(requester.concat(sep, normalize(subPath)))

return this.removeSlashes(execDir)
Expand Down
Loading