Skip to content

Commit

Permalink
Use relative paths for files under current working directory (#78)
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Koops <jonkoops@gmail.com>
  • Loading branch information
jonkoops authored Jul 12, 2024
1 parent c1962ce commit e73f692
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ Here's the output:
There are 4 handle(s) keeping the process running
# Timeout
/path/to/project/example.js:6 - setInterval(() => {}, 1000)
/path/to/project/example.js:10 - startServer()
example.js:6 - setInterval(() => {}, 1000)
example.js:10 - startServer()
# TCPSERVERWRAP
/path/to/project/example.js:7 - server.listen(0)
/path/to/project/example.js:10 - startServer()
example.js:7 - server.listen(0)
example.js:10 - startServer()
# Timeout
/path/to/project/example.js:6 - setInterval(() => {}, 1000)
/path/to/project/example.js:11 - startServer()
example.js:6 - setInterval(() => {}, 1000)
example.js:11 - startServer()
# TCPSERVERWRAP
/path/to/project/example.js:7 - server.listen(0)
/path/to/project/example.js:11 - startServer()
example.js:7 - server.listen(0)
example.js:11 - startServer()
```

## Usage (as a CLI)
Expand Down
28 changes: 23 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export default function whyIsNodeRunning (logger) {
} else {
var padding = ''
stacks.forEach(function (s) {
var pad = (normalizeFileName(s.getFileName()) + ':' + s.getLineNumber()).replace(/./g, ' ')
var pad = formatLocation(s).replace(/./g, ' ')
if (pad.length > padding.length) padding = pad
})
stacks.forEach(function (s) {
var prefix = normalizeFileName(s.getFileName()) + ':' + s.getLineNumber()
var prefix = formatLocation(s)
try {
var src = fs.readFileSync(normalizeFileName(s.getFileName()), 'utf-8').split(/\n|\r\n/)
var src = fs.readFileSync(normalizeFilePath(s.getFileName()), 'utf-8').split(/\n|\r\n/)
logger.error(prefix + padding.slice(prefix.length) + ' - ' + src[s.getLineNumber() - 1].trim())
} catch (e) {
logger.error(prefix + padding.slice(prefix.length))
Expand All @@ -67,6 +67,24 @@ export default function whyIsNodeRunning (logger) {
}
}

function normalizeFileName(fileName) {
return fileName.startsWith('file://') ? fileURLToPath(fileName) : fileName
function formatLocation (stack) {
const filePath = formatFilePath(stack.getFileName())
return `${filePath}:${stack.getLineNumber()}`
}

function formatFilePath (filePath) {
const absolutePath = normalizeFilePath(filePath)
const relativePath = path.relative(process.cwd(), absolutePath)

// If the file is outside the current working directory, return the absolute path.
return relativePath.startsWith('..') ? absolutePath : relativePath
}

function normalizeFilePath (filePath) {
// Convert file:// URLs to file paths.
if (filePath.startsWith('file://')) {
return fileURLToPath(filePath)
}

return filePath
}

0 comments on commit e73f692

Please sign in to comment.