sindresorhus / clean-stack

Clean up error stack traces
MIT License
337 stars 29 forks source link

Support relative paths #10

Closed Tyler-Murphy closed 5 years ago

Tyler-Murphy commented 5 years ago

Currently, the pretty option can be specified to replace the path to the home directory with ~. I think it'd be nice to have an option to show relative paths, so they're even shorter. As-is, the leading part of every path is often identical.

This additional check in the step where the path is modified seems to do it, assuming options.relativePath is true.

        .map(line => {
            if (options.pretty) {
                return line.replace(extractPathRegex, (m, p1) => m.replace(p1, p1.replace(homeDir, '~')));
            }

            if (options.relativePath) {
                return line.replace(extractPathRegex, (m, p1) => m.replace(p1, path.relative(process.cwd(), p1)));
            }

            return line;
        })

Would you accept a PR to add this option? Any preferences for the implementation?

sindresorhus commented 5 years ago

What would the paths be relative to?

Tyler-Murphy commented 5 years ago

They'd be relative to process.cwd(), which is the directory the command was run from, assuming the command didn't change directories.

For example, if my current directory is ~/code, and I run node projectFolder/someFile.js, and someFile.js throws an error that's caught and passed through clean-stack, this new option would cause the path in the stack to be projectName/someFile.js.

sindresorhus commented 5 years ago

I don't think that is a good idea. That means you need to know from where the code was run to be able to read stack traces. I'm going to pass on this because it's too easily abused. Stack traces are important. Cleaning non-essential information is one thing, but making the paths hard to interpret in certain situations is not something I want to encourage.