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

hexojs / hexo-util / 16651986209

31 Jul 2025 02:35PM UTC coverage: 73.036% (-23.8%) from 96.875%
16651986209

Pull #426

github

web-flow
Merge e24598988 into d497bc760
Pull Request #426: build: restructure for dual ESM/CJS output, update tooling, and enhance utilities

811 of 1128 branches covered (71.9%)

1594 of 2028 new or added lines in 40 files covered. (78.6%)

6 existing lines in 1 file now uncovered.

5290 of 7243 relevant lines covered (73.04%)

69.13 hits per line

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

85.71
/lib/cross_dirname.ts
1
const DIRNAME_POSIX_REGEX =
2✔
2
  /^((?:\.(?![^/]))|(?:(?:\/?|)(?:[\s\S]*?)))(?:\/+?|)(?:(?:\.{1,2}|[^/]+?|)(?:\.[^./]*|))(?:[/]*)$/;
2✔
3
const DIRNAME_WIN32_REGEX =
2✔
4
  /^((?:\.(?![^\\]))|(?:(?:\\?|)(?:[\s\S]*?)))(?:\\+?|)(?:(?:\.{1,2}|[^\\]+?|)(?:\.[^.\\]*|))(?:[\\]*)$/;
2✔
5
const EXTRACT_PATH_REGEX = /@?([file://]?[^(\s]+):[0-9]+:[0-9]+/;
2✔
6
const WIN_POSIX_DRIVE_REGEX = /^\/[A-Z]:\/*/;
2✔
7

2✔
8
const pathDirname = (path: string) => {
2✔
9
  let dirname = DIRNAME_POSIX_REGEX.exec(path)?.[1];
2✔
10

2✔
11
  if (!dirname) {
2!
NEW
12
    dirname = DIRNAME_WIN32_REGEX.exec(path)?.[1];
×
NEW
13
  }
×
14

2✔
15
  if (!dirname) {
2!
NEW
16
    throw new Error(`Can't extract dirname from ${path}`);
×
NEW
17
  }
×
18

2✔
19
  return dirname;
2✔
20
};
2✔
21

2✔
22
const getPathFromErrorStack = () => {
2✔
23
  let path = '';
5✔
24
  const stack = new Error().stack;
5✔
25

5✔
26
  if (!stack) {
5!
NEW
27
    console.warn('Error has no stack!');
×
NEW
28
    return path;
×
NEW
29
  }
×
30

5✔
31
  // Node.js
5✔
32
  let initiator: string | undefined = stack.split('\n').slice(4, 5)[0];
5✔
33

5✔
34
  // Other
5✔
35
  if (!initiator) {
5!
NEW
36
    initiator = stack.split('\n').slice(3, 4)[0];
×
NEW
37
  }
×
38

5✔
39
  if (initiator) {
5✔
40
    const match = EXTRACT_PATH_REGEX.exec(initiator);
5✔
41
    path = match && match[1] ? match[1] : '';
5!
42
  }
5✔
43

5✔
44
  if (!initiator || !path) {
5!
NEW
45
    console.warn("Can't get path from error stack!");
×
NEW
46
  }
×
47

5✔
48
  return path;
5✔
49
};
5✔
50

2✔
51
const getPath = () => {
2✔
52
  let path = getPathFromErrorStack();
5✔
53

5✔
54
  // Remove protocol
5✔
55
  const protocol = 'file://';
5✔
56
  if (path.indexOf(protocol) >= 0) {
5✔
57
    path = path.slice(protocol.length);
5✔
58
  }
5✔
59

5✔
60
  // Transform to win32 path
5✔
61
  if (WIN_POSIX_DRIVE_REGEX.test(path)) {
5!
NEW
62
    path = path.slice(1).replace(/\//g, '\\');
×
NEW
63
  }
×
64

5✔
65
  return path;
5✔
66
};
5✔
67

2✔
68
/**
2✔
69
 * Cross platform implementation for `__dirname`.
2✔
70
 *
2✔
71
 * @note Please do not use this method in nested other methods,
2✔
72
 * instead always use it in the root of your file, otherwise it may return wrong results.
2✔
73
 * @returns What `__dirname` would return in CJS
2✔
74
 */
2✔
75
export const getDirname = () => {
2✔
76
  const path = getPath();
2✔
77
  const dirname = pathDirname(path);
2✔
78
  return dirname;
2✔
79
};
2✔
80

2✔
81
/**
2✔
82
 * Cross platform implementation for `__filename`.
2✔
83
 *
2✔
84
 * @note Please do not use this method in nested other methods,
2✔
85
 * instead always use it in the root of your file, otherwise it may return wrong results.
2✔
86
 * @returns What `__filename` would return in CJS
2✔
87
 */
2✔
88
export const getFilename = () => {
2✔
89
  const filename = getPath();
3✔
90
  return filename;
3✔
91
};
3✔
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

© 2025 Coveralls, Inc