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

fix: Improve cleanPath platform compatability #249

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hyperupcall
Copy link
Contributor

@hyperupcall hyperupcall commented Oct 12, 2023

Uses os.homedir() since that works better on non-POSIXy platforms.

I know this code uses old conventions and methods, so if NodeJS version compatability is a concern (even though they have been unsupported for decades), these are when these functions were added to NodeJS:

doctoc.js Outdated
, minimist = require('minimist')
, file = require('./lib/file')
, transform = require('./lib/transform')
, files;

function cleanPath(path) {
var homeExpanded = (path.indexOf('~') === 0) ? process.env.HOME + path.substr(1) : path;
var homeExpanded = (path.indexOf('~') === 0) ? path.join(os.homedir() + path.substr(1)) : path;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While using os.homedir() indeed improves cross-platform compatibility, I noticed that the introduction of path.join() deviates slightly from the existing style. I'm unsure if this stylistic change was intended for this PR.

Additionally, if path.join() is used, it should accept multiple arguments (e.g., path.join(os.homedir(), path.substr(1))) rather than a single concatenated string.

Let me know your thoughts on this!

Copy link
Contributor Author

@hyperupcall hyperupcall Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! Embarrassingly, when I first tested these changes I must have forgotten to quote:

$ ./doctoc.js ~/Documents/...
$ ./doctoc.js '~/Documents/...'

I re-ran and it works as expected. I also fixed up the code to allow for using path.join by renaming the parameter in the function. path.join is also more cross platform.

function cleanPath(path) {
var homeExpanded = (path.indexOf('~') === 0) ? process.env.HOME + path.substr(1) : path;
function cleanPath(filepath) {
var homeExpanded = (filepath.indexOf('~') === 0) ? path.join(os.homedir(), filepath.substr(1)) : filepath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is renaming path to filepath a mistake because it's unrelated to the topic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope it's intentional; I renamed path to filepath because the path parameter in the cleanPath function was shadowing the path variable defined at the top of the file (var path = require('path')). And this caused a bug, since path.join is a function exported from the path module - String.prototype.join() does not exist. A mistake in my testing prevented me from catching this earlier

As I understand both files and folders can be passed to the cleanPath function, maybe you prefer a different rename?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, okay, I thought it was about the rewrite of process.env.HOME only, will take a deeper look at it.

Copy link

@Joj501 Joj501 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved to change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants