forked from bsv-blockchain/ts-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize-lcov-paths.mjs
More file actions
55 lines (43 loc) · 1.51 KB
/
Copy pathnormalize-lcov-paths.mjs
File metadata and controls
55 lines (43 loc) · 1.51 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
52
53
54
55
#!/usr/bin/env node
import { readFileSync, writeFileSync } from 'node:fs'
import { isAbsolute, relative } from 'node:path'
const [lcovPath, sourcePrefix] = process.argv.slice(2)
if (lcovPath == null || sourcePrefix == null) {
console.error('Usage: node scripts/normalize-lcov-paths.mjs <lcov.info> <source-prefix>')
process.exit(1)
}
const slashify = (value) => value.replaceAll('\\', '/')
const trimTrailingSlashes = (value) => {
let end = value.length
while (end > 0 && value[end - 1] === '/') end -= 1
return value.slice(0, end)
}
const trimLeadingSlashes = (value) => {
let start = 0
while (start < value.length && value[start] === '/') start += 1
return value.slice(start)
}
const prefix = trimTrailingSlashes(slashify(sourcePrefix))
const content = readFileSync(lcovPath, 'utf8')
const cwd = process.cwd()
const normalizePath = (filePath) => {
let normalized = slashify(filePath)
if (isAbsolute(normalized)) {
const relativePath = slashify(relative(cwd, normalized))
if (!relativePath.startsWith('..')) {
normalized = relativePath
}
}
if (isAbsolute(normalized) || normalized === prefix || normalized.startsWith(`${prefix}/`)) {
return normalized
}
return `${prefix}/${trimLeadingSlashes(normalized)}`
}
const normalized = content
.split('\n')
.map(rawLine => {
const line = rawLine.endsWith('\r') ? rawLine.slice(0, -1) : rawLine
return line.startsWith('SF:') ? `SF:${normalizePath(line.slice(3))}` : line
})
.join('\n')
writeFileSync(lcovPath, normalized)