Skip to content

Commit

Permalink
fix: correctly obtain callerPath
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Dec 16, 2023
1 parent b53dde9 commit 135e2f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
</head>
<body>
<div class="i-heroicons-academic-cap bg-white text-red-500"></div>
<div class="i-[mdi--ab-testing] text-white"></div>
</body>
</html>
26 changes: 1 addition & 25 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IconifyIcon, IconifyJSON } from "@iconify/types"
import { getIconCSS, getIconData } from "@iconify/utils"
import { createRequire } from "module"
import { CollectionNames } from "../types"
import { callerPath } from "./utils"

export type GenerateOptions = {
/**
Expand Down Expand Up @@ -35,31 +36,6 @@ export const localResolve = (cwd: string, id: string) => {
}
}

function callerPath(): string | null {
const error = new Error()
const stack = error.stack?.split("\n") as string[]

const data = stack.find(
(line) =>
!line.trim().startsWith("Error") &&
!line.includes("(") &&
!line.includes(")"),
)
if (!data) {
return null
}

const filePathPattern = new RegExp(
/\s*at (\/.*|[a-zA-Z]:\\(?:([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\|..\\)*([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\?|..\\))?):\d+:\d+/i,
)
const result = filePathPattern.exec(data)
if (!result) {
return null
}

return result[1]
}

export const isPackageExists = (id: string) => {
const p = callerPath()
const cwd = p ? path.dirname(p) : process.cwd()
Expand Down
52 changes: 52 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>

function callsites() {
const _prepareStackTrace = Error.prepareStackTrace
try {
let result: NodeJS.CallSite[] = []
Error.prepareStackTrace = (_, callSites) => {
const callSitesWithoutCurrent = callSites.slice(1)
result = callSitesWithoutCurrent
return callSitesWithoutCurrent
}

new Error().stack
return result
} finally {
Error.prepareStackTrace = _prepareStackTrace
}
}

function callerPath1() {
const callSites = callsites()
if (!callSites[0]) return
return callSites[0].getFileName()
}

function callerPath2() {
const error = new Error()
const stack = error.stack?.split("\n") as string[]

const data = stack.find(
(line) =>
!line.trim().startsWith("Error") &&
!line.includes("(") &&
!line.includes(")"),
)
if (!data) {
return
}

const filePathPattern = new RegExp(
/\s*at (\/.*|[a-zA-Z]:\\(?:([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\|..\\)*([^<>:"\/\\|?*]*[^<>:"\/\\|?*.]\\?|..\\))?):\d+:\d+/i,
)
const result = filePathPattern.exec(data)
if (!result) {
return
}

return result[1]
}

export function callerPath() {
return callerPath1() ?? callerPath2()
}

0 comments on commit 135e2f0

Please sign in to comment.