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

Implementing cd('-') to behave like Bash's "cd -" #273

Merged
merged 1 commit into from
Jan 24, 2016
Merged

Implementing cd('-') to behave like Bash's "cd -" #273

merged 1 commit into from
Jan 24, 2016

Conversation

nfischer
Copy link
Member

@nfischer nfischer commented Jan 8, 2016

I added a previousDir field to common.state to allow the cd() function to have this option. I also added unit tests for this behavior.

Usage is as follows:

// Setup
mkdir('foo');
mkdir('bar');
cd('foo');
cd('../bar');
echo(pwd()); // outputs "/path/to/bar"
cd('-'); // jump back
echo(pwd()); // outputs "/path/to/foo"
cd('-'); // jump back again
echo(pwd()); // outputs "/path/to/bar"

@nfischer
Copy link
Member Author

nfischer commented Jan 8, 2016

This can already be done by something like:

var prevDir = pwd();
cd('foo');
// do something
cd(prevDir); // jump back

This PR just makes things a little easier if you're porting sh to shelljs, and makes things a little more convenient for that use-case. This isn't a critical feature, but it doesn't cause too much harm, so I figured I'd implement it.

if (dir === '-') {
var previous_dir = true;
if (!common.state.previousDir)
common.error('OLDPWD not set');
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe make this error message a little more descriptive? OLDPWD takes a minute to understand.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure thing. I just copied the error message from bash. I agree, it probably should be more descriptive.

@ariporad
Copy link
Contributor

@nfischer: Just a few small things, otherwise great job!

@ariporad ariporad assigned nfischer and unassigned ariporad Jan 24, 2016
@nfischer
Copy link
Member Author

@ariporad this should be good now

@ariporad
Copy link
Contributor

LGTM!

ariporad added a commit that referenced this pull request Jan 24, 2016
@ariporad ariporad merged commit 12c47e3 into shelljs:master Jan 24, 2016
@nfischer nfischer deleted the PreviousDirectory 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