Skip to content

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

Merged
ariporad merged 1 commit intoshelljs:masterfrom
nfischer:PreviousDirectory
Jan 24, 2016
Merged

Implementing cd('-') to behave like Bash's "cd -"#273
ariporad merged 1 commit intoshelljs:masterfrom
nfischer:PreviousDirectory

Conversation

@nfischer
Copy link
Copy Markdown
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
Copy Markdown
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.

src/cd.js Outdated
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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

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

@ariporad this should be good now

@ariporad
Copy link
Copy Markdown
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