Skip to content

Conversation

@nfischer
Copy link
Member

This adds tilde expansion to the expand() function. Arguments starting with '~/'
will have the tilde expanded to the user's home directory, as with Bash.

Also, if an argument is simply ~, then it will expand to the home directory as well.

> cd('~'); // Go to home directory
> ls('~/*.js'); // Look for all .js files
> cd('~/..'); // Go to /home, /Users, or C:\Users

This does not support all of Bash's tilde expansion (not sure if this would be difficult). For example:

$ ls ~otheruser # look into otheruser's home folder
$ # Nothing like this is supported in ShellJS

@nfischer
Copy link
Member Author

This works right now with the old expand() function. If we rework this function to use the glob module (#275), this should still be compatible.

This still needs to be tested on Windows.

@nfischer
Copy link
Member Author

For future reference, this prefers os.homedir when it's available, which I can see in node v3.0.0 (nodejs/node#1791). For earlier versions, this relies on environmental variables, which appears to work well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just use os-homedir instead? I don't really feel like we need to reinvent the wheel. (It's also not really that big a deal either way).

Copy link
Member Author

Choose a reason for hiding this comment

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

In terms of implementation, this should be very similar. I opted to just code it myself because it cuts out a dependency and it was only a few lines of code. I think it's easier to keep small snippets in the code base.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok

@ariporad ariporad assigned nfischer and unassigned ariporad Jan 24, 2016
This adds tilde expansion to the expand() function. Arguments starting with '~/'
will have the tilde expanded to the user's home directory, as with Bash.
@ariporad
Copy link
Contributor

Did this ever get tested on Windows?

@nfischer
Copy link
Member Author

@ariporad good catch! I did test it on my Windows 7 machine. It passes the test/cd.js unit tests. I also ran a simple shell script myself, and it behaves exactly as I would expect.

cd('~');
echo(pwd());
cd('~/Documents'); // still works, evven with a forward slash
echo(pwd());
cd('~');
echo(pwd());

@nfischer
Copy link
Member Author

That was testing on Windows 7 64 bit, Node v5, running through cmd.exe

@ariporad
Copy link
Contributor

LGTM!

ariporad added a commit that referenced this pull request Jan 24, 2016
feat: add tilde expansion to expand()
@ariporad ariporad merged commit c422069 into shelljs:master Jan 24, 2016
@nfischer nfischer deleted the feat-home-directory-tilde branch January 28, 2016 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants