-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.ts
99 lines (86 loc) · 2.39 KB
/
node.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import smc from '@cspotcode/source-map'
import Kink from '@termsurf/kink'
import fs from 'fs'
import pathResolve from 'path'
import makeText from './index.js'
export * from './index.js'
const HOST_LINK_MESH: Record<string, smc.SourceMapConsumer> = {}
function loadHostLink(line: string): smc.SourceMapConsumer | void {
const link = HOST_LINK_MESH[`${line}`]
if (link) {
return link
}
const fileLink = line.replace(/^file:\/\//, '')
if (fs.existsSync(`${fileLink}.map`)) {
const mapContent = fs.readFileSync(`${fileLink}.map`, 'utf-8')
const json = JSON.parse(mapContent) as smc.RawSourceMap
const sm = new smc.SourceMapConsumer(json)
HOST_LINK_MESH[`${line}`] = sm
return sm
}
}
function readLinkBase(path: string): string {
const relative = pathResolve.relative(process.cwd(), path)
// get pnpm symlink path so it's nicer to see.
if (relative.match(/^(node_modules\/\.pnpm\/.+\/node_modules)/)) {
return `node_modules/${relative.slice(RegExp.$1.length)}`
}
return relative
}
function readLink(path: string, context: string): string {
return readLinkBase(pathResolve.resolve(context, path))
}
function readHostLinkFile(
file: string,
line: number,
rise: number,
): [string, number | undefined, number | undefined] {
const link = loadHostLink(file)
const trace = {
column: rise,
filename: file,
line: line,
}
if (link) {
const token = link.originalPositionFor(trace)
if (token.source) {
return [
readLink(
token.source,
pathResolve.dirname(file.replace(/^file:\/\//, '')),
),
token.line == null ? undefined : token.line,
token.column == null ? undefined : token.column,
]
} else {
return [file, line, rise]
}
} else {
return [readLinkBase(file), line, rise]
}
}
export { makeText }
export function makeKinkText(kink: Kink): string {
return makeText({
time: kink.time,
host: kink.host,
code: kink.code,
note: kink.note,
list: kink.stack?.split('\n') ?? [],
link: kink.link,
hook: readHostLinkFile,
})
}
export function makeBaseKinkText(kink: Error) {
return makeText({
time: Kink.makeTime(Date.now()),
host: 'node',
code:
'code' in kink && typeof kink.code === 'string'
? kink.code
: '0000',
note: kink.message,
list: kink.stack?.split('\n') ?? [],
hook: readHostLinkFile,
})
}