mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
Improved cd performance and fix error messages
This commit is contained in:
parent
1c2ef49f8e
commit
48a3aade2e
23
src/cd.js
23
src/cd.js
@ -16,19 +16,20 @@ function _cd(options, dir) {
|
||||
dir = common.state.previousDir;
|
||||
}
|
||||
|
||||
// This complexity is so that we only stat once.
|
||||
var error = null;
|
||||
try {
|
||||
var stat = fs.statSync(dir);
|
||||
if(stat.isDirectory()) {
|
||||
common.state.previousDir = process.cwd();
|
||||
process.chdir(dir);
|
||||
} else {
|
||||
error = 'not a directory: ' + dir;
|
||||
}
|
||||
var curDir = process.cwd();
|
||||
process.chdir(dir);
|
||||
common.state.previousDir = curDir;
|
||||
} catch (e) {
|
||||
error = 'no such file or directory: ' + dir;
|
||||
// something went wrong, let's figure out the error
|
||||
var err;
|
||||
try {
|
||||
fs.statSync(dir); // if this succeeds, it must be some sort of file
|
||||
err = 'not a directory: ' + dir;
|
||||
} catch (e) {
|
||||
err = 'no such file or directory: ' + dir;
|
||||
}
|
||||
if (err) common.error(err);
|
||||
}
|
||||
if (error) common.error(error);
|
||||
}
|
||||
module.exports = _cd;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user