• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

earthnutDev / a-node-tools / 14630860861

24 Apr 2025 12:38AM UTC coverage: 39.098% (+6.2%) from 32.891%
14630860861

push

github

earthnutDev
version: 2.2.0 2025-04-24 08:38:51 没啥,更新着玩 😄

71 of 182 branches covered (39.01%)

Branch coverage included in aggregate %.

2 of 3 new or added lines in 2 files covered. (66.67%)

58 existing lines in 6 files now uncovered.

137 of 350 relevant lines covered (39.14%)

1.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/path/getCallerFileInfo.ts
1
import { isWindows } from './isWindows';
2

×
3
/**
×
4
 *
×
5
 * 获取调用文件信息,此方法存在一些限制,请谨慎使用
×
6
 *
7
 *
8
 * @param fileName 通过调用文件信息,返回调用者的文件路径
9
 * @returns 调用文件的信息
10
 *     - name 文件名
11
 *     - line 行号
12
 *     - row  列数
13
 *     - originArr 调用栈信息
14
 *
15
 */
16
export function getCallerFileInfo(fileName: string): {
17
  name: string;
18
  line: number;
19
  row: number;
20
  originArr: string[];
UNCOV
21
} {
×
22
  /** 结果行 */
UNCOV
23
  const regexp = new RegExp(fileName);
×
24
  let errorInfo: Error;
25
  try {
×
26
    // 抛出异常好通过这里捕捉调用栈信息
27
    throw new Error();
UNCOV
28
  } catch (error: unknown) {
×
29
    errorInfo = error as Error;
UNCOV
30
  }
×
31
  const lines: string[] = (
32
    errorInfo.stack?.replace(/\\/gm, '/').split('\n') as string[]
×
33
  ).reverse();
UNCOV
34
  /** 查找结果 */
×
35
  const resultIndex: number = lines.findIndex(
×
36
    (currentEle: string, currentIndex: number, arr: string[]) =>
×
37
      !regexp.test(currentEle) && regexp.test(arr[currentIndex + 1]),
UNCOV
38
  );
×
39
  /** 如果没找到 */
×
40
  if (resultIndex == -1) return { name: '', line: 0, row: 0, originArr: lines };
41

UNCOV
42
  let result = lines[resultIndex];
×
43

×
44
  // 去除结果行中的 () 外部分
45
  if (/\(.*\)/.test(result)) {
46
    result = result.replace(/^.*\((.*)\).*/, '$1');
×
UNCOV
47
  }
×
48
  /** 在 windows 环境去除 file:/// 前缀 */
UNCOV
49
  if (/file:\/*/.test(result)) {
×
50
    result = result.replace(/^.*file:\/*(.*)/, '$1');
51
  }
52
  // 非 windows 桌面添加 /
53
  if (!isWindows && !result.startsWith('/')) {
54
    result = '/' + result;
55
  }
56
  return {
57
    name: result.replace(/^(.*):\d+:\d+$/, '$1'),
58
    line: Number(result.replace(/^.*:(\d+):\d+$/, '$')),
59
    row: Number(result.replace(/^.*:\d+:(\d+)$/, '$1')),
60
    originArr: lines,
61
  };
62
}
63

64
/**
65
 *
66
 * 获取调用文件的文本信息
67
 *
68
 * 需要搭配使用 `initializeFile`
69
 *
70
 *
71
 * @param fileName 请使用 __filename
72
 * @returns 调用者信息
73
 * @example
74
 *
75
 *  ```ts
UNCOV
76
 *  const [__filename,__dirname]  = initializeFile;
×
77
 *  const dir = getCallerFilename(__dirname);
78
 *
79
 * ```
80
 *
81
 *
82
 */
83
export function getCallerFilename(fileName: string) {
84
  return getCallerFileInfo(fileName).name;
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc