| 10 | //@ directory if no argument is supplied. Returns a |
| 11 | //@ [ShellString](#shellstringstr) to indicate success or failure. |
| 12 | function _cd(options, dir) { |
| 13 | if (!dir) dir = os.homedir(); |
| 14 | |
| 15 | if (dir === '-') { |
| 16 | if (!process.env.OLDPWD) { |
| 17 | common.error('could not find previous directory'); |
| 18 | } else { |
| 19 | dir = process.env.OLDPWD; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | var curDir = process.cwd(); |
| 25 | process.chdir(dir); |
| 26 | process.env.OLDPWD = curDir; |
| 27 | } catch (e) { |
| 28 | // something went wrong, let's figure out the error |
| 29 | var err; |
| 30 | try { |
| 31 | common.statFollowLinks(dir); // if this succeeds, it must be some sort of file |
| 32 | err = 'not a directory: ' + dir; |
| 33 | } catch (e2) { |
| 34 | err = 'no such file or directory: ' + dir; |
| 35 | } |
| 36 | if (err) common.error(err); |
| 37 | } |
| 38 | return ''; |
| 39 | } |
| 40 | module.exports = _cd; |