Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relative paths for files under current working directory #78

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}