-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.ts
More file actions
51 lines (48 loc) · 1.38 KB
/
lib.ts
File metadata and controls
51 lines (48 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as path from 'path';
/**
* Transforms a stack string with format 1. into format 2. using
* package.json information of that package.
*
* Formats:
* 1. CWD/node_modules/<module1>/../node_modules/<module2>
* 2. <module1>@<module1-version>/<module2>@<module2-version>
*/
export function npmize(stack: string): string {
const cwd = process.cwd();
const moduleRE = /([^() ]*?)node_modules(.)[^/\\]+[/\\]/g;
let base: string | undefined;
return stack
.replace(moduleRE, ($0, $1: string, $2: string) => {
if ($1) {
const rel = path.relative(cwd, $1);
base = $0;
// Dynamic path: must use require(); import is static, import() is async
const pack = require(path.join(base, 'package.json')) as {
name: string;
version: string;
};
return rel + pack.name + '@' + pack.version + $2;
}
base += $0;
// Dynamic path: must use require(); import is static, import() is async
const pack = require(path.join(base!, 'package.json')) as {
name: string;
version: string;
};
return pack.name + '@' + pack.version + $2;
})
.replace(cwd, '.');
}
export function formatStack(stack: string): string {
return (
npmize(stack) +
'\n--\n' +
'cwd ' +
process.cwd() +
'\n' +
'node-' +
process.version +
' ' +
new Date().toISOString()
);
}