From 9da525c9fdb359959387b2471919e1953a91758c Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Tue, 3 Oct 2023 16:00:18 +0800 Subject: [PATCH] fix: correctly get caller path --- package.json | 1 - pnpm-lock.yaml | 30 ++++-------------------------- src/core.ts | 22 +++++++++++++++++++++- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index c8586f5..81aa2fb 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "@iconify/json": "^2.2.73", "@iconify/types": "^2.0.0", "@types/node": "^18.13.0", - "caller-path": "^4.0.0", "postcss": "^8.4.21", "prettier": "3.0.3", "prettier-plugin-tailwindcss": "^0.5.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6efa801..d1ddb54 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,9 +1,5 @@ lockfileVersion: '6.0' -settings: - autoInstallPeers: true - excludeLinksFromLockfile: false - dependencies: '@iconify/utils': specifier: ^2.1.4 @@ -25,9 +21,6 @@ devDependencies: '@types/node': specifier: ^18.13.0 version: 18.13.0 - caller-path: - specifier: ^4.0.0 - version: 4.0.0 postcss: specifier: ^8.4.21 version: 8.4.21 @@ -676,25 +669,6 @@ packages: engines: {node: '>=8'} dev: true - /caller-callsite@5.0.0: - resolution: {integrity: sha512-l3+xZHsuNlFjchy1nl1kpQwi7PhKHgtQ7MOx5ijHwlouAjnwRNjq3AlxH8SGuWiF5XAhb/mSH+c90LIDFjJKtA==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - dependencies: - callsites: 4.0.0 - dev: true - - /caller-path@4.0.0: - resolution: {integrity: sha512-18JFq7TTe4+cDKsyZETRk/IS9Xka3dRcU9VWm1C6XlkAjKPSR2bskpDSUHVQw7gvef34bi1gltzmCxToUw1oQw==} - engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} - dependencies: - caller-callsite: 5.0.0 - dev: true - - /callsites@4.0.0: - resolution: {integrity: sha512-y3jRROutgpKdz5vzEhWM34TidDU8vkJppF8dszITeb1PQmSqV3DTxyV8G/lyO/DNvtE1YTedehmw9MPZsCBHxQ==} - engines: {node: '>=12.20'} - dev: true - /camelcase-css@2.0.1: resolution: {integrity: sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==} engines: {node: '>= 6'} @@ -1905,3 +1879,7 @@ packages: resolution: {integrity: sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==} engines: {node: '>=12.20'} dev: true + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false diff --git a/src/core.ts b/src/core.ts index 1920f04..ddbf3ce 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,5 +1,4 @@ import path from "path" -import callerPath from "caller-path" import fs from "fs" import { IconifyIcon, IconifyJSON } from "@iconify/types" import { getIconCSS, getIconData } from "@iconify/utils" @@ -22,6 +21,27 @@ export const localResolve = (cwd: string, id: string) => { } } +function callerPath(): string | null { + const error = new Error() + const stack = error.stack?.split("\n") as string[] + + // first path do not contains ( and ) and is not Error + const data = stack.find( + (line) => line !== "Error" && !line.includes("(") && !line.includes(")"), + ) + if (!data) { + return null + } + + const filePathPattern = new RegExp(/\s*at ((?:\/\S+)+):\d+:\d+/) + const result = filePathPattern.exec(data) + if (!result) { + return null + } + + return result[1] +} + export const getIconCollections = ( include: CollectionNames[] | "all" = "all", ): Record => {